Re: How to grab key combination event in GDK?



On Sat, Feb 4, 2012 at 2:30 AM, victor-victor <nadaeck hotmail com> wrote:
>
> Hello
>
> I know how to grab a single key event with gdk, but I'd like to know how to
> grab a key combination  that is *not* using a modifier key. For example I
> would like to grab when 'GDK_KEY_a' and 'GDK_KEY_right' are pressed
> simultaneously. As far as I understand, the GdkEventKey structure allows
> only one key to be grabbed at a time (with or without a  modifier key).

(1) you cannot ever press keys simultaneously. the keyboard is a
physical device that is scanned at a given rate by the keyboard
electronics. one key will always be discovered to be down "first" even
if you could simultaneously depress them.

(2) given the above, you will get (in the case of a "simultaneous" key
press + release) the following events:

              key press
              key press
              <gap>
              key release
              key release

> In SDL, there is the function SDL_GetKeyState() that gets a snapshot of the
> keyboard state; it returns a pointer to an array; then every key can be
> checked if pressed or not. Is there something similar in GDK? If not how
> could I achieve this?

there is no similar function available for GTK.

look into gdk event filters for gdk windows, which will allow you to
catch and handle every single key event no matter what else is done
with it. then use a hash map to store which keys are down, and a
simple api to search it or return the current "down set".

alternatively, use a state machine approach in a regular pair of key
press/release handlers. before the first press, you're in state
"zero". after the first press, you're in state "one". after the second
press, you're in state "two". then trigger things from the state
transitions rather than the key presses themselves.

--p


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]