检查是否“控制”在lablgtk2中按下

发布于 2024-11-18 06:12:02 字数 405 浏览 0 评论 0原文

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

I'm having a lot of trouble with the test_modifier method in lablgtk2. I can test for Shift, but that isn't very useful for my purposes. Whenever I test if control and another key is pressed nothing happens. I've also tried this:

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
          ...

It worked for awhile and then it stopped. What is wrong with the above code that it wouldn't do anything? How can I properly test for a Control key press in lablgtk2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱世争霸 2024-11-25 06:12:02

一次可以有多个修饰符,因此与 [`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 ()

There can be several modifiers at once so comparing to [`CONTROL] is not valid.

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 ()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文