Solved

ESC Key

Posted on 2006-07-21
5
2,795 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 80
can't find the executable in Simulator 1 92
Create a path if not exists 7 78
When i run adoquery my application freezes 26 167
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

822 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