cookbook: Fix key press to examine modifers correctly

The simple key press example in the cookbook used a brittle
and incorrect switch statement to test modifier values. Instead,
use logical "&" of the state with the modifiers we're interested
in to check which keys were pressed.

http://bugzilla.clutter-project.org/show_bug.cgi?id=2223
This commit is contained in:
Elliot Smith 2010-07-19 12:52:00 +01:00 committed by Emmanuele Bassi
parent 5865d2a4b3
commit e45b8be71b

View File

@ -102,31 +102,26 @@ g_signal_connect (actor, "key-press-event", G_CALLBACK (_key_press_cb), NULL);
<informalexample> <informalexample>
<programlisting> <programlisting>
<![CDATA[
static gboolean static gboolean
_key_press_cb (ClutterActor *actor, _key_press_cb (ClutterActor *actor,
ClutterEvent *event, ClutterEvent *event,
gpointer user_data) gpointer user_data)
{ {
guint keyval = clutter_event_get_key_symbol (event); guint keyval = clutter_event_get_key_symbol (event);
ClutterModifierType state = clutter_event_get_state (event);
gboolean shift_pressed = (state & CLUTTER_SHIFT_MASK ? TRUE : FALSE);
gboolean ctrl_pressed = (state & CLUTTER_CONTROL_MASK ? TRUE : FALSE);
if (CLUTTER_Up == keyval) if (CLUTTER_Up == keyval)
{ {
ClutterModifierType modifiers = clutter_event_get_state (event); if (shift_pressed & ctrl_pressed)
g_debug ("Up and shift and control pressed");
switch (modifiers) else if (shift_pressed)
{ g_debug ("Up and shift pressed");
case CLUTTER_SHIFT_MASK: else
g_debug ("Up and shift pressed"); g_debug ("Up pressed");
break;
case CLUTTER_SHIFT_MASK + CLUTTER_CONTROL_MASK:
g_debug ("Up and shift and control pressed");
break;
default:
g_debug ("Up pressed");
break;
}
/* The event was handled, and the emission should stop */ /* The event was handled, and the emission should stop */
return TRUE; return TRUE;
@ -135,6 +130,7 @@ _key_press_cb (ClutterActor *actor,
/* The event was not handled, and the emission should continue */ /* The event was not handled, and the emission should continue */
return FALSE; return FALSE;
} }
]]>
</programlisting> </programlisting>
</informalexample> </informalexample>