gtk3/docs/reference/gtk/html/checklist-modifiers.html
2021-04-15 09:52:10 +01:00

88 lines
5.2 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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+ 3 Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="GTK+ 3 Reference Manual">
<link rel="up" href="gtk-migrating-checklist.html" title="Migration Details 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.1 (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="gtk3-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="gtk3-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="gtk3-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)
{
GdkModifierType 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.1</div>
</body>
</html>