?
Solved

ESC Key

Posted on 2006-07-21
5
Medium Priority
?
2,849 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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