PowerShell: Turn "Num Lock" on and off.

I would like to turn on and off the “Num Lock” key on the keyboard. I have tried several examples on the Internet and here, without success. This is the closest solution to me:

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

The above code looks like it can work, and I see the “Num Lock” indicator on my keyboard for a second, but it does not “stick”.

+6
source share
3 answers

I experienced the same thing as you.

This works great for me, try:

 $wsh = New-Object -ComObject WScript.Shell $wsh.SendKeys('{NUMLOCK}') 
+12
source

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

+1
source
 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 
0
source

Source: https://habr.com/ru/post/911241/


All Articles