Solved

Key trapping (OH NO)

Posted on 2002-03-14
7
254 Views
Last Modified: 2012-05-04
Please can anyone tell me how to trap a key on a form level basis.
I have looked at TForm.Keypreviem := True, but that does not do what I want. For example if a form has a TButton on it, and the button has focus, then TForm.OnKeyDown does not trap the enter key ????

How can I get round this. I do not want a global hook system (if it is not neccessary).

Many Thanks

Phil.

0
Comment
Question by:phil1_curtis
[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
7 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 6864130
I think that is better to hook the keyboard . Couple articles with sources at www.delphi3000.com .
Just go to search and type : hook .
0
 
LVL 4

Expert Comment

by:binkzz
ID: 6864247
You can't catch global key pressings easily, the best way to do this is to forward each components' OnKeyPress event to your form's OnKeyPress handler (by selecting it from the drop down list in the event list of the button).

Mr B
0
 
LVL 4

Expert Comment

by:nestorua
ID: 6864935
HI,
You can do what you want.
Simply put the TApplicationEvents component on your form
and handle the Message WM_KEYDOWN with wParam=VK_RETURN.
And your button will do nothing on pressing the
Enter key.
Sincerely,
Nestorua.
0
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!

 
LVL 34

Expert Comment

by:Slick812
ID: 6867236
hello phil1_curtis, you might try setting thr TabStop for that TButton to False, and nuestorua is right about using the TApplicationEvents for Application message handling, but if you haven't worked with windows Messaging, it can be confusing about the messages and the Hwnd identifiers.
0
 

Expert Comment

by:westphal
ID: 6869861
Hello,

You can trap all messages in the application.OnMessage
ex:

unit ufMain;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, ComCtrls;

type
 TForm1 = class(TForm)
   Button1: TButton;
   Memo1: TMemo;
   procedure FormCreate(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
   procedure MyMessage(var Msg: TMsg; var Handled: Boolean);
 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 application.OnMessage:=MyMessage
end;

procedure TForm1.MyMessage(var Msg: TMsg; var Handled: Boolean);
begin
 if msg.message=WM_KEYDOWN then MessageBeep(0);
end;

end.
0
 

Author Comment

by:phil1_curtis
ID: 6917486
I am going to accept the answer from nestoura, because it does exactly what I want.

Please change from a comment to an answer and I will send the points.

Phil.
0
 
LVL 4

Accepted Solution

by:
nestorua earned 100 total points
ID: 6917501
HI, phil1_curtis,
I'm glad you accepted my comment as an answer,
but I don't know how to change it to make it now an answer.
Maybe you simply accept my comment as an answer
(or teach me how to make the changing you proposed).
Sincerely,
Nestorua.
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

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 200
Downloading email attachments 2 84
select query - oracle 16 101
FMX and jaudiotracker playing memory stream 29 98
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…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 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