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并将Focus更改为Form上的任何控件,它将禁用此事件,直到我们再次将Focus更改为主窗体?
问题是 即使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;
答案 0 :(得分:12)
您可以将表单的KeyPreview
设置为true。
如果
KeyPreview
为真,则表单上会出现键盘事件 在主动控件上发生。 (主动控制由。指定 ActiveControl属性。)如果
KeyPreview
为false,则键盘事件仅在活动状态下发生 控制。导航键(Tab键,BackTab键,箭头键等) 不受
KeyPreview
影响,因为它们不会生成键盘 事件。同样,当按钮具有焦点或其默认值时 如果属性为true,则Enter键不受KeyPreview
的影响,因为 它不会生成键盘事件。
KeyPreview
默认为false。