Windows api: 等到系统范围内所有键盘键都被释放
Windows api: wait until all keyboard keys are released system wide
我在 Delphi 中制作了一个应用程序,可以处理一些定义的系统范围的热键,运行完美。但是,对于某些热键功能,我必须 trigger/simulate 一些键盘敲击,例如 ALT+ENTER。这在用户直接释放热键时效果很好,但是当用户仍然按下这些键时,键盘模拟失败。
有没有办法(使用 windows API)在我处理键盘模拟之前检查是否所有键都已释放?
使用 GetAsyncKeyState,因为此 API 反映了键盘的真实当前状态,而不是您的应用上次调用时 GetMessage
。只需编写一个循环,为 0 到 0xFF 之间的每个值调用它。
If the most significant bit is set, the key is down
感谢@David Ching 和@David Heffernan(两个 David!)解决方案不仅要测试键盘输入,还要测试鼠标输入或更好的输入设备的状态。
鼠标也包括在内,因为:
{ Virtual Keys, Standard Set }
VK_LBUTTON = 1;
VK_RBUTTON = 2;
VK_MBUTTON = 4; { NOT contiguous with L & RBUTTON }
因此,如果不想测试鼠标按钮,则必须将其从循环中排除。最好也检查这些,因为有些热键必须与鼠标一起使用。最好检查输入上的所有内容是否空闲。
function isUserInputDevicesInUse() : Boolean; // Keyboard pressed / mouse pressed?
var
i : LongInt;
begin
i:=256;
Result:=FALSE;
while( i > 0 ) and ( NOT Result ) do
begin
Dec( i );
Result:=( GetAsyncKeyState(i) < 0 );
end;
end;
function isUserInputDevicesIdle() : Boolean;
begin
Result:=NOT isUserInputDevicesInUse();
end;
我在 Delphi 中制作了一个应用程序,可以处理一些定义的系统范围的热键,运行完美。但是,对于某些热键功能,我必须 trigger/simulate 一些键盘敲击,例如 ALT+ENTER。这在用户直接释放热键时效果很好,但是当用户仍然按下这些键时,键盘模拟失败。
有没有办法(使用 windows API)在我处理键盘模拟之前检查是否所有键都已释放?
使用 GetAsyncKeyState,因为此 API 反映了键盘的真实当前状态,而不是您的应用上次调用时 GetMessage
。只需编写一个循环,为 0 到 0xFF 之间的每个值调用它。
If the most significant bit is set, the key is down
感谢@David Ching 和@David Heffernan(两个 David!)解决方案不仅要测试键盘输入,还要测试鼠标输入或更好的输入设备的状态。
鼠标也包括在内,因为:
{ Virtual Keys, Standard Set }
VK_LBUTTON = 1;
VK_RBUTTON = 2;
VK_MBUTTON = 4; { NOT contiguous with L & RBUTTON }
因此,如果不想测试鼠标按钮,则必须将其从循环中排除。最好也检查这些,因为有些热键必须与鼠标一起使用。最好检查输入上的所有内容是否空闲。
function isUserInputDevicesInUse() : Boolean; // Keyboard pressed / mouse pressed?
var
i : LongInt;
begin
i:=256;
Result:=FALSE;
while( i > 0 ) and ( NOT Result ) do
begin
Dec( i );
Result:=( GetAsyncKeyState(i) < 0 );
end;
end;
function isUserInputDevicesIdle() : Boolean;
begin
Result:=NOT isUserInputDevicesInUse();
end;