Results 1 to 6 of 6

Thread: detect whether NUMLOCK is on or off

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2002
    Posts
    24

    detect whether NUMLOCK is on or off

    anyway to detect whether the NUMLOCK is currently on or off.

    I need to off the NUMLOCK before sending DOWN to a dos program to create arrow down signal, otherwise '2' is sent instead.

    So, any idea to do that? How? Any example code?Thanks

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    perhaps someone else will post the code (i'm too tired)
    but the api call you want is GetKeyboardState.
    Study up on it and arrays. It needs a 256 byte array to work.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    2. Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
    3.  
    4. Private Const VK_CAPITAL = &H14
    5. Private Const VK_NUMLOCK = &H90
    6. Private Const VK_SCROLL = &H91
    7.  
    8. ReDim KeyboardBuffer(256) As Byte
    9. GetKeyboardState KeyboardBuffer(0)
    10. 'This example show you how to press the Caps Lock button. if you want
    11. 'to press the Num Lock button, Replace all the following 3 'VK_CAPITAL'
    12. 'with 'VK_NUMLOCK' or VK_SCROLL as desired.
    13. If KeyboardBuffer(VK_CAPITAL) And 1 Then
    14. KeyboardBuffer(VK_CAPITAL) = 0
    15. Else
    16. KeyboardBuffer(VK_CAPITAL) = 1
    17. End If
    18. SetKeyboardState KeyboardBuffer(0)
    19.  
    20. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    21.  
    22. 'To check if Num Lock is on Replace the 'vbKeyCapital' below with 'vbKeyNumlock'
    23.  
    24. Dim lngWhatState As Long
    25. lngWhatState = GetKeyState(vbKeyCapital)
    26. If lngWhatState = 1 Then
    27. MsgBox "The Caps Lock Is On"
    28. Else
    29. MsgBox "The Caps Lock Is Off"
    30. End If

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2002
    Posts
    24
    then, how to off the NUMLOCK if it is on?

  5. #5
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313
    Code:
    If KeyboardBuffer(VK_CAPITAL) And 1 Then
    KeyboardBuffer(VK_CAPITAL) = 0
    Else
    KeyboardBuffer(VK_CAPITAL) = 1
    End If
    SetKeyboardState KeyboardBuffer(0)
    my best guess it would be in the code Hack provided. My guess would be u sed VK_CAPITAL to 0 for off and 1 for on then tell it to setkeyboardstate keyboardbuffer(0)

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. '*****'This decleration block is needed to switch Numlock to on.. yike
    2. ' Declare Type for API call:
    3.       Private Type OSVERSIONINFO
    4.         dwOSVersionInfoSize As Long
    5.         dwMajorVersion As Long
    6.         dwMinorVersion As Long
    7.         dwBuildNumber As Long
    8.         dwPlatformId As Long
    9.         szCSDVersion As String * 128   '  Maintenance string for PSS usage
    10.       End Type
    11.  
    12.       ' API declarations:
    13.  
    14.       Private Declare Function GetVersionEx Lib "kernel32" _
    15.          Alias "GetVersionExA" _
    16.          (lpVersionInformation As OSVERSIONINFO) As Long
    17.  
    18.       Private Declare Sub keybd_event Lib "user32" _
    19.          (ByVal bVk As Byte, _
    20.           ByVal bScan As Byte, _
    21.           ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    22.  
    23.       Private Declare Function GetKeyboardState Lib "user32" _
    24.          (pbKeyState As Byte) As Long
    25.  
    26.       Private Declare Function SetKeyboardState Lib "user32" _
    27.          (lppbKeyState As Byte) As Long
    28.  
    29.       ' Constant declarations:
    30.       Const VK_NUMLOCK = &H90
    31.       Const VK_SCROLL = &H91
    32.       Const VK_CAPITAL = &H14
    33.       Const KEYEVENTF_EXTENDEDKEY = &H1
    34.       Const KEYEVENTF_KEYUP = &H2
    35.       Const VER_PLATFORM_WIN32_NT = 2
    36.       Const VER_PLATFORM_WIN32_WINDOWS = 1
    37. ' *********End of Numlock Check declarations *********8
    38.  
    39.  
    40.  
    41. Public Sub TurnNumLockON()
    42.  
    43.  
    44. Dim o As OSVERSIONINFO
    45.       Dim NumLockState As Boolean
    46.       o.dwOSVersionInfoSize = Len(o)
    47.       GetVersionEx o
    48.       Dim keys(0 To 255) As Byte
    49.       GetKeyboardState keys(0)
    50.  
    51.                                     ' Switch Numlock on if needed
    52.       NumLockState = keys(VK_NUMLOCK)
    53.       If NumLockState <> True Then    'Turn numlock on
    54.         If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=== Win95/98
    55.  
    56.           keys(VK_NUMLOCK) = 1
    57.           SetKeyboardState keys(0)
    58.         ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=== WinNT
    59.         'Simulate Key Press
    60.           keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    61.         'Simulate Key Release
    62.           keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
    63.              Or KEYEVENTF_KEYUP, 0
    64.         End If
    65.       End If
    66. End Sub

    The code I just posted actually switches numlock on if its off, but you can just change the code in the turnnumlockon sub. I ripped it outta one of my programs... god knows who actually wrote this code..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width