Emulate a keyboard button via the Command Line

43,458

Solution 1

How do I toggle numlock status using PowerShell.

Use the following script:

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

Source StackOverflow answer PowerShell: Toggle “Num Lock” on and off. by Andy Arismendi

As pointed out by ABashore in his comment this can be shortened as follows:

$wshell.SendKeys('{NUMLOCK}')

Solution 2

Here's a powershell line that will toggle your NUMLOCK. I've tested this and it works on my Logitech K120 USB keyboard.

$wshell.SendKeys('{NUMLOCK}')

Here's a list of the other SendKey codes.

If you need to send the keystroke to an interactive application, more code will be needed.

Share:
43,458

Related videos on Youtube

ZomoXYZ
Author by

ZomoXYZ

Updated on September 18, 2022

Comments

  • ZomoXYZ
    ZomoXYZ over 1 year

    My keyboard doesn't have a Num Lock key, and none of the registry edits work for some reason.

    Can I use the Command Prompt or PowerShell on Windows 10 to "press" a key? I'm looking for something like press pgup to press the Page Up Key in the Command Prompt or PowerShell, but for Num Lock.

  • Lisa
    Lisa over 8 years
    It doesnt appear that you need to define "wsh" to do what he needs. I was able to simply toggle the NUMLOCK with just a single line.
  • DavidPostill
    DavidPostill over 8 years
    @ABashore Cool. I've upvoted your answer
  • ZomoXYZ
    ZomoXYZ almost 4 years
    would you mind updating your answer to give a little more information as to what’s going on in that script?
  • Davidson Lima
    Davidson Lima over 3 years
    For one line command what worked to me was: (New-Object -ComObject WScript.Shell).SendKeys('{NUMLOCK}')