Toggle Scroll Lock on or off

4k views Asked by At

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?

2

There are 2 answers

0
Mathias R. Jessen On

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
}
1
Bill Riedy On

You can also get it by running:

[System.Windows.Forms.Control]::IsKeyLocked('Scroll')