Detecting Key Presses


#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
Setup the callback

	//Get keypresses
	g_signal_connect (G_OBJECT(MainWindow), "key_press_event", G_CALLBACK (on_key_press), NULL);
Create A Function To Be Called

//******************************
//******************************
//********** KEYPRESS **********
//******************************
//******************************
gboolean on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
	
	if (event->state & GDK_SHIFT_MASK)
	{
		printf("SHIFT pressed\n");
	}
	else if (event->state & GDK_CONTROL_MASK)
	{
		printf("CTRL pressed\n");
	}
	
	
	switch (event->keyval)			//See the <gdk/gdkkeysyms.h> header file for a complete list of GDK key codes
	{
	case 'q':
		printf("key pressed: q\n");
		break;
	case 'Q':
		printf("key pressed: Q\n");
		break; 
	case GDK_KEY_Up:
		printf("key pressed: Up\n");
		break;
	case GDK_KEY_Down:
		printf("key pressed: Down\n");
		break;
	case GDK_KEY_Left:
		printf("key pressed: Left\n");
		break;
	case GDK_KEY_Right:
		printf("key pressed: Right\n");
		break;
	}

	return FALSE; 
}

 

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *