Windows API:等到系统范围内释放所有键盘键 [英] Windows api: wait until all keyboard keys are released system wide

查看:121
本文介绍了Windows API:等到系统范围内释放所有键盘键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Delphi中创建了一个应用程序,该应用程序可以处理一些定义的系统范围的热键,并且运行良好.但是,对于某些热键功能,我必须触发/模拟一些键盘击键,例如ALT + ENTER.当用户直接释放热键时,这种方法非常有用,但是当用户仍然按下热键时,键盘模拟就会失败.

I have made an application in Delphi that handles some defined system wide hotkeys, works perfectly. However, for some hotkey functionality I have to trigger/simulate some keyboard strokes such as ALT+ENTER. This works great when the user releases the hotkey keys directly, but when the keys are still pressed by the user the keyboard simulation fail.

在进行键盘模拟之前,是否有一种方法(使用Windows API)检查是否释放了所有键?

Is there a way (with windows API) to check if all keys are released before I process a keyboard simulation?

推荐答案

感谢@David Ching和@David Heffernan(两个David!).该解决方案不仅可以测试键盘输入,还可以测试鼠标输入或更好的状态.输入设备.

Thanks go to @David Ching and @David Heffernan (two Davids!) The solution is not only to test keyboard input but also mouse input or better, the state of input devices.

之所以也包括鼠标,是因为:

The mouse is also included because of:

  { Virtual Keys, Standard Set }
  VK_LBUTTON = 1;
  VK_RBUTTON = 2;
  VK_MBUTTON = 4;  { NOT contiguous with L & RBUTTON }

因此,如果不想测试鼠标按钮,则必须将其从循环中排除.最好也检查这些,因为鼠标必须使用一些热键.最好检查输入中的所有内容是否空闲.

So, if don't want to test mousebuttons you have to exclude it from the loop. It's better to check these also because there are hotkeys that must be used with the mouse. It's better to check everything on input is idle.

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;

这篇关于Windows API:等到系统范围内释放所有键盘键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆