Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Using the TApplicationEvents OnShortCut event to detect Alt+C key presses

Tags:

delphi

vcl

I am using TApplicationEvents OnShortCut event to get application keyboard short cuts in a Delphi program.

Using the following code:

procedure TForm1.ApplicationEvents1ShortCut(var Msg: TWMKey; var Handled: Boolean) ;
begin
   if (Msg.CharCode = VK_F9) then
   begin
     ShowMessage('F9 pressed!') ;
     Handled := True;
   end;
end;

Question:

How do I detect when 'ALT C' has been pressed ?

like image 814
Charles Faiga Avatar asked Apr 09 '09 16:04

Charles Faiga


2 Answers

Like so:

procedure TForm1.ApplicationEvents1ShortCut(var Msg: TWMKey;
  var Handled: Boolean);
begin
  if (Msg.CharCode = Ord('C'))
    and (HiWord(Msg.KeyData) and KF_ALTDOWN <> 0)
  then begin
    ShowMessage('Alt+C pressed!') ;
    Handled := TRUE;
  end;
end;

Please note that using Alt and some key only is a bad choice for a shortcut, as the system uses these to activate menu items or dialog controls.

like image 191
mghie Avatar answered Oct 20 '22 04:10

mghie


Or you can create simple TAction, they eats shortcuts before others.

like image 2
DiGi Avatar answered Oct 20 '22 05:10

DiGi