﻿Introduction:

Whenever you press a key, an event is emitted. You can attach a function to handle such events.

To set up the keyboard events, you first have to call the add_events() function with a bit mask of the events you're dealing with.

The values for the argument can be looked up in:

	http://developer.gnome.org/gdk3/stable/gdk3-Events.html    ( search for GdkEventMask )

	Then you just need to change the namespace part, like:
			
		GDK_KEY_PRESS_MASK to Gdk::KEY_PRESS_MASK
			
	and call:
			
		add_events( Gdk::KEY_PRESS_MASK );
			
			
	To add several, just use the bitwise OR operator, like:
			
			add_events( Gdk::KEY_PRESS_MASK | Gdk::BUTTON_PRESS_MASK );


You need to connect the event to a function handler, just like you would do with any event.

	for example, in a class constructor:
		
		theObject.signal_key_press_event().connect( sigc::mem_fun( *this, &MyClass::onKeyPress ) );
		

The function will receive an argument, depending on the type of event we're dealing with.

	You can check all the possibilities here: (link -> http://developer.gnome.org/gdk3/stable/gdk3-Event-Structures.html)

It has to return a bool value, to allow (return false) or not (return true) event propagation.



In the case of keyboard events, you'll get a GdkEventKey, where you see which key was pressed, etc.

	Something like:
		bool MyClass::onKeyPress( GdkEventKey *event );

To determine which key was pressed/released, you read the value of keyval, and compare it with a macro in the <gdk/gdkkeysyms.h> header file (look in the gtk3 folder).



If you want to see if there's any modifier key been pressed as-well, you have the state property, which has bit-masks for you to check.
	(link: http://developer.gnome.org/gdk3/stable/gdk3-Windows.html#GdkModifierType

So for example, if you want to check if the alt key is been pressed (and only the alt key), you can compare it with GDK_MOD1_MASK.

	if ( some_key && event->state == GDK_MOD1_MASK ) ... 
	
 but if you want to check for ctrl + alt, then you use the bitwise OR operator, 
 
	if ( some_key && event->state == (GDK_MOD1_MASK | GDK_CONTROL_MASK) ) ...

If you want to know if there's a modifier key been press, but don't care if others might be pressed as-well, then use the bitwise AND operator, for example:

	if ( some_key && (event->state & GDK_CONTROL_MASK) ) ...





Example:

In this example, there's three keyboard shortcuts that are set, alt + 1 to select the first radio button, alt + 2 selects the second, and pressing the ESC key hides(closes) the window.

