Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle Scroll Lock on or off

Tags:

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?

like image 746
Gaurav Samanta Avatar asked Jul 20 '18 16:07

Gaurav Samanta


1 Answers

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
}
like image 146
Mathias R. Jessen Avatar answered Oct 02 '22 13:10

Mathias R. Jessen