Jump to content
Willicious

Assign KeyDown function to btnClick

Recommended Posts

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 by Willicious

Share this post


Link to post

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;

image.png.8aa3d32e3a70cd9e534007fef9e0a196.png

Edited by programmerdelphi2k
  • Thanks 2

Share this post


Link to post

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 by Willicious
  • Thanks 1

Share this post


Link to post

you can call the event as:   ComponentXXXX.EventNameXXXX( ... params ) -> like do the Form definitions!

  •     Button1.OnClick(Self);

image.thumb.png.1c465e887ff5bac5d29a1567fb19f6e8.png

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post
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...

  • Like 2

Share this post


Link to post
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...

 

ECE452F7-82D1-4FF0-BA23-2C748E271529.gif

  • Like 1

Share this post


Link to post
4 minutes ago, Remy Lebeau said:

ECE452F7-82D1-4FF0-BA23-2C748E271529.gif

     

4 minutes ago, Remy Lebeau said:

ECE452F7-82D1-4FF0-BA23-2C748E271529.gif    Hey, Ho.... Let's Go!  Hey! Ho... dont let it go! :classic_biggrin:

 

 

Edited by programmerdelphi2k

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×