{"id":1864,"date":"2012-09-04T14:35:51","date_gmt":"2012-09-04T14:35:51","guid":{"rendered":"http:\/\/www.mjtnet.com\/blog\/?p=1864"},"modified":"2012-09-04T14:35:51","modified_gmt":"2012-09-04T14:35:51","slug":"custom-dialogs-how-to-make-a-keypress-alter-a-button-click-outcome","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2012\/09\/04\/custom-dialogs-how-to-make-a-keypress-alter-a-button-click-outcome\/","title":{"rendered":"Custom Dialogs: How to make a keypress alter a button click outcome"},"content":{"rendered":"<p>Today I was asked how the outcome of a button click on a custom dialog could be altered if the ALT or CTRL key was pressed down at the time of the click.<\/p>\n<p>This is possible by trapping the dialog&#8217;s OnKeyDown and OnKeyUp handlers.  Using these we can detect if ALT or CTRL is pressed\/released and set a flag accordingly.  Then in the button&#8217;s OnClick handler we can check the value of this flag and decide what we should do.<\/p>\n<p>Here&#8217;s some example code.  Run it and click the button without holding down any keys.  You&#8217;ll see a message saying that ALT was not pressed.  Now click the button while holding down the ALT key and you&#8217;ll see the &#8220;you clicked the button while ALT was pressed&#8221; message.  <\/p>\n<pre class=\"brush: macroscript\">Dialog>Dialog1\r\nobject Dialog1: TForm\r\n  Left = 467\r\n  Top = 241\r\n  HelpContext = 5000\r\n  BorderIcons = [biSystemMenu]\r\n  Caption = 'CustomDialog'\r\n  ClientHeight = 212\r\n  ClientWidth = 431\r\n  Color = clBtnFace\r\n  Font.Charset = DEFAULT_CHARSET\r\n  Font.Color = clWindowText\r\n  Font.Height = -11\r\n  Font.Name = 'MS Sans Serif'\r\n  Font.Style = []\r\n  KeyPreview = True\r\n  OldCreateOrder = True\r\n  ShowHint = True\r\n  OnTaskBar = False\r\n  PixelsPerInch = 96\r\n  TextHeight = 13\r\n  object MSButton1: tMSButton\r\n    Left = 147\r\n    Top = 47\r\n    Width = 75\r\n    Height = 25\r\n    Caption = 'MSButton1'\r\n    TabOrder = 0\r\n    DoBrowse = False\r\n    BrowseStyle = fbOpen\r\n  end\r\nend\r\nEndDialog>Dialog1\r\n\r\nAddDialogHandler>Dialog1,MSButton1,OnClick,doClick\r\nAddDialogHandler>Dialog1,,OnKeyDown,doKeyDown\r\nAddDialogHandler>Dialog1,,OnKeyUp,doKeyUp\r\n\r\nLet>CTRL_DOWN=FALSE\r\nLet>ALT_DOWN=FALSE\r\n\r\nShow>Dialog1,r\r\n\r\nSRT>doClick\r\n    \/\/button was clicked, which key is down\r\n    If>ALT_DOWN=TRUE\r\n      MessageModal>You clicked the button while ALT was pressed\r\n    Else\r\n      MessageModal>You clicked the button without pressing ALT\r\n    Endif\r\nEND>doClick\r\n\r\nSRT>doKeyDown\r\n    \/\/is CTRL key pressed\r\n    If>DoKeyDown_Key=17\r\n      Let>CTRL_DOWN=TRUE\r\n    Endif\r\n    \/\/is ALT key pressed\r\n    If>DoKeyDown_Key=18\r\n      Let>ALT_DOWN=TRUE\r\n    Endif\r\nEND>doKeyDown\r\n\r\nSRT>doKeyUp\r\n    \/\/was ctrl released\r\n    If>doKeyUp_Key=17\r\n      Let>CTRL_DOWN=FALSE\r\n    Endif\r\n    \/\/was ALT released\r\n    If>doKeyUp_Key=18\r\n      Let>ALT_DOWN=FALSE\r\n    Endif\r\nEND>doKeyUp<\/pre>\n<p>You might be wondering how I know that the CTRL key is number 17 and the ALT key is 18.  To find out the value of a key press the simplest way is just to pop a breakpoint at the start of the doKeyDown subroutine, then run the script in the debugger, press a key and look in the watch list to see what value is produced.  Alternatively there&#8217;s a <a href=\"http:\/\/www.mjtnet.com\/vkcodes.htm\">table here<\/a>.  Note that ALT is known as VK_MENU and CTRL is VK_CONTROL.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I was asked how the outcome of a button click on a custom dialog could be altered if the ALT or CTRL key was pressed down at the time of the click. This is possible by trapping the dialog&#8217;s OnKeyDown and OnKeyUp handlers. Using these we can detect if ALT or CTRL is pressed\/released [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1864"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=1864"}],"version-history":[{"count":7,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1864\/revisions"}],"predecessor-version":[{"id":1871,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1864\/revisions\/1871"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=1864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=1864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=1864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}