How to bind ESC key to close a `gwindow()` in gWid

2019-09-07 12:23发布

问题:

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?

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



回答2:

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.