Results 1 to 6 of 6

Thread: Can you turn on NumLock from VB ?

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Question Can you turn on NumLock from VB ?

    Another week, another barrage of questions................


    If a user starts to type using the NumLock key, is it possible to detect this and turn NumLock on ?

    Alternatively, is it easy enough to have a program turn NumLock on when it starts up ?


    Thanks.

  2. #2
    New Member
    Join Date
    Sep 2001
    Posts
    14

    please don't ask me how

    i do know it's possible --- so the answer to your question is yes... i know this because i have seen a "certain" program written in vb that does just what you want
    at least now you know the answer

    i just can't tell you how

    MAYBE I GAVE YOU HOPE!!!!!!!!!

  3. #3

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long

    Private Const VK_CAPITAL = &H14
    Private Const VK_NUMLOCK = &H90
    Private Const VK_SCROLL = &H91


    ReDim KeyboardBuffer(256) As Byte
    GetKeyboardState KeyboardBuffer(0)
    'This example show you how to press the Caps Lock button. if you want
    'to press the Num Lock button, Replace all the following 3 'VK_CAPITAL'
    'with 'VK_NUMLOCK' or VK_SCROLL as desired.
    If KeyboardBuffer(VK_CAPITAL) And 1 Then
    KeyboardBuffer(VK_CAPITAL) = 0
    Else
    KeyboardBuffer(VK_CAPITAL) = 1
    End If
    SetKeyboardState KeyboardBuffer(0)

  5. #5
    Junior Member
    Join Date
    Jun 2001
    Posts
    30
    I could not get the API calls:
    GetKeyboardState
    and
    SetKeyboardState
    to work. However, the following code seems to work a treat.

    ' Declare the API function and associated constant
    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 GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

    Private Const VK_NUMLOCK = &H90 Private Const KEYEVENTF_EXTENDEDKEY = &H1 Private Const KEYEVENTF_KEYUP = &H2

    ' Now use the follwing code:

    If GetKeyState(VK_NUMLOCK) Then
    Else
    keybd_event VK_NUMLOCK, 0, 0, 0
    End If

  6. #6
    New Member
    Join Date
    Mar 2007
    Posts
    11

    Re: Can you turn on NumLock from VB ?

    This does not work correctly as it sends a Key Down code but not a keyup code. So if the user presses the Num Lock key it does not work the first time. This code works better (as expected)

    keybd_event VK_NUMLOCK, 0, 0, 0 'send NumLock keydown
    keybd_event VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0 'send numlock keyup

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