Frage

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:

  1. Write and bind both keypress and key-release handlers
  2. 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)
  3. 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.

War es hilfreich?

Lösung

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.

Andere Tipps

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.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top