Delphi – How does my form detect the keydown event when another control is focused?

procedure TMainForm.KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (GetKeyState(Ord('Q '))<0) and (GetKeyState(Ord('W'))<0) and (GetKeyState(Ord('E'))<0)
then ShowMessage('You pressed it');
end;

The above event only works when the focus is set to the main form.
If I run the application and continue to press Tab and change the focus to any control on the Form, it Will this event be disabled until we change the Focus to the main form again?

The question is
even if the focus is not in the main form, how can I detect whether the three buttons are pressed?

I also thought about if I use RegisterHotKey, but registering Q, W and E while my application is running is not a good idea.

 procedure TMainForm.WMHotKey(var Msg: TWMHotKey);
begin
if ActiveCaption ='my Form Caption' then
Begin
if Msg.HotKey = HotKey1 then
begin
//DoSomething;
end
else
if Msg.HotKey = HotKey2 then
begin
//DoSomething;
end;
End
else
//DoSomething;
end;

You can set the form’s KeyPreview to true.

If KeyPreview is true , keyboard events occur on the form before they
occur on the active control. (The active control is specified by the
ActiveControl property.)

If KeyPreview is false, keyboard events occur only on the active
control.

Navigation keys (Tab, BackTab, the arrow keys, and so on) are
unaffected by KeyPreview because they do not generate keyboard
events. Similarly, when a button has focus or when its Default
property is true, the Enter key is unaffected by KeyPreview because
it does not generate a keyboard events.

KeyPreview is false by default.

procedure TMainForm.KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (GetKeyState(Ord('Q'))<0) and (GetKeyState(Ord('W') ))<0) and (GetKeyState(Ord('E'))<0)
then ShowMessage('You pressed it');
end;

Only set the focus The above event is only valid when it is the main form.
If I run the application and continue to press Tab and change the focus to any control on the Form, it will disable this event until we change the Focus to the main form again ?

The question is
even if the focus is not in the main form, how can I detect whether the three buttons are pressed?

I also thought about if I use RegisterHotKey, but registering Q, W and E while my application is running is not a good idea.

 procedure TMainForm.WMHotKey(var Msg: TWMHotKey);
begin
if ActiveCaption ='my Form Caption' then
Begin
if Msg.HotKey = HotKey1 then
begin
//DoSomething;
end
else
if Msg.HotKey = HotKey2 then
begin
//DoSomething;
end;
End
else
//DoSomething;
end;

You can set the KeyPreview< /code>Set to true.

If KeyPreview is true, keyboard events occur on the form before they
occur on the active control. (The active control is specified by the
ActiveControl property.)

If KeyPreview is false, keyboard events occur only on the active
control.

Navigation keys (Tab, BackTab, the arrow keys, and so on) are
unaffected by KeyPreview because they do not generate keyboard
events. Similar ly, when a button has focus or when its Default
property is true, the Enter key is unaffected by KeyPreview because
it does not generate a keyboard events.

KeyPreview is false by default.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3142 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.