Link to home
Start Free TrialLog in
Avatar of Mustadio
Mustadio

asked on

NumsLock

How can I make sure my NumLock is ON?
Sendkeys("{NUMLOCK}") doesn't turn it on, and only makes it "flash" if it is already on... so..? :)

thx!
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

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
This code is better, it toggles the light as well. Taken from
http://www.kather.net/VisualBasicSource/KnowledgeBase/q177674.txt

Option Explicit
' Declare Type for API call:
Private Type OSVERSIONINFO
  dwOSVersionInfoSize As Long
  dwMajorVersion As Long
  dwMinorVersion As Long
  dwBuildNumber As Long
  dwPlatformId As Long
  szCSDVersion As String * 128   '  Maintenance string for PSS usage
End Type

' API declarations:
Private Declare Function GetVersionEx Lib "kernel32" _
   Alias "GetVersionExA" _
   (lpVersionInformation As OSVERSIONINFO) As Long

Private Declare Sub keybd_event Lib "user32" _
   (ByVal bVk As Byte, _
    ByVal bScan As Byte, _
    ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Declare Function GetKeyboardState Lib "user32" _
   (pbKeyState As Byte) As Long

Private Declare Function SetKeyboardState Lib "user32" _
   (lppbKeyState As Byte) As Long

' Constant declarations:
Const VK_NUMLOCK = &H90 'NumLock
Const VK_SCROLL = &H91 'ScrollLock
Const VK_CAPITAL = &H14 'CapsLock
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VER_PLATFORM_WIN32_NT = 2
Const VER_PLATFORM_WIN32_WINDOWS = 1

Private Sub Command1_Click()
    Dim o As OSVERSIONINFO
    Dim LockState As Boolean

    o.dwOSVersionInfoSize = Len(o)
    GetVersionEx o
    Dim keys(0 To 255) As Byte
    GetKeyboardState keys(0)
 
    LockState = keys(VK_NUMLOCK)
    'Toggle numlock
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '===== Win95
      keys(VK_NUMLOCK) = Not LockState
      SetKeyboardState keys(0)
    ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '===== WinNT
    'Simulate Key Press
      keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    'Simulate Key Release
      keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
         Or KEYEVENTF_KEYUP, 0
    End If
End Sub



Avatar of Moondancer
Moondancer

This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
https://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange