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

﹥>﹥吖頭↗ 提交于 2019-12-13 02:56:16

问题


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.



来源:https://stackoverflow.com/questions/24711308/how-to-bind-esc-key-to-close-a-gwindow-in-gwidgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!