Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: Toggle "Num Lock" on and off.

I would like to be able toggle on and off the "Num Lock" key on the keyboard. I have tried multiple examples around the web and here, with no success. This is the closest thing I've got to a solution:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("{NUMLOCK}")

The above code looks like it might work and I see the "Num Lock" indicator on my keyboard flash for a second but it doesn't "stick".

like image 632
Vippy Avatar asked Mar 20 '12 21:03

Vippy


People also ask

Which key toggles number pad on or off?

Techopedia Explains Toggle Key The most commonly used toggle key is the caps lock key, which alternates the letter keys between uppercase and lowercase. Num lock is another toggle key which helps to input numerals from the numeric keyboard and is turned on by default.

How do I turn on Num Lock on my keyboard?

The NmLk key is located on the top, right hand side of the keyboard. Sometimes it is on the same key as F8, F7, or Insert. Press Fn+F8, F7, or Insert to enable/disable numlock. For 15-inch or above laptops, the numeric keypad is located on the right side of the keyboard.


1 Answers

Stumbled here looking for a way to keep Num Lock ON eternally (apparently the word "lock" doesn't mean much 🤷‍♂️). This script checks Num Lock status every 2 seconds, and switches it back on if it's been turned off:

$wsh = New-Object -ComObject WScript.Shell
while($true){
if ([console]::NumberLock -eq $false) {
$wsh.SendKeys('{NUMLOCK}')
}
Start-Sleep 2
}

I think it would be a good candidate for converting into a standalone tiny .exe using PS2EXE.

like image 169
KERR Avatar answered Oct 04 '22 01:10

KERR