2016-01-09 26 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

感謝您的回覆! –