-3

I want my program to detect if ESC key is pressed to do something, but it's not detecting it ! I want to detect the escape key pressing during the whole runtime even if focus is set on another component ! I tried this code

if key=#27 then
 begin
  //do something
 end;

any suggestions please ?

7
  • You need to be much more specific. Where is this code in your program? Some kind of key event I'm sure, but what specifically? Also key events tend to default to the active control unless you set KeyPreview to true.
    – Glenn1234
    Dec 30, 2012 at 21:32
  • @Glenn1234 I've Set Keypreveiw to true in the OnCreate Event, this code is in onkeypress event of the form1 , I press escape key but nothing happens !
    – Billo .S
    Dec 30, 2012 at 21:35
  • That's the problem. OnKeyPress is only for alphabetic characters. It needs to be in the OnKeyDown or OnKeyUp events.
    – Glenn1234
    Dec 30, 2012 at 21:40
  • 1
    Assuming you're wanting to hook the escape key only in your own program, set your Form's KeyPreview property to true.
    – LaKraven
    Dec 30, 2012 at 21:44
  • Do you want to detect this system wide? Dec 30, 2012 at 22:03

2 Answers 2

4

Which event handler are you using? If it's connected to a control, the event will be fired when a key is pressed and the control has focus.

If you want the event to fire when a key is pressed while your application has focus, drop a TApplicationEvents component on a form and add an OnMessage event handler something like this:

procedure TfrmMain.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
begin
  case Msg.message of
    WM_KEYDOWN:
      if Msg.wParam = 27 then
      begin
        // Do something
      end;
  end;
end; 
4
  • my code is written in onkeypress event of form1 , in runtime nothing happens when I press escape
    – Billo .S
    Dec 30, 2012 at 21:36
  • I'm getting this error : [DCC Error] Unit1.pas(104): E2065 Unsatisfied forward or external declaration: 'TForm1.FormKeyDown'
    – Billo .S
    Dec 30, 2012 at 22:23
  • Why post a compiler error without code? What do you expect us to do? Dec 30, 2012 at 22:27
  • @Billo.S this is not a simple copy-paste solution. HowTo is right in the answer, just follow it.
    – Sir Rufo
    Dec 31, 2012 at 8:50
4

To formalize what I wrote above as an answer, borrowing from the Delphi 3 help for OnKeyDown

The following code aborts a print job if the user presses Esc. Note that you should set KeyPreview to True to ensure that the OnKeyDown event handler of Form1 is called.

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  begin
    if (Key=VK_ESCAPE) and Printer.Printing then
      begin
        Printer.Abort;
        MessageDlg('Printing aborted', mtInformation, [mbOK],0);
      end;
  end;
14
  • it's not working ! I used your very code to press a button on Escape key pressing but it's not working ! when focus is not on the form it doesn't work , it works on when application is activated but if you press anything it doesn't work anymore !
    – Billo .S
    Dec 30, 2012 at 21:49
  • it's not working during runtime !
    – Billo .S
    Dec 30, 2012 at 21:53
  • Well, naturally the event only fires when the form is active. Dec 30, 2012 at 21:57
  • 1
    Okay, so you changed your requirements again, but you haven't really stated them well anyway. You might try stating them in a much more clearer manner than you have so far.
    – Glenn1234
    Dec 30, 2012 at 21:57
  • 2
    @Billo.S: Please stop adding details in comments, and edit your question to clarify what it is you're trying to do. Making us play 20 questions trying to figure it out is not how StackOverflow works.
    – Ken White
    Dec 30, 2012 at 22:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.