0

Consider:

require(gWidgets2)
w <- gwindow("notebook example", visible=T)
nb <- gnotebook(container=w)
gbutton("Page one", label="tab 1", container=nb) ## note label argument
gbutton("Page two", label="tab 2", container=nb)

How can I bind a given key (say, ESC) to close the gwindow() in gWidgets, that is to execute dispose(w)? In other words, how do you assign keybindings in gWidgets?

landroni
  • 2,902
  • 1
  • 32
  • 39

2 Answers2

1

With RGtk2 (and possibly others) the addHandlerKeystroke method can be used to catch keystrokes. You have to dig into the h object to capture the ESC key. There isn't any portable code for that, but the Gtk docs should be able to help.

jverzani
  • 5,600
  • 2
  • 21
  • 17
  • Using these instructions I managed to come up with a solution. See separate answer. – landroni Jul 12 '14 at 20:04
  • One question, though: How can I capture the `F4` key? If I use this method and hit `F4`, then `h$key==""`, so that obviously doesn't seem captured.. – landroni Jul 12 '14 at 20:31
0

As per the accepted answer, I had to:

addHandlerKeystroke(w, function(h, ...){
    browser()
})

Then bring up the w window and hit ESC, then in the browser() terminal:

print(h)

And notice that:

Browse[1]> h$key
[1] "\033"

Then the following handler does what I want:

h_esc <- addHandlerKeystroke(w, function(h, ...){
    if(h$key=="\033") dispose(w)
})

As per how to program window to close with escape key and How to define ESC char in git?, it seems that ESC is often captured as \033.

Community
  • 1
  • 1
landroni
  • 2,902
  • 1
  • 32
  • 39
  • That is likely defined within an enumeration, but you have to dig into the Gtk docs to find out what it is called. – jverzani Jul 14 '14 at 02:01
  • Thanks, but I now found an issue with the solution for `ESC`: it turns out that `gWidgets2` captures *all* `ESC` instances. So for example if I right-click on the Window Manager title to get the WM options, then hit `ESC`, `gWidgets2` will unexpectedly activate the `addHandlerKeystroke` handler. Instead I would have expected the `Gtk` menu to close but for the window to stay open; I would have expected that only a *second* `ESC` would close the window. Am I getting it wrong? – landroni Jul 14 '14 at 06:25
  • 1
    I don't know how to filter those events out, perhaps there is something in the event information. As is, the event handler is bound to the window widget and is called when that widget has focus and the esc key is pressed. – jverzani Jul 14 '14 at 11:51
  • Hmm, can gWidgets2 detect that the widget no longer has focus? When the WM context-menu is popped out, or the Gtk completion pop-up (in a `gedit` widget, say) is displayed, then `w` effectively loses focus: scrolling in the main window works not until the pop-up disappears, and *any* mouse click anywhere in the `w` window will as a first effect dispose of that context-menu pop-up. So can gWidgets2 detect when it loses focus to some pop-up window? – landroni Jul 14 '14 at 14:08
  • Maybe `addHandlerBlur` can be used here? – jverzani Jul 14 '14 at 15:52
  • Can a handler be disabled temporarily? I tried `addHandlerBlur(w, function(h, ...){enabled(h_esc) <- FALSE})`, but this doesn't seem to have any effect. – landroni Jul 14 '14 at 17:20
  • there are `blockHandler` and `unblockHandler` methods. Good luck. – jverzani Jul 15 '14 at 01:29
  • Hmm, I'm confused as to how to use ` blockHandler(obj, ID, ...)`, especially the `ID` part (the docs aren't straightforward). If I do `blockHandler(w, h_esc)` or `blockHandler(w, 'h_esc')`, then I get an error: "Error in id$signal : $ operator is invalid for atomic vectors". How do I determine the `ID` returned by the handler? – landroni Jul 15 '14 at 06:02
  • it is returned by `addHandler`. – jverzani Jul 15 '14 at 13:13
  • I'm sorry but I'm still confused. If I try `addHandlerBlur(w, function(h, ...){ print(addHandler(w)) })` then I simply get `NULL` as return value. I must be doing it wrongly. – landroni Jul 15 '14 at 16:46
  • Oh my, that's embarrassing. This is now fixed, but you need to update in both `gWidgets2` and `gWidgets2RGtk2`. – jverzani Jul 15 '14 at 20:45