检查lablgtk2中是否按下了“control”

时间:2011-06-29 16:53:49

标签: user-interface ocaml

我在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键?

1 个答案:

答案 0 :(得分:3)

可以同时使用多个修饰符,因此与[`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 ()