与 OCaml 图形的交互
Interactivity with OCaml Graphics
嘿伙计,我不太了解 OCaml,但我必须使用标准图形模块而不是 lablgtk 编写一个小型 GUI,我想知道它在图形中如何工作以监听按键和鼠标移动等事件,文档对我来说似乎有点神秘,可以给我一个小例子吗?
提前致谢,
科林
此代码使用 OCaml 图形模块在图形中显示用户的鼠标位置和按键操作 window:
open Graphics
open Printf
(* Displays mouse position and keys pressed in the graphics window,
and exits if q is pressed. *)
let rec loop () =
let e = wait_next_event [Mouse_motion; Key_pressed] in
let mouse_description = sprintf "Mouse position: %d,%d" e.mouse_x e.mouse_y in
let key_description = if e.keypressed then sprintf "Key %c was pressed" e.key else "" in
clear_graph ();
moveto 0 100; draw_string key_description;
moveto 0 0; draw_string mouse_description;
if e.key <> 'q' then loop () else ()
let () =
open_graph "";
loop ();
close_graph ();
嘿伙计,我不太了解 OCaml,但我必须使用标准图形模块而不是 lablgtk 编写一个小型 GUI,我想知道它在图形中如何工作以监听按键和鼠标移动等事件,文档对我来说似乎有点神秘,可以给我一个小例子吗?
提前致谢, 科林
此代码使用 OCaml 图形模块在图形中显示用户的鼠标位置和按键操作 window:
open Graphics
open Printf
(* Displays mouse position and keys pressed in the graphics window,
and exits if q is pressed. *)
let rec loop () =
let e = wait_next_event [Mouse_motion; Key_pressed] in
let mouse_description = sprintf "Mouse position: %d,%d" e.mouse_x e.mouse_y in
let key_description = if e.keypressed then sprintf "Key %c was pressed" e.key else "" in
clear_graph ();
moveto 0 100; draw_string key_description;
moveto 0 0; draw_string mouse_description;
if e.key <> 'q' then loop () else ()
let () =
open_graph "";
loop ();
close_graph ();