6
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 并将焦点更改为窗体上的任何控件,它将禁用此事件,直到我们再次将焦点更改为主窗体?

问题是 即使焦点不在主窗体中,我如何检测三个键被按下?

我还想如果我使用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;
4

1 回答 1

15

您可以将KeyPreview表单设置为 true。

如果KeyPreview为真,则键盘事件会先于窗体上发生,然后再发生在活动控件上。(活动控件由 ActiveControl 属性指定。)

如果KeyPreview为 false,则键盘事件仅在活动控件上发生。

导航键(Tab、BackTab、箭头键等)不受影响,KeyPreview因为它们不生成键盘事件。类似地,当一个按钮有焦点或当它的 Default 属性为 true 时,Enter 键不受影响,KeyPreview因为它不会生成键盘事件。

KeyPreview默认为假。

于 2014-11-05T15:46:33.267 回答