【问题标题】:Detecting Key Presses Across Applications in Powershell在 Powershell 中检测跨应用程序的按键
【发布时间】:2018-02-11 03:38:39
【问题描述】:

这是一篇可以通过 Wayback 机器在 Powershell.com 上找到的帖子。它是从 2015 年开始的,不再出现在网站上。 链接在这里:Detecting Key Presses Across Applications

这篇文章是为了存档。

【问题讨论】:

    标签: powershell powershell-2.0 user32


    【解决方案1】:

    通过访问 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)
    

    【讨论】:

      猜你喜欢
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多