Search code examples
delphidelphi-7

Perform a button click on key press on keyboard


I want to click button in my app when i press a key on keyboard. It may be probably done by using If function. I'm not sure which variable equals to key press monitoring.


Solution

  • Using action lists

    You might find the following example illuminating:

    1. Create a new VCL application.

    2. Drop a TActionList on the form.

    3. Double-click the TActionList and create a new action. Set its Name to aGreet, its ShortCut to F1, and its Caption to Greet!.

    4. Double-click the action in the list box to create its OnExecute handler.

    5. Add the following code to the handler: ShowMessage('Welcome!')

    6. Go back to the visual form editor and drop a TButton on the form.

    7. Set its Action property to aGreet.

    8. Run the application.

    Now, as you can see, pressing F1 has the same effect as clicking the button.

    Welcome to the wonderful world of action lists!

    Using only the accelerator

    The above example assumes that you want to use a hotkey for your button. But you should also be aware of accelerator keys.

    In Microsoft Windows, for decades, the norm is that all buttons, except OK and Cancel have accelerator keys. For instance, the button's caption might be &Save, which means that the S is underlined and pressing Alt+S will "click" the button.

    If this is all you need, you don't need to use actions (although actions are good for many other reasons) and you don't need to write a single line of code. Just add an ampersand character to the button's caption!

    In a well-designed Win32 GUI, all buttons, except default (OK) and cancel (Cancel) buttons, should have such accesskeys. Keyboard users like myself get really annoyed when they need to tab 27 times or use the computer's pointing device.

    Other approaches

    Of course, you can also create an OnKeyDown or OnKeyPress event handler for your form, see if the key is VK_F1 (for example) or S with Ctrl being depressed (another example), and if so, invoke the same code as is invoked by the button. But this is likely a less than ideal approach, and it gets slightly more involved if the form has windowed child controls (hint: have a look at the KeyPreview property).

    Just some examples:

    procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
    
      case Key of
        VK_F1:                                                       // F1
          ShowMessage('You want help?');
        VK_F2:                                                       // F2
          ShowMessage('You want to rename something?');
      end;
    
      if (Key = Ord('S')) and (ssCtrl in Shift) then                 // Ctrl + S
        ShowMessage('Time to save?');
    
    end;
    
    procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
    begin
      case Key of
        ^A:                                                          // Ctrl + A
          ShowMessage('Select all!');
        'p':                                                         // P
          ShowMessage('A pause, perhaps?');
      end;
    end;