Test for modifier keys correctly: CTK+ 3 Reference Manual

## Test for modifier keys correctly

Why. With `ctk_accelerator_get_default_mod_mask()` you can test for modifier keys reliably; this way your key event handlers will work correctly even if NumLock or CapsLock are activated.

In a CdkEventKey, the `state` field is a bit mask which indicates the modifier state at the time the key was pressed. Modifiers are keys like Control and NumLock. When implementing a “key-press-event” handler, you should use `ctk_accelerator_get_default_mod_mask()` to test against modifier keys. This function returns a bit mask which encompasses all the modifiers which the user may be actively pressing, such as Control, Shift, and Alt, but ignores "innocuous" modifiers such as NumLock and CapsLock.

Say you want to see if Control+F10 was pressed. Doing a simple test like `event->keysym == CDK_F10 && event->state == CDK_CONTROL_MASK` is not enough. If CapsLock is pressed, then `event->state` will be equal to `CDK_CONTROL_MASK | CDK_LOCK_MASK`, and the simple test will fail. By taking the logical-and of `event->state` and `ctk_accelerator_get_default_mod_mask()`, you can ignore the modifiers which are not actively pressed by the user at the same time as the base key.

The following example correctly tests for Control+F10 being pressed.

    static gboolean
    my_widget_key_press_event_handler (CtkWidget *widget, CdkEventKey *event)
    {
      CdkModifierType modifiers;

      modifiers = ctk_accelerator_get_default_mod_mask ();

      if (event->keysym == CDK_F10
          && (event->state & modifiers) == CDK_CONTROL_MASK)
        {
          g_print ("Control-F10 was pressed\n");
          return TRUE;
        }

      return FALSE;
    }