Solved

ESC Key

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

Technology Partners: 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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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