Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Key trapping (OH NO)

Posted on 2002-03-14
7
Medium Priority
?
257 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 300 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

715 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