PowerShell: Toggle "Num Lock" on and off.
Asked Answered
B

5

16

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".

Bookplate answered 20/3, 2012 at 21:2 Comment(1)
Answers are also useful for those who can't find NumLock key on their physical keyboard.Minsk
H
30

I experienced the same on/off flicker you did.

This works fine for me though, give it a try:

$wsh = New-Object -ComObject WScript.Shell
$wsh.SendKeys('{NUMLOCK}')
Hagood answered 20/3, 2012 at 21:42 Comment(6)
(this makes me wonder what Powershell problems Andy hasn't met!)Grandstand
Seems like "Guru" would be an understatement for Andy. PowerShell overlord?Bookplate
@Bookplate Has a nice ring to it.. hehe ;-)Hagood
In addition you can use [console]::NumberLock to check if it is enabled or not.Arlettaarlette
resuming a very old thread, I know. Does this work before logging in? I'd like to set a scheduled task at startup, before logging in, so I can login with the numeric keyboard set. On my PC (win10), I tried to set a scheduled task, running both as me or as SYSTEM user, but if I try to launch it the task hangs, in "executing" status. Any (other) easy way to set numlock at startup? My PC bios has not an option for itArmand
Checking with task manager, I see that the task load an explorer extension (TortoiseSVN): very likely, that why the task does not work in a non interactive environmnet. Is there any power shell command (or other) allowing handling numLock status without these kind of "need the user logged in" problems?Armand
F
3

For what it is worth, from the perspective of the keyboard, the OS sends a set and then a reset of the toggle key (caps, scroll lock, numlock) when using [System.Windows.Forms.SendKeys]::SendWait("{CAPSLOCK}"), but only sends a single event using WScript.Shell.

Flats answered 26/4, 2016 at 11:46 Comment(0)
F
2

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.

Fixer answered 29/11, 2021 at 22:15 Comment(0)
J
0
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
$myshell = New-Object -com "wscript.Shell"
while($true) {
   $myshell.sendkeys('{CAPSLOCK}') # or NUMLOCK, or any
   $sleepms = Get-Random -Minimum 10 -Maximum 1000 # random sleep between 10ms and 1000ms
   Start-Sleep -Milliseconds $sleepms
}
# TODO; Conver to a MORSE blink generator ;-)
Johm answered 23/2 at 15:51 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Multiplicand
C
-3
intTime=0
strInputVal=InputBox("Enter the time","Enter Hours in Int")
intTime=strInputVal * 60 * 60

set WshShell = WScript.CreateObject("WScript.Shell")
For i = 1 to intTime
    WScript.Sleep 500
    WshShell.SendKeys "{NUMLOCK}"
    WScript.Sleep 500
Next
WScript.Quit
Set WshShell = Nothing 
Cacia answered 17/10, 2013 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.