{"id":765,"date":"2012-01-20T09:08:27","date_gmt":"2012-01-20T09:08:27","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=765"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"detecting-key-presses-on-a-form","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/keypresses\/detecting-key-presses-on-a-form","title":{"rendered":"Detecting Key Presses On A Form"},"content":{"rendered":"<p>Select the form:<\/p>\n<p style=\"padding-left: 30px;\">Set the KeyPreview property of the form to true so that keyboard messages are received by the form before they reach any controls on the form.<\/p>\n<p style=\"padding-left: 30px;\">Create a KeyPress event (detects keys down combined with any modifier key &#8211; i.e. no event for pressing CTRL and a modified key value for CTRL + &#8216;y&#8217;<\/p>\n<p style=\"padding-left: 30px;\">Or create a KeyDown event (detects every key down including modifier keys and gives individual key value)<\/p>\n<h4>Example Handler<\/h4>\n<pre><code>\r\n\t\/\/***********************************\r\n\t\/\/***********************************\r\n\t\/\/********** FORM KEY DOWN **********\r\n\t\/\/***********************************\r\n\t\/\/***********************************\r\n\tprivate: System::Void frmMain_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)\r\n\t{\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\t\/\/MessageBox::Show(\"Form.KeyPress: '\" + e-&gt;KeyValue.ToString() + \"' pressed.\");\r\n\r\n\t\t\tif ((Control::ModifierKeys == Keys::Control) &amp;&amp; (e-&gt;KeyValue == 'L'))\t\t\/\/Key values are for CAPS version of letters\r\n\t\t\t{\r\n\r\n\t\t\t\t\/\/----- CTRL+L PRESSED -----\r\n\t\t\t\ttextBox1-&gt;Text = \"A\";\r\n\r\n\t\t\t}\r\n\t\t}\r\n\t\tcatch (Exception ^)\r\n\t\t{\r\n\t\t}\r\n\t}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Select the form: Set the KeyPreview property of the form to true so that keyboard messages are received by the form before they reach any controls on the form. Create a KeyPress event (detects keys down combined with any modifier key &#8211; i.e. no event for pressing CTRL and a modified key value for CTRL [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-765","post","type-post","status-publish","format-standard","hentry","category-keypresses"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=765"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":1027,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/765\/revisions\/1027"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}