这是 OCaml 中的代码,用于在 Linux 终端中使用 Unix 模块和原始模式检测按键。
我唯一无法检测到的键是 ESC。可能还有其他键,但我的主要目标是检测 ESC 键。
该函数的代码如下:
(* 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 *)
请注意,C/curses 可以工作,但是初始化/完成速度非常慢并且会清除屏幕,因此它不是最佳选择。
这确实是一个挑战。AI 建议我使用 Lwt/Async,但这些都不起作用,因为我无法在 Arch Linux 中成功安装它们。