Solved

Key trapping (OH NO)

Posted on 2002-03-14
7
256 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
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!

 
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

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

696 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