How to read a character in OCaml without a return key?

自古美人都是妖i 提交于 2019-12-23 07:59:35

问题


I'm looking for something like input_char stdin but without waiting for a return key. I would not to depend on a big dependency like lambda-term.


回答1:


Handling input in full lines is easy. Handling it a character at a time is a little bit system dependent. If you're on a Unix-like system you should be able to do this using the Unix module:

let get1char () =
    let termio = Unix.tcgetattr Unix.stdin in
    let () =
        Unix.tcsetattr Unix.stdin Unix.TCSADRAIN
            { termio with Unix.c_icanon = false } in
    let res = input_char stdin in
    Unix.tcsetattr Unix.stdin Unix.TCSADRAIN termio;
    res


来源:https://stackoverflow.com/questions/13410159/how-to-read-a-character-in-ocaml-without-a-return-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!