Solved

Key trapping (OH NO)

Posted on 2002-03-14
7
252 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
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 33

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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