gpt4 book ai didi

delphi - 当另一个控件获得焦点时,我的窗体如何检测 KeyDown 事件?

转载 作者:行者123 更新时间:2023-12-03 15:05:07 25 4
gpt4 key购买 nike

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;

最佳答案

您可以设置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 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.

关于delphi - 当另一个控件获得焦点时,我的窗体如何检测 KeyDown 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26761084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com