File: checklist-modifiers.html

package info (click to toggle)
gtk%2B2.0 2.24.33-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie, trixie-proposed-updates, trixie-updates
  • size: 124,860 kB
  • sloc: ansic: 574,091; makefile: 5,171; sh: 4,618; xml: 1,193; python: 1,117; perl: 749; awk: 49; cpp: 34
file content (88 lines) | stat: -rw-r--r-- 5,257 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test for modifier keys correctly: GTK+ 2 Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="GTK+ 2 Reference Manual">
<link rel="up" href="gtk-migrating-checklist.html" title="Migration Checklist">
<link rel="prev" href="checklist-gdkeventexpose-region.html" title="Use GdkEventExpose.region">
<link rel="next" href="checklist-named-icons.html" title="Use named icons">
<meta name="generator" content="GTK-Doc V1.33.0 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="gtk-migrating-checklist.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="checklist-gdkeventexpose-region.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="checklist-named-icons.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="checklist-modifiers"></a>Test for modifier keys correctly</h2></div></div></div>
<p><b>Why. </b>
	With <a class="link" href="gtk2-Keyboard-Accelerators.html#gtk-accelerator-get-default-mod-mask" title="gtk_accelerator_get_default_mod_mask ()"><code class="function">gtk_accelerator_get_default_mod_mask()</code></a> you can test for 
        modifier keys reliably; this way your key event handlers will 
        work correctly even if <span class="keycap"><strong>NumLock</strong></span> or 
        <span class="keycap"><strong>CapsLock</strong></span> are activated.
      </p>
<p>
      In a <span class="structname">GdkEventKey</span>, the
      <em class="structfield"><code>state</code></em> field is a bit mask which
      indicates the modifier state at the time the key was pressed.
      Modifiers are keys like <span class="keycap"><strong>Control</strong></span> and
      <span class="keycap"><strong>NumLock</strong></span>.  When implementing a 
      <a class="link" href="GtkWidget.html#GtkWidget-key-press-event" title="The “key-press-event” signal"><span class="type">“key-press-event”</span></a> handler, you should use 
      <a class="link" href="gtk2-Keyboard-Accelerators.html#gtk-accelerator-get-default-mod-mask" title="gtk_accelerator_get_default_mod_mask ()"><code class="function">gtk_accelerator_get_default_mod_mask()</code></a> 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 <span class="keycap"><strong>Control</strong></span>,
      <span class="keycap"><strong>Shift</strong></span>, and <span class="keycap"><strong>Alt</strong></span>, but ignores
      "innocuous" modifiers such as <span class="keycap"><strong>NumLock</strong></span> and
      <span class="keycap"><strong>CapsLock</strong></span>.
    </p>
<p>
      Say you want to see if
      <span class="keycap"><strong>Control</strong></span>+<span class="keycap"><strong>F10</strong></span>
      was pressed.  Doing a simple test like
      <code class="literal">event-&gt;keysym == GDK_F10 &amp;&amp;
      event-&gt;state == GDK_CONTROL_MASK</code> is not
      enough.  If <span class="keycap"><strong>CapsLock</strong></span> is pressed, then
      <em class="structfield"><code>event-&gt;state</code></em> will be equal to
      <code class="literal">GDK_CONTROL_MASK | GDK_LOCK_MASK</code>, and the
      simple test will fail.  By taking the logical-and of
      <em class="structfield"><code>event-&gt;state</code></em> and
      <a class="link" href="gtk2-Keyboard-Accelerators.html#gtk-accelerator-get-default-mod-mask" title="gtk_accelerator_get_default_mod_mask ()"><code class="function">gtk_accelerator_get_default_mod_mask()</code></a>, you
      can ignore the modifiers which are not actively pressed by the
      user at the same time as the base key.
    </p>
<p>
      The following example correctly tests for
      <span class="keycap"><strong>Control</strong></span>+<span class="keycap"><strong>F10</strong></span>
      being pressed.
    </p>
<a name="default-mod-mask"></a><pre class="programlisting">
static gboolean
my_widget_key_press_event_handler (GtkWidget *widget, GdkEventKey *event)
{
  guint modifiers;

  modifiers = gtk_accelerator_get_default_mod_mask ();

  if (event-&gt;keysym == GDK_F10
      &amp;&amp; (event-&gt;state &amp; modifiers) == GDK_CONTROL_MASK)
    {
      g_print ("Control-F10 was pressed\n");
      return TRUE;
    }

  return FALSE;
}
    </pre>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.33.0</div>
</body>
</html>