I am using PowerShell in my script to check the status of various keys like NumLock and CapsLock.
powershell.exe -Command [Console]::CapsLock
powershell.exe -Command [Console]::NumberLock
But I found no way to check the status of ScrollLock through PowerShell console command. Can you guys tell me why powershell.exe -Command [Console]::ScrollLock
does not work and what is needed to be done?
You can get the ScrollLock
key state with the GetKeyState()
function from the user32.dll
native Windows API:
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With