34

Using F# and Eto.Forms on Linux, with the Gtk3 backend.

EDIT: Adding updates to this google groups thread. My current best theory is that the way Eto adds keypress events to a widget in its Gtk backend does not let you capture events before a default handler both handles them and stops the signal from propagating further.


I'm trying to capture KeyDown events in a form:

let f = new Form(Topmost=true, ClientSize = new Size(600, 480))

f.KeyDown.Add(fun e ->
  match e.Key with
  | Keys.Up -> cursor.Move(Move_Up)
  // ...
  )

but I'm running into this issue: Up, Down, Left and Right arrow keys do not trigger KeyDown event

I cannot figure out how to follow the solution provided there (override PreviewKeyDown and set e.IsInputKey = true). I tried adding the following:

f.PreviewKeyDown.Add(fun e -> e.IsInputKey <- true)

but that just complained that f.PreviewKeyDown did not exist.


EDIT: It might be this Gtk#-specific issue rather than the one above

As of release 0.15, Gtk# started using the CONNECT_AFTER flag when connecting event handlers to signals. This means that the event handlers are not run until after the default signal handlers, which means that the widget will be updated when the event handlers run. A side effect of this change is that in the case where default handlers return true to stop signal propagation, Gtk# events will not be emitted.


Further detail: If I hold down a modifier key as well (e.g. shift + up arrow), then KeyDown triggers and e.Key does match Keys.Up. Also KeyUp always triggers, even when KeyDown does not.

bfontaine
  • 14,702
  • 12
  • 64
  • 87
Martin DeMello
  • 10,876
  • 6
  • 44
  • 64
  • 2
    To overrride you need to inherit from the form class. – John Palmer May 01 '16 at 10:45
  • didn't work either; looks like Eto.Forms just doesn't expose that event :( – Martin DeMello May 01 '16 at 11:00
  • It works fine with Eto.WPF backend. Which version are you having trouble with? – jpe May 09 '16 at 11:10
  • `PreviewKeyDown.Add` is available in a `System.Windows.Forms` form, but not in `Eto.Forms`. Is the problem persistent or do you lose arrow keys occasionallly? Which backend to Eto are you using (GTK2, GTK3, WPF, ...)? – jpe May 09 '16 at 12:39
  • using the gtk3 backend, and it's persistent - the arrow keys do not register unless there is a modifier held down as well. some other keys like space and tab don't register either, which makes me wonder if there are some default form navigation keys that are swallowed at the application level. – Martin DeMello May 09 '16 at 19:19
  • also the KeyUp event is triggered when i release an arrow key, even if KeyDown isn't when I press it. – Martin DeMello May 09 '16 at 19:20

1 Answers1

1

Try returning false in the event handler after you have handled the event.