Ocaml, modular graphics - keystrokes

I am writing a simple game in Ocaml using my module Graphicsfor drawing and interacting. I ran into a problem that Graphics.read_key()stops all keystrokes for later use, so when I hold the key for a while, many “keystrokes” are put into memory. After release, the action is still in progress.

Is there a way to remove entries from this queue or just (even better) not to queue them?

+3
source share
1 answer

This is probably not the most beautiful solution, but you can use it key_pressed. This function will return true if a keystroke is available. So, as soon as you read the keystroke read_key, you can discard your turn by calling read_keyuntil key_pressedit becomes false and ignores the result.

(* flush_kp : unit -> unit *)
let flush_kp () = while key_pressed () do
                      let c = read_key ()
                      in ()
                  done ;;

Hope this helps.

+5
source

All Articles