http://stackoverflow.com/questions/10134956/in-simple-gtk-key-press-event-example-gdk-shift-mask-seems-to-be-ignored
Could someone please compile and execute the small sample code I provided below? Please let me know if the shift key modifier works properly for you. This example is suppose to demonstrate the key press functionality in gtk. It works fine for simple key presses and even works with the control key modifier, but it does not work with the shift key modifier.
/*
*
* compile command:
*
* gcc keypress3.c -o keypress3 `pkg-config --libs --cflags gtk+-2.0`
*
*/
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
gboolean
on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data);
int main (int argc, char *argv[])
{
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (on_key_press), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
gboolean
on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
switch (event->keyval)
{
case GDK_p:
printf("key pressed: %s\n", "p");
break;
case GDK_s:
if (event->state & GDK_SHIFT_MASK)
{
printf("key pressed: %s\n", "shift + s");
}
else if (event->state & GDK_CONTROL_MASK)
{
printf("key pressed: %s\n", "ctrl + s");
}
else
{
printf("key pressed: %s\n", "s");
}
break;
case GDK_m:
if (event->state & GDK_SHIFT_MASK)
{
printf("key pressed: %s\n", "shift + m");
}
else if (event->state & GDK_CONTROL_MASK)
{
printf("key pressed: %s\n", "ctrl + m");
}
else
{
printf("key pressed: %s\n", "m");
}
break;
default:
return FALSE;
}
return FALSE;
}
The output I am getting:
key pressed: m
key pressed: ctrl + m
key pressed: p
key pressed: ctrl + s
key pressed: s
I get nothing when I press shift + s or shift + m, so it seems that I am not quite getting how the GDK_SHIFT_MASK should be used even though I have read the documentation and I have seen plenty of other examples where it appears to be used exactly the same way.
c gtk2
share|improve this question
edited Apr 15 '12 at 18:40
asked Apr 13 '12 at 3:43
nomadicME
484415
You are getting that output for what input, exactly? – ptomato Apr 13 '12 at 7:01
@ptomato The output describes the input exactly. – nomadicME Apr 13 '12 at 17:21
add a comment
1 Answer
activeoldestvotes
up vote
5
down vote
accepted
The value of event->keyval when shift+s is pressed is GDK_S, not GDK_s. In other words, GDK has already interpreted the keyboard for you, giving you the symbol 'S', not 's'. The shift mask is still set, though. You can see this by adding a case for GDK_S:
...
case GDK_S: // add this line
case GDK_s:
if (event->state & GDK_SHIFT_MASK)
{
printf("key pressed: %s\n", "shift + s");
}
else if (event->state & GDK_CONTROL_MASK)
{
....