检测按下键盘上的 SHIFT 键(仅限 shift 键)

Detect SHIFT key pressed on a keyboard (only the shift key)

提问人:StartingPS 提问时间:3/10/2023 更新时间:3/10/2023 访问量:93

问:

我正在启动 PowerShell,我正在尝试使用网上找到的以下代码测试是否按下了 shift 键:

function Test-ControlKey
{
  # key code for Shift key:
  $key = 16

  # this is the c# definition of a static Windows API method:
  $Signature = @'
    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] 
    public static extern short GetAsyncKeyState(int virtualKeyCode); 
  '@

  Add-Type -MemberDefinition $Signature -Name Keyboard -Namespace PsOneApi
  [bool]([PsOneApi.Keyboard]::GetAsyncKeyState($key) -eq -32767)
}

尝试运行此函数时,出现此错误:

创建管道时出错。 + CategoryInfo:NotSpecified:(:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId:运行时异常

该功能有什么问题,以及如何仅检测shift键,而不是用另一个键按下。

感谢您的帮助

我尝试了网上的其他方法,但没有找到任何能给我带来好结果的方法

PowerShell 输入 键盘

评论


答: 暂无答案