在PowerShell中检测跨应用程序的按键 [英] Detecting Key Presses Across Applications in Powershell

查看:12
本文介绍了在PowerShell中检测跨应用程序的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个可以通过Wayback机器在Powershell.com上找到的帖子。它是2015年的,现在已经不在网站上了。 指向它的链接在此处:Detecting Key Presses Across Applications

此帖子旨在存档。

推荐答案

通过访问Windows低级系统调用,PowerShell可以查询 用于按下的键的键盘。下面的示例一直等到 用户按‘A’。这是一个简单的示例,没有考虑 Shift键的状态,也不检查虚拟键。然而,它 检测所有打开的应用程序中的按键操作。因此,PowerShell将 检测按下的"A"键,即使它没有焦点,并且您 已将"A"输入其他应用程序。

#requires -Version 2

$signature = @'
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] 
public static extern short GetAsyncKeyState(int virtualKeyCode); 
'@

# load signatures and make members available
$API = Add-Type -MemberDefinition $signature -Name 'Keypress' -Namespace API -PassThru

# wait for 'A'
$waitFor = 'A'
$ascii = [byte][char]$waitFor.ToUpper()

do 
{
  Start-Sleep -Milliseconds 40
} until ($API::GetAsyncKeyState($ascii) -eq -32767)

这篇关于在PowerShell中检测跨应用程序的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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