{"id":1315,"date":"2013-10-07T19:41:14","date_gmt":"2013-10-07T19:41:14","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1315"},"modified":"2015-10-29T21:31:49","modified_gmt":"2015-10-29T21:31:49","slug":"keypresses","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/gui-programming-in-c\/gtk\/keyboard\/keypresses","title":{"rendered":"Keypresses"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<h4>\nDetecting Key Presses<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#include &lt;gtk\/gtk.h&gt;\r\n#include &lt;gdk\/gdkkeysyms.h&gt;\r\n<\/code><\/pre>\n<h5>\nSetup&nbsp;the callback<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/Get keypresses\r\n\tg_signal_connect (G_OBJECT(MainWindow), &quot;key_press_event&quot;, G_CALLBACK (on_key_press), NULL);\r\n<\/code><\/pre>\n<h5>\nCreate A Function To Be Called<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\/\/******************************\r\n\/\/******************************\r\n\/\/********** KEYPRESS **********\r\n\/\/******************************\r\n\/\/******************************\r\ngboolean on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data)\r\n{\r\n\t\r\n\tif (event-&gt;state &amp; GDK_SHIFT_MASK)\r\n\t{\r\n\t\tprintf(&quot;SHIFT pressed\\n&quot;);\r\n\t}\r\n\telse if (event-&gt;state &amp; GDK_CONTROL_MASK)\r\n\t{\r\n\t\tprintf(&quot;CTRL pressed\\n&quot;);\r\n\t}\r\n\t\r\n\t\r\n\tswitch (event-&gt;keyval)\t\t\t\/\/See the &lt;gdk\/gdkkeysyms.h&gt; header file for a complete list of GDK key codes\r\n\t{\r\n\tcase &#39;q&#39;:\r\n\t\tprintf(&quot;key pressed: q\\n&quot;);\r\n\t\tbreak;\r\n\tcase &#39;Q&#39;:\r\n\t\tprintf(&quot;key pressed: Q\\n&quot;);\r\n\t\tbreak; \r\n\tcase GDK_KEY_Up:\r\n\t\tprintf(&quot;key pressed: Up\\n&quot;);\r\n\t\tbreak;\r\n\tcase GDK_KEY_Down:\r\n\t\tprintf(&quot;key pressed: Down\\n&quot;);\r\n\t\tbreak;\r\n\tcase GDK_KEY_Left:\r\n\t\tprintf(&quot;key pressed: Left\\n&quot;);\r\n\t\tbreak;\r\n\tcase GDK_KEY_Right:\r\n\t\tprintf(&quot;key pressed: Right\\n&quot;);\r\n\t\tbreak;\r\n\t}\r\n\r\n\treturn FALSE; \r\n}\r\n\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Detecting Key Presses #include &lt;gtk\/gtk.h&gt; #include &lt;gdk\/gdkkeysyms.h&gt; Setup&nbsp;the callback \/\/Get keypresses g_signal_connect (G_OBJECT(MainWindow), &quot;key_press_event&quot;, G_CALLBACK (on_key_press), NULL); Create A Function To Be Called \/\/****************************** \/\/****************************** \/\/********** KEYPRESS ********** \/\/****************************** \/\/****************************** gboolean on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer user_data) { if (event-&gt;state &amp; GDK_SHIFT_MASK) { printf(&quot;SHIFT pressed\\n&quot;); } else if (event-&gt;state &amp; GDK_CONTROL_MASK) { printf(&quot;CTRL [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-1315","post","type-post","status-publish","format-standard","hentry","category-keyboard"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1315"}],"version-history":[{"count":3,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":1718,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1315\/revisions\/1718"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}