Solved

ESC Key

Posted on 2006-07-21
5
2,824 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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