Hot Key assigned to Button lazarus / 31-LCL ---------------------------------------------------------------------- Elmug · 2011-07-21 10:37 I found this past thread http://www.lazarus.freepascal.org/index.php?action=post;quote=44476;to pic=9128.0 on how to assign a Hot Key to a button, using the buttons event OnKeyDown. I read one adds to Uses, the library LCLType. No problem there. But can someone add a little more, like please describe in words, or small code sample, how to do it? Specially on this: if key = 112 then go; I get 112 is the key char code, but  Is "go" a reserved word? Thanks! ---------------------------------------------------------------------- felipemdc · 2011-07-21 11:58 That example doesn't really assign a hot key to the button, it will handle a OnKeyDown event somewhere and react to it. You can put this event handler in the form, for example, but I'm not sure it will be really global, I think it will only be handled when the form itself has focus, but not when other controls have focus. So you would need to assign this handler to all focusable controls. And the code should be something like this: Code: [Select]uses LCLType; procedure TMyForm.HandleKeyDown(...); begin   if key = VK_F12 then HandleButtonXClick(Self); end; go is not a key word, he only ment that you should substitute go with something. The correct way to do this should be adding a & to the caption of the button. For example: &Button1 Then by hitting Ctrl+B this button will be called, regardless of which control of the form has focus. ---------------------------------------------------------------------- Elmug · 2011-07-21 19:13 Quote from: felipemdc on July 21, 2011, 01:58:11 pmThat example doesn't really assign a hot key to the button, it will handle a OnKeyDown event somewhere and react to it. You can put this event handler in the form, for example, but I'm not sure it will be really global, I think it will only be handled when the form itself has focus, but not when other controls have focus. So you would need to assign this handler to all focusable controls. And the code should be something like this: Code: [Select]uses LCLType; procedure TMyForm.HandleKeyDown(...); begin   if key = VK_F12 then HandleButtonXClick(Self); end; go is not a key word, he only ment that you should substitute go with something. The correct way to do this should be adding a & to the caption of the button. For example: &Button1 Then by hitting Ctrl+B this button will be called, regardless of which control of the form has focus. Thanks Felipe, good explanation and clear (i believe) on all aspects. As to using the "&" to the caption, I tried it before and did not seem to work; on top of that I read at the web that that is only OK in Delphi. However I'will try it again. Now, if I get it to work, where would be formal documentation on this use of the "&"?...Offhand, for instance, I am wondering if  the "Enter" key can be used? Or what keys can be so used?  Or, even, what would happen if the same key is assigned with the "&" to more than one button? Thanks for the great help. ---------------------------------------------------------------------- Elmug · 2011-07-21 19:37 Hi again, I tried the "&" option and it DOES WORK in Lazarus. My problem in the past when I tried it and thought it did not work is that I misinterpreted that the letter following the "&" would be the assigned letter, but it is actually an assignement to Alt+"assigned letter" And, curious me, I just now even tried caption with TWO "&" to see if there'd be a double assignment, but no.  The only quirk is that the assignment is to the FIRST ampersand BUT the underlining is done to the SECOND letter. So the caption underlines incorrectly, in that sense, if I put TWO ampersands, which roots back to silly me (but could fool others). Another quirk I just found  (Windows XP) is that the underlining of the letter assigned, in any case, does NOT SHOW when I initially run the program, either by the green run arrow, or by the .exe file. As soon as I press Alt key, though, the underlining appears. Seems like a little bug? Thanks. Thanks. ---------------------------------------------------------------------- Scoops · 2011-07-21 20:03 Hi, QuoteAnother quirk I just found  (Windows XP) is that the underlining of the letter assigned, in any case, does NOT SHOW when I initially run the program, either by the green run arrow, or by the .exe file. As soon as I press Alt key, though, the underlining appears. Seems like a little bug? By default Windows (XP) shows the underlined hotkeys only, when ALT is pressed. This can be changed in display-properties in the subdialog "Effects" so, that the hotkeys are always underlined. ---------------------------------------------------------------------- Blaazen · 2011-07-21 20:04 QuoteAnother quirk I just found  (Windows XP) is that the underlining of the letter assigned, in any case, does NOT SHOW when I initially run the program, either by the green run arrow, or by the .exe file. As soon as I press Alt key, though, the underlining appears. Seems like a little bug? AFAIK It is not a bug. It is feature of some Windows themes. Another feature, two ampersands, is also good, when you need to have caption like: 'Save & Exit' then you must write '&Save && Exit' and your caption is not missinterpreted as 'Save   Exit' When you put more ampersand letter to one caption: '&Save and &Exit' then only first is valid as accel. character. When you have more buttons with the same acceleration character '&Button1' and '&Button2' then Alt+B only swap focus between those buttons and never press. ---------------------------------------------------------------------- Elmug · 2011-07-22 03:43 Thank you scoops and Blazeen. I am happy to hear it is not a bug, and also to know that there is an option to show the underlying at start up!