CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2001
    Location
    Trivandrum, Kerala, India
    Posts
    21

    Toggling Caps Lock


    Hai,

    I want to turn on/off the Caps Lock, Num Lock and Scroll Lock in my VB Project. Can anyone help me?

    Thanks in advance

    Kishore


  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Toggling Caps Lock

    The MSDN contains an article

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

    Search the MSDN for it.


  3. #3
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Toggling Caps Lock

    You can use SendKeys to do this:

    SendKeys {CAPSLOCK}, false
    SendKeys {NUMLOCK}, false
    SendKeys {SCROLLOCK}, false




    Tom Cannaerts
    slisse@planetinternet.be

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  4. #4
    Join Date
    Jul 2001
    Posts
    3

    Re: Toggling Caps Lock

    Usually, when dealing with the case of turning on/off certain keys in Visual Basic, I like to use the Select Case. Below is example code for turning on, and off Caps Lock.


    'Turns off Caps lock
    private Sub Text1_Keypress(KeyAscii as Integer)
    Select Case KeyAscii
    Case vbKeyCapital
    KeyAscii = vbEmpty
    End Select
    End Sub




    'Turns on Caps lock
    private Sub Text1_Keypress(KeyAscii as Integer)
    Select Case KeyAscii
    Case vbKeyCapital
    End Select
    End Sub




    As you can see, you can select the case for certain keys. vbKeyNumlock is for Num Lock, etc. This Visual Basic defined variables can be found in the MSDN library under "Key Code Constants." Continuing on the code, in the very first block, you select the case for Caps Lock. Then, you say that the KeyAscii (Keyboard Keys) set to nothing. This is in simple terms, for an easier understanding. In the second block of code, you take the same case of Caps Lock, but you allow the user to use it because you enter no code for that certain case.

    Hoped that cleared up enough for you. If you need more help, feel free to email me.


    Daniel Su
    Laff@lanset.com

    Even though he was an enemy of mine, I had to admit that what he had accomplished was a brilliant piece of strategy. First, he punched me, then he kicked me, then he punched me again.

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Toggling Caps Lock


    Another way from Api-guide gurus:
    Const VK_CAPITAL = &H14
    Const VK_NUMLOCK = &H90
    Const VK_SCROLL = &H91
    Const VK_USED = VK_SCROLL
    private Type KeyboardBytes
    kbByte(0 to 255) as Byte
    End Type
    private Declare Function GetKeyState Lib "user32" (byval nVirtKey as Long) as Long
    private Declare Function GetKeyboardState Lib "user32" (kbArray as KeyboardBytes) as Long
    private Declare Function SetKeyboardState Lib "user32" (kbArray as KeyboardBytes) as Long
    private Declare Sub Sleep Lib "kernel32" (byval dwMilliseconds as Long)
    Dim kbArray as KeyboardBytes, CapsLock as Boolean, kbOld as KeyboardBytes
    private Sub Form_Load()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    'get the current keyboardstate
    GetKeyboardState kbOld
    'Hide the form
    me.Hide
    MsgBox "Keep your eyes on the little num-, shift- and scrolllock lights on the keyboard."
    TurnOff VK_CAPITAL
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 1000
    TurnOn VK_NUMLOCK
    Sleep 100
    TurnOn VK_CAPITAL
    Sleep 100
    TurnOn VK_SCROLL
    Sleep 300
    TurnOff VK_NUMLOCK
    Sleep 100
    TurnOff VK_CAPITAL
    Sleep 100
    TurnOff VK_SCROLL
    Sleep 500
    TurnOn VK_NUMLOCK
    TurnOn VK_SCROLL
    Sleep 200
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 200
    TurnOn VK_NUMLOCK
    TurnOn VK_SCROLL
    Sleep 200
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 200
    TurnOn VK_CAPITAL
    Sleep 200
    TurnOff VK_CAPITAL
    Sleep 200
    TurnOn VK_CAPITAL
    Sleep 200
    TurnOff VK_CAPITAL
    Sleep 200
    TurnOn VK_NUMLOCK
    TurnOn VK_SCROLL
    Sleep 200
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 200
    TurnOn VK_NUMLOCK
    TurnOn VK_SCROLL
    Sleep 200
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 200
    TurnOn VK_CAPITAL
    Sleep 400
    TurnOff VK_CAPITAL
    Sleep 200
    TurnOn VK_NUMLOCK
    Sleep 100
    TurnOn VK_CAPITAL
    Sleep 100
    TurnOn VK_SCROLL
    Sleep 300
    TurnOff VK_SCROLL
    Sleep 100
    TurnOff VK_CAPITAL
    Sleep 100
    TurnOff VK_NUMLOCK
    Sleep 1000
    Unload me
    End Sub
    private Sub TurnOn(vkKey as Long)
    'get the keyboard state
    GetKeyboardState kbArray
    'Change a key
    kbArray.kbByte(vkKey) = 1
    'set the keyboard state
    SetKeyboardState kbArray
    End Sub
    private Sub TurnOff(vkKey as Long)
    'get the keyboard state
    GetKeyboardState kbArray
    'change a key
    kbArray.kbByte(vkKey) = 0
    'set the keyboard state
    SetKeyboardState kbArray
    End Sub
    private Sub Form_Unload(Cancel as Integer)
    'restore the old keyboard state
    SetKeyboardState kbOld
    End Sub





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood,
    Bruno Paris and all the other wonderful people who made and make Codeguru
    a great place.
    Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Toggling Caps Lock

    'module

    Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    Public Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Public Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long


    Function GetCapslock() As Boolean
    ' Return or set the Capslock toggle.
    GetCapslock = CBool(GetKeyState(vbKeyCapital) And 1)

    End Function

    Sub SetCapslock(value As Boolean)
    ' Return or set the Capslock toggle.
    Call SetKeyState(vbKeyCapital, value)
    End Sub

    End Sub


    Private Sub SetKeyState(intKey As Integer, fTurnOn As Boolean)

    Dim abytBuffer(0 To 255) As Byte
    GetKeyboardState abytBuffer(0)
    abytBuffer(intKey) = CByte(Abs(fTurnOn))
    SetKeyboardState abytBuffer(0)
    End Sub


    'form
    Call GetCapslock
    If GetCapslock = True Then
    SetCapslock (False)
    Else
    Exit Sub
    End If

    Call GetCapslock
    If GetCapslock = False Then
    SetCapslock (True)
    Else
    Exit Sub
    End If


    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

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

Featured