Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ocaml, Module Graphics - Queuing keypresses

Tags:

ocaml

I am writing a simple game in Ocaml, using its module Graphics to perform drawing and interaction. I came across the problem that Graphics.read_key() queues all presses for later use, therefore when I hold a key for a while then many "presses" are put into memory. After release the action is still performed.

Is there any way to delete entries from this queue, or just (even better) not queue them at all?

like image 249
mechu Avatar asked Mar 03 '26 14:03

mechu


1 Answers

THis is problably not the most beautiful solution, but you can use key_pressed. This function will return true if a keypress is available. So once you have read a keypress with read_key you can flush your queue by calling read_key until key_pressed is false and ignoring the result.

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

Hope this could help.

like image 73
Benoît Fraikin Avatar answered Mar 09 '26 01:03

Benoît Fraikin