Recent

Author Topic: Creating keyboard shortcuts  (Read 16085 times)

cybersmyth

  • Jr. Member
  • **
  • Posts: 51
Creating keyboard shortcuts
« on: April 05, 2011, 04:36:30 pm »
How do you go about creating a keyboard shortcut? I have a "Start" button, I'd like the user to be able to execute it with ctrl+s rather than just clicking on it.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Creating keyboard shortcuts
« Reply #1 on: April 05, 2011, 07:54:12 pm »
Hmm... TButton doesn't seem to have Shortcut property, but it has Action property where its instance has Shortcut property. So you have to start designing your apps correctly (read: remove redundant code), by forwarding all requests (menus, buttons, etc) through a TAction of TActionList.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Creating keyboard shortcuts
« Reply #2 on: April 05, 2011, 11:24:19 pm »
Acceleration keys are not enough?

Caption:='&Start';
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Creating keyboard shortcuts
« Reply #3 on: April 05, 2011, 11:43:40 pm »
Code: [Select]
// KeyPreview on
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = LCLType.VK_S) and (ssCtrl in Shift) then
    ButtonStartClick(Sender);
end; 
« Last Edit: April 05, 2011, 11:46:08 pm by typo »

cybersmyth

  • Jr. Member
  • **
  • Posts: 51
Re: Creating keyboard shortcuts
« Reply #4 on: April 06, 2011, 03:21:12 pm »
The FormKeyDown method worked great after a quick tweak. Apparently you have to have the form property for "KeyPreview" set to true. Otherwise each object will to capture the keyboard.

Thanks guys!

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Creating keyboard shortcuts
« Reply #5 on: April 06, 2011, 09:38:05 pm »
Now that you've done it the hard way, you could try leledumbo's approach and remove all redundant code:
- Drop a TActionList op the form.
- Add a new action to the actionlist that performs the requested action in the OnExecute method.
- Set Ctrl-s as the shortcut key for this action via the Object Inspector.
- Assign this action to the Start button as well.

Configuration over programming!
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018