metalnick
metalnick

Reputation: 43

Checking if "control" is pressed in lablgtk2

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?

Upvotes: 2

Views: 108

Answers (1)

ygrek
ygrek

Reputation: 6697

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 ()

Upvotes: 3

Related Questions