Turn Num Lock, Caps Lock keys on and off with VB6


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Turn Num Lock, Caps Lock keys on and off with VB6

  1. #1
    Chris Huynh Guest

    Turn Num Lock, Caps Lock keys on and off with VB6


    Hi,
    Could anyone know how to programmatically turn the num lock and caps lock
    keys on and off using VB6 ?

    Thanks in advance

    Chris

  2. #2
    Larry Rebich Guest

    Re: Turn Num Lock, Caps Lock keys on and off with VB6

    From VB then help file:

    ----------------------------------------------
    HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
    ID: Q177674


    ----------------------------------------------------------------------------
    ----
    The information in this article applies to:

    a.. Microsoft Visual Basic Learning, Professional, and Enterprise Editions
    for Windows, versions 5.0, 6.0
    b.. Microsoft Visual Basic Standard, Professional, and Enterprise
    Editions, 32-bit only, for Windows, version 4.0

    ----------------------------------------------------------------------------
    ----


    SUMMARY
    This article demonstrates how to toggle the NUM LOCK, CAPS LOCK, AND SCROLL
    LOCK keys under Windows 95, Windows 98, Windows NT, or Windows 2000.



    MORE INFORMATION
    To toggle the NUM LOCK, CAPS LOCK, or SCROLL LOCK keys, you can use the
    following logic:


    a.. Use the GetKeyboardState function to determine the state of the key.


    b.. Determine which operating system is being used with the GetVersionEx
    API. (Windows 95/98 and Windows NT/2000 require different methods for
    toggling these keys.)


    c.. Under Windows 95 or Windows 98, use the SetKeyboardState API function
    to set the state of the key. Under Windows NT or Windows 2000, use the
    keybd_event function to simulate a key press.



    This example shows how to toggle these three keys to "on" if they are "off."
    This sample may be easily modified to toggle them off or just to check their
    state.

    Sample Project
    1.. Start a new Standard EXE project in Visual Basic. Form1 is created by
    default.


    2.. Add a CommandButton to Form1.


    3.. Add the following code to the General Declarations section of Form1:



    ' 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
    Const VK_SCROLL = &H91
    Const VK_CAPITAL = &H14
    Const KEYEVENTF_EXTENDEDKEY = &H1
    Const KEYEVENTF_KEYUP = &H2
    Const VER_PLATFORM_WIN32_NT = 2
    Const VER_PLATFORM_WIN32_WINDOWS = 1

    4.. Add the following code to the Click event of the CommandButton:



    Private Sub Command1_Click()
    Dim o As OSVERSIONINFO
    Dim NumLockState As Boolean
    Dim ScrollLockState As Boolean
    Dim CapsLockState As Boolean

    o.dwOSVersionInfoSize = Len(o)
    GetVersionEx o
    Dim keys(0 To 255) As Byte
    GetKeyboardState keys(0)

    ' NumLock handling:
    NumLockState = keys(VK_NUMLOCK)
    If NumLockState <> True Then 'Turn numlock on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98

    keys(VK_NUMLOCK) = 1
    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 If

    ' CapsLock handling:
    CapsLockState = keys(VK_CAPITAL)
    If CapsLockState <> True Then 'Turn capslock on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
    keys(VK_CAPITAL) = 1
    SetKeyboardState keys(0)
    ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
    'Simulate Key Press
    keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    'Simulate Key Release
    keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
    Or KEYEVENTF_KEYUP, 0
    End If
    End If

    ' ScrollLock handling:
    ScrollLockState = keys(VK_SCROLL)
    If ScrollLockState <> True Then 'Turn Scroll lock on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
    keys(VK_SCROLL) = 1
    SetKeyboardState keys(0)
    ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
    'Simulate Key Press
    keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    'Simulate Key Release
    keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY _
    Or KEYEVENTF_KEYUP, 0
    End If
    End If
    End Sub

    5.. Press the F5 key to run the program. Click the CommandButton. The
    state of the CAPS LOCK, the NUM LOCK, and the SCROLL LOCK keys should all be
    "on."





    REFERENCES
    For additional information, please see the following articles in the
    Microsoft Knowledge Base:


    Q127190 : HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys



    Additional query words: numlock capslock scrolllock key state kbKeyboard

    Keywords : kbSDKWin32 kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpVB kbDSupport
    Version : WINDOWS:4.0,5.0,6.0
    Platform : WINDOWS
    Issue type : kbhowto
    Technology :


    Last Reviewed: April 19, 2000
    2000 Microsoft Corporation. All rights reserved. Terms of Use.




    ----------------------------------------------------------------------------
    ----
    Send feedback to MSDN.Look here for MSDN Online resources.

    ----------------------------------------------

    Cheers,
    Larry Rebich

    More tips link to:
    http://www.buygold.net/tips.html

    Please:
    No personal e-mail questions :-)


    "Chris Huynh" <chris.huynh@norwich-union.com.au> wrote in message
    news:3a9de3b8$1@news.devx.com...
    >
    > Hi,
    > Could anyone know how to programmatically turn the num lock and caps lock
    > keys on and off using VB6 ?
    >
    > Thanks in advance
    >
    > Chris




  3. #3
    Larry Rebich Guest

    Re: Turn Num Lock, Caps Lock keys on and off with VB6

    From VB then help file:

    ----------------------------------------------
    HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
    ID: Q177674


    ----------------------------------------------------------------------------
    ----
    The information in this article applies to:

    a.. Microsoft Visual Basic Learning, Professional, and Enterprise Editions
    for Windows, versions 5.0, 6.0
    b.. Microsoft Visual Basic Standard, Professional, and Enterprise
    Editions, 32-bit only, for Windows, version 4.0

    ----------------------------------------------------------------------------
    ----


    SUMMARY
    This article demonstrates how to toggle the NUM LOCK, CAPS LOCK, AND SCROLL
    LOCK keys under Windows 95, Windows 98, Windows NT, or Windows 2000.



    MORE INFORMATION
    To toggle the NUM LOCK, CAPS LOCK, or SCROLL LOCK keys, you can use the
    following logic:


    a.. Use the GetKeyboardState function to determine the state of the key.


    b.. Determine which operating system is being used with the GetVersionEx
    API. (Windows 95/98 and Windows NT/2000 require different methods for
    toggling these keys.)


    c.. Under Windows 95 or Windows 98, use the SetKeyboardState API function
    to set the state of the key. Under Windows NT or Windows 2000, use the
    keybd_event function to simulate a key press.



    This example shows how to toggle these three keys to "on" if they are "off."
    This sample may be easily modified to toggle them off or just to check their
    state.

    Sample Project
    1.. Start a new Standard EXE project in Visual Basic. Form1 is created by
    default.


    2.. Add a CommandButton to Form1.


    3.. Add the following code to the General Declarations section of Form1:



    ' 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
    Const VK_SCROLL = &H91
    Const VK_CAPITAL = &H14
    Const KEYEVENTF_EXTENDEDKEY = &H1
    Const KEYEVENTF_KEYUP = &H2
    Const VER_PLATFORM_WIN32_NT = 2
    Const VER_PLATFORM_WIN32_WINDOWS = 1

    4.. Add the following code to the Click event of the CommandButton:



    Private Sub Command1_Click()
    Dim o As OSVERSIONINFO
    Dim NumLockState As Boolean
    Dim ScrollLockState As Boolean
    Dim CapsLockState As Boolean

    o.dwOSVersionInfoSize = Len(o)
    GetVersionEx o
    Dim keys(0 To 255) As Byte
    GetKeyboardState keys(0)

    ' NumLock handling:
    NumLockState = keys(VK_NUMLOCK)
    If NumLockState <> True Then 'Turn numlock on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98

    keys(VK_NUMLOCK) = 1
    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 If

    ' CapsLock handling:
    CapsLockState = keys(VK_CAPITAL)
    If CapsLockState <> True Then 'Turn capslock on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
    keys(VK_CAPITAL) = 1
    SetKeyboardState keys(0)
    ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
    'Simulate Key Press
    keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    'Simulate Key Release
    keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
    Or KEYEVENTF_KEYUP, 0
    End If
    End If

    ' ScrollLock handling:
    ScrollLockState = keys(VK_SCROLL)
    If ScrollLockState <> True Then 'Turn Scroll lock on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
    keys(VK_SCROLL) = 1
    SetKeyboardState keys(0)
    ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then '=== WinNT
    'Simulate Key Press
    keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    'Simulate Key Release
    keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY _
    Or KEYEVENTF_KEYUP, 0
    End If
    End If
    End Sub

    5.. Press the F5 key to run the program. Click the CommandButton. The
    state of the CAPS LOCK, the NUM LOCK, and the SCROLL LOCK keys should all be
    "on."





    REFERENCES
    For additional information, please see the following articles in the
    Microsoft Knowledge Base:


    Q127190 : HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys



    Additional query words: numlock capslock scrolllock key state kbKeyboard

    Keywords : kbSDKWin32 kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpVB kbDSupport
    Version : WINDOWS:4.0,5.0,6.0
    Platform : WINDOWS
    Issue type : kbhowto
    Technology :


    Last Reviewed: April 19, 2000
    2000 Microsoft Corporation. All rights reserved. Terms of Use.




    ----------------------------------------------------------------------------
    ----
    Send feedback to MSDN.Look here for MSDN Online resources.

    ----------------------------------------------

    Cheers,
    Larry Rebich

    More tips link to:
    http://www.buygold.net/tips.html

    Please:
    No personal e-mail questions :-)


    "Chris Huynh" <chris.huynh@norwich-union.com.au> wrote in message
    news:3a9de3b8$1@news.devx.com...
    >
    > Hi,
    > Could anyone know how to programmatically turn the num lock and caps lock
    > keys on and off using VB6 ?
    >
    > Thanks in advance
    >
    > Chris




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center