2016-01-09 27 views
0

嗨,我并不是很了解OCaml,但我必须使用标准图形模块而不是lablgtk来编写一个小型GUI,我想知道它是如何在Graphics中工作来监听事件,如按键和鼠标移动,文档似乎有点神秘,我可以给我一个小例子吗?与OCaml图形互动

由于提前, 科林

回答

1

该代码使用OCaml的图形模块显示在图形窗口中用户的鼠标位置和按键:

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(); 
+0

感谢您的回复! –