Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 528

How to detect Esc key using Unix module and raw mode?

$
0
0

This is the code in OCaml to detect keys using the Unix module and raw mode in the linux terminal.

The only key I can't detect is ESC. There could be others, but my main goal is to detect ESC key.

Here is the code of the function:

    (* Function to read a character and recognize special keys *)    let read_character () =      let in_chan = in_channel_of_descr stdin in      let c = input_char in_chan in      if c = '\027' then        let next1 = input_char in_chan in        let next2 = input_char in_chan in        match (next1, next2) with        | ('[', 'A') -> "Up Arrow"        | ('[', 'B') -> "Down Arrow"        | ('[', 'C') -> "Right Arrow"        | ('[', 'D') -> "Left Arrow"        | ('[', 'H') -> "Home"        | ('[', 'F') -> "End"        | ('[', '3') when input_char in_chan = '~' -> "Delete"        | ('[', '2') when input_char in_chan = '~' -> "Insert"        | ('[', '5') when input_char in_chan = '~' -> "Page Up"        | ('[', '6') when input_char in_chan = '~' -> "Page Down"        | _ -> "Ignore"      else        match c with        | '\n' -> "Enter"        (* Handle Enter key *)        | '\127' -> "Backspace"  (* Handle Backspace (DEL) *)        | _ -> String.make 1 c   (* Return single character *)

Please, take into account that C/curses works, but it is very slow to initialize/finish and it clears the screen, so it is not an optimal option.

It is kind of a challenge. AI suggested me using Lwt/Async, but none of that worked, since I didn't have success installing that in Arch Linux.


Viewing all articles
Browse latest Browse all 528

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>