Sinthet
Sinthet

Reputation: 893

Detect which key is pressed in C

I'm trying to find a way to find out which key is pressed down in C. This will be in a graphical environment, written in GTK2, but I don't think the answer lies there. I think I might be able to do this using Xlib, but I haven't been able to find anything conclusive on this.

Does anyone have any suggestions on how to do this?

I've managed to catch a keypress using the follow code:

GtkWidget *window;
void gtk_widget_set_events(window,GDK_KEY_RELEASE_MASK);
g_signal_connect(window,"key_release_event",G_CALLBACK(hello),NULL);

However, I would like to identify which key is pressed. From the link posted by Aditya Kumar, I know the answer lies with using GdkEventKey, since it is a structure which has a keyval field, but I cannot seem to get the syntax right. What is the correct way of getting this number?

This is a method I've tried:

static void hello( GtkWidget *widget,
               guint   data ){

g_print ("Hello World, %d was pressed\n",data);}

I tried supplying "data" by doing this when I catch the key_release_event:

g_signal_connect(window,"key_release_event",G_CALLBACK(hello),GdkEventKey.keyval);

However, I get a compiler error like so:

hello.c:85:5: error: expected ‘)’ before ‘.’ token 
hello.c:85:5: error: expected expression before ‘,’ token

Upvotes: 3

Views: 8550

Answers (3)

martin
martin

Reputation: 1

event->keyval is a pointer to a struct, where keyval contains a integer value for the key pressed, this has been used above in a function gdk_keyval_name (event->keyval) which gets a actual name for the key.

Upvotes: 0

Mark
Mark

Reputation: 108512

You are correct with your original syntax.

g_signal_connect(window, "key-release-event", G_CALLBACK(key_event), NULL);

Where the key_event function looks something like (note I am using the gdk_keyval_name to convert the keyval int value to a string for printing):

static gboolean
key_event(GtkWidget *widget,
          GdkEventKey *event)
{
    g_printerr("%s\n",
               gdk_keyval_name (event->keyval));
    return FALSE;
}

Here's a complete example program:

#include <gtk/gtk.h>

static gboolean
key_event(GtkWidget *widget,
          GdkEventKey *event)
{
    g_printerr("%s\n",
               gdk_keyval_name (event->keyval));
    return FALSE;
}

int main( int   argc,
          char *argv[] )
{

    GtkWidget *window;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    g_signal_connect(window, "key-release-event", G_CALLBACK(key_event), NULL);

    gtk_widget_show (window);

    gtk_main ();

    return 0;
}

Upvotes: 9

A. K.
A. K.

Reputation: 38116

while looking at the gdk reference manual i think you can capture the keyboard events using this unless you specifically want to have a 'C' program.

Here is the link to help you out.

http://www.gtk.org/api/2.6/gdk/gdk-Keyboard-Handling.html

Upvotes: 1

Related Questions