Solved

ESC Key

Posted on 2006-07-21
5
2,768 Views
Last Modified: 2009-12-16
Hi,

Here is the situation: Usually, we use quite a lot of FORMS in a Delphi application. MainForm, DialogForm, SomeForm, etc.

In each form, we put several objects, TEdit, TListBox, TComboBox, TCheckbox, etc.

What is the best way to catch the ESC key for "closing" a form ?

I know that we can catch it in the "KeyDown" event.

But it will be too much for catching this in every object's event.

Because I tried to set in the TForm OnKeyDown, it will not work if the focus is in TEdit.

Anyone know a trick for this ?

One catch function per-TForm is ok tough.

Thanks.
0
Comment
Question by:muis2002
5 Comments
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 125 total points
ID: 17157764
set the form KeyPreview to true
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 17157766
then you can use the form's OnKeyDown even to catch the keys before they get to the controls
0
 
LVL 14

Expert Comment

by:Hamidreza Vakilian
ID: 17157805
Hi!

you can easily solve this problem by using API Hook procedures.

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}
Add these lines to your program =>

var
  Form1: TForm1;

  Hook: HHook; {this intercepts keyboard input}

  {callback's declaration}
  function KeyboardHookProc
   (Code: Integer;
   WordParam: Word;
   LongParam: LongInt): LongInt; stdcall;

implementation
....
{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}
Add this line to OnCreate Event of the form =>

Hook:=SetWindowsHookEx(WH_KEYBOARD,
           {callback —>}  @KeyboardHookProc,
                          HInstance,
                          GetCurrentThreadId());
{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}
The main hook function goes here =>

function KeyboardHookProc
   (Code: Integer;
   WordParam: Word;
   LongParam: LongInt) : LongInt;
begin

if (wordparam=vk_escape)and(form1.Focused=true) then form1.close;
{if form is focused and the user pressed ESC then close form}
result := 0;
 {To prevent Windows from passing the keystrokes
 to the target window, the Result value must
 be a nonzero value.}

end;
{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}
To free system resources associated with the hook, we must call the UnhookWindowsHookEx function in the OnDestroy event:

procedure TForm1.FormDestroy(Sender: TObject);
begin
  {unhook the keyboard interception}
  UnHookWindowsHookEx(Hook);
end;
{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

you can use this technique for hooking every keystroke from the keyboard...

HRV

 





0
 
LVL 4

Expert Comment

by:JDSkinner
ID: 17157865
Hi

Set the form to receive keyboard events before the active control.
This can be done in the FormCreate event.

procedure TForm1.FormCreate(Sender: TObject);
begin
    Form1.KeyPreview := True; // default stting is false
end;
 
You can then use the FormKeyPress event to check if the Escape key has been pressed.

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if (key = #27) then Close;
end;
0
 
LVL 5

Expert Comment

by:Scay7
ID: 17157892
Aye on the Tform1 object box set keypreview to TRUE then itll catch the key globaly before getting to controls like
JDSkinner and BlackTigerX said...

Peace Gav
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now