Re: Getting keyboard state.



Philip Kendall wrote:

On Fri, Jan 12, 2001 at 01:06:24PM +0700, Maxim Koshelev wrote:

Hi there!
Does somebody know how to get state of the keyboard via gtk/gdk?
I just need to know about current state of Shift key in my program.


Connect handlers to both the `key-press-event' and `key-release-event'
for the appropriate widgets. (`key-release-event' isn't received by
default, so you'll need a call along the lines of

gtk_widget_add_events( widget, GDK_KEY_RELEASE_MASK );

to make sure you get them).

Then every time you get a key press event with event->key.keyval being
either `GDK_Shift_L' or `GDK_Shift_R', set a flag. Clear the flag on the
corresponding key release events (making sure you deal properly with a
sequence like Shift_L pressed, Shift_R pressed, Shift_R released).

Or if you only need shift when combined with other keys, just look at
bit 0 of event->key.state.

Rather than creating handlers and maintaining state for this, you can get the information along with the pointer position from anywhere in your code:

int x, y;
GdkModifierType modifiers;
gdk_window_get_pointer(window, &x, &y, &modifiers);

... the modifier keys will be OR-ed together GdkModifierType values.

Regards,
Timothy M. Shead
tshead k-3d com





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