Willicious 8 Posted May 3, 2023 (edited) I have a form with a treeview, which can be browsed using the arrow keys. Once an item in the treeview is selected, hitting an "OK" button on the form (which has the modal result mrOK) will close the form, and load the currently selected item into the app. What I want to do is assign VK_RETURN (which I believe is the [Enter] key...?) to the OK button, so that when browsing the treeview using keys, the user can just hit Enter/Return to load the currently selected item. btnOKClick has its own procedure, detailed here: procedure TFLevelSelect.btnOKClick(Sender: TObject); begin WriteToParams; ModalResult := mrOk; end; I can enter "WriteToParams;" into the Treeview's OnKeyDown procedure, but this doesn't actually have any effect until the OK button is pressed. So, one option is to tell the OnKeyDown procedure to also "virtually press the OK button" (which I'm not sure is even possible...?) - this would be my preferred method, if it can be done. Another option is to assign a hotkey to btnOKClick. The only problem with this is that there are multiple other buttons on the form, all of which already respond to [Enter] when Tab-selected. If I assign [Enter] as a hotkey to btnOKClick, then [Enter] either can't then be used for any of the other buttons on the form - or, worse, the app will crash because it won't know which button to press if the Tab is on a different button than 'OK', and the user hits [Enter]. Any and all suggestions/help welcome. I apologise if I haven't provided any necessary information. Edited May 3, 2023 by Willicious Share this post Link to post
programmerdelphi2k 237 Posted May 4, 2023 (edited) maybe some like this help you? same that your Edit/Memo-controls is focused, this works! // a easy way... procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := 'message button1'; end; procedure TForm1.Button2Click(Sender: TObject); begin Label1.Caption := 'message button2'; end; procedure TForm1.FormCreate(Sender: TObject); begin Form1.KeyPreview := true; // <---- to OnKeyDown,etc... end; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = vk_return { and other verifications... } then Button1.Click; end; Edited May 4, 2023 by programmerdelphi2k 2 Share this post Link to post
Willicious 8 Posted May 4, 2023 (edited) Brilliant, thank you @programmerdelphi2k! This one worked: procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = vk_return { and other verifications... } then Button1.Click; end; And now, I know how to virtually click buttons, which is incredibly helpful 🙂👍 Edited May 4, 2023 by Willicious 1 Share this post Link to post
programmerdelphi2k 237 Posted May 4, 2023 (edited) you can call the event as: ComponentXXXX.EventNameXXXX( ... params ) -> like do the Form definitions! Button1.OnClick(Self); Edited May 4, 2023 by programmerdelphi2k 1 Share this post Link to post
PeterBelow 238 Posted May 4, 2023 14 hours ago, Willicious said: I have a form with a treeview, which can be browsed using the arrow keys. Once an item in the treeview is selected, hitting an "OK" button on the form (which has the modal result mrOK) will close the form, and load the currently selected item into the app. What I want to do is assign VK_RETURN (which I believe is the [Enter] key...?) to the OK button, so that when browsing the treeview using keys, the user can just hit Enter/Return to load the currently selected item. Just set the buttons Default property to true, it will then automatically "click" when the user presses Enter/Return and the active control does not handle the key itself. No code needed... 2 Share this post Link to post
Remy Lebeau 1392 Posted May 4, 2023 5 hours ago, PeterBelow said: Just set the buttons Default property to true, it will then automatically "click" when the user presses Enter/Return and the active control does not handle the key itself. No code needed... 1 Share this post Link to post
programmerdelphi2k 237 Posted May 4, 2023 (edited) 4 minutes ago, Remy Lebeau said: 4 minutes ago, Remy Lebeau said: Hey, Ho.... Let's Go! Hey! Ho... dont let it go! Edited May 4, 2023 by programmerdelphi2k Share this post Link to post