Link to home
Start Free TrialLog in
Avatar of kbay808
kbay808Flag for United States of America

asked on

How to turn on numlock via VBS code?

For some reason my code is turning off numlock and I need a way to make sure it's turned back on.
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{NUMLOCK}"

Open in new window

Avatar of kbay808

ASKER

How can I check to make sure that numlock is off?  I don't want it to turn it off if it happens to be on.
Avatar of Bill Prew
Bill Prew

Is the code you want to add this too VBScript, or VBA?


»bp
Also, as I reread this, I suppose another approach is to figure out where your code is turning off numlock, and stop that from happening.  If doable that would be a better solution than trying to determine the state before you ran, and then restore that state afterwards.

What can you tell us about the application and the code involved?


»bp
Avatar of kbay808

ASKER

The issue is when using sendkeys
Avatar of kbay808

ASKER

I use this code in VBA.  Is there is version that will check in VBS?

If GetKeyState(NumlockKey) = 0 Then
     SendKeys "{NUMLOCK}"
End if

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbay808

ASKER

@ Shaun Vermaak -  for some reason that link won't open for me, but I would rather not use word if possible.

@ Bill Prew - That work great.  Thanks
Avatar of kbay808

ASKER

Thank you
And you could package that in a small function that returns a logical variable for IF test, like:

If GetNumlockState() = True Then
    Wscript.Echo "Numlock is ON"
Else
    Wscript.Echo "Numlock is FF"
End If

Function GetNumlockState()
    strResult = WScript.CreateObject("WScript.Shell").Exec("powershell.exe -command [console]::NumberLock").StdOut.ReadLine
    If strResult = "True" Then GetNumlockState = True Else GetNumlockState = False
End Function

Open in new window


»bp
Great, glad we got to a working solution, that took some thinking but was fun, thanks for the feedback.


»bp