打开或关闭滚动锁定

Gau*_*nta 6 keyboard powershell scroll-lock

我在脚本中使用 PowerShell 来检查各种键的状态,例如NumLockCapsLock

powershell.exe -Command [Console]::CapsLock
powershell.exe -Command [Console]::NumberLock
Run Code Online (Sandbox Code Playgroud)

ScrollLock但我发现无法通过 PowerShell 控制台命令检查状态。你们能告诉我为什么powershell.exe -Command [Console]::ScrollLock不起作用以及需要做什么吗?

Mat*_*sen 5

您可以使用本机 Windows API中的函数ScrollLock获取按键状态:GetKeyState()user32.dll

Add-Type -MemberDefinition @'
[DllImport("user32.dll")] 
public static extern short GetKeyState(int nVirtKey);
'@ -Name keyboardfuncs -Namespace user32

# 0x91 = 145, the virtual key code for the Scroll Lock key 
# see http://www.foreui.com/articles/Key_Code_Table.htm
if([user32.keyboardfuncs]::GetKeyState(0x91) -eq 0){
    # Scroll Lock is off
}
else {
    # Scroll Lock is on
}
Run Code Online (Sandbox Code Playgroud)