When you get the key press event the event structure (GdkEventKey) has a member called state
that tells you which modifiers (including mouse buttons) are currently pressed. You will need to mask the state with the ruby version of gtk_accelerator_get_default_mod_mask
to mask out caps-lock and num-lock etc.
Easy way to detect modifier keys (e.g. Ctrl-P) in GTK / Glade in Ruby?
質問
I'm looking for a way to detect key combinations, such as the Ctrl+P sequence (hold down right or left Ctrl, hit the P key), from within key_press_event
handlers for a widget. There doesn't seem to be any easy clean way to tell whether a modifier key is held down.
The clunky way is:
- Write and bind both
keypress
andkey-release
handlers - Store press and release of the modifier keys I care about, to know when they're held down (right and left Ctrl, etc., keys are different)
- Detect press of letter-keys I care about, and treat them differently depending on which modifier keys are held down.
AccelGroup (see answer below) seems to work for most purposes, but they're per-window, not per-widget.
解決
他のヒント
Accelerator groups appear to do most of what I was looking for. Ruby documentation is in http://ruby-gnome2.sourceforge.jp/ja/hiki.cgi?Gtk%3A%3AAccelGroup.
It appears that an AccelGroup can bind key-combinations to a block or closure, and the AccelGroup is then added to a window.
Hat tip and +1 to "another.anon.coward."
(However, they are per-window, not per-widget.)