我在lablgtk2中使用test_modifier方法时遇到了很多问题。我可以测试Shift,但这对我的目的不是很有用。每当我测试是否控制并按下另一个键时,都不会发生任何事情。我也试过这个:
view#event#connect#key_press ~callback:(fun ev ->
let m = GdkEvent.Key.state ev in
let k = GdkEvent.Key.keyval ev in
if (m = [`CONTROL] && k = _F) then
...
它工作了一段时间,然后就停止了。上面的代码有什么问题,它什么都不会做?如何在lablgtk2中正确测试控制键按下?
一次可以有多个修饰符,因此与[`CONTROL]进行比较是无效的。
let pr fmt = Printf.ksprintf print_endline fmt
let button label packing f =
let b = GButton.button ~label ~packing () in
let _ = b#connect#clicked ~callback:f in
()
let () =
let locale = GtkMain.Main.init () in
let window = GWindow.window ~title:"test" ~border_width:10 () in
let _ = window#connect#destroy ~callback:GMain.quit in
let mainbox = GPack.vbox ~packing:window#add () in
button "quit" mainbox#pack window#destroy;
let _ = window#event#connect#key_press ~callback:begin fun ev ->
let m = GdkEvent.Key.state ev in
let k = GdkEvent.Key.keyval ev in
if (List.mem `CONTROL m && k = GdkKeysyms._F) then pr "WOO HOO";
if (List.mem `CONTROL m && k = GdkKeysyms._f) then pr "woo hoo";
false
end in
window#event#add [`KEY_PRESS];
window#show ();
GMain.main ()