Solved

Key trapping (OH NO)

Posted on 2002-03-14
7
245 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 33

Expert Comment

by:Slick812
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now