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; 只有将焦点设置为主窗
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;
只有将焦点设置为主窗体时,上述事件才有效.
如果我运行应用程序并继续按Tab并将焦点更改为Form上的任何控件,它将禁用此事件,直到我们再次将Focus更改为主窗体?
问题是
即使焦点不在主窗体中,我如何能够检测到三个按键是否被按下?
我也想过如果我使用RegisterHotKey,但在我的应用程序运行时注册Q,W和E并不是一个好主意.
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;您可以将表单的
KeyPreview
设置为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 byKeyPreview
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 byKeyPreview
because
it does not generate a keyboard events.
KeyPreview
is false by default.