Results 1 to 8 of 8

Thread: how can I turn on caps lock?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    how can I turn on caps lock?

    I'm trying to do this, I got a code but sounds like it doesnt work for windows NT. What is a SIMPLE way to do this? I tried SendKeys function too, but for some reason it didn't work
    help needed!!!!

    Thanks in advance

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    when you say SOUNDS LIKE did you try to use the code? or did you base it on your instincts?
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  3. #3
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I know it's easier to ask... but, had you have a look at MSDN??
    HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys
    Last reviewed: December 5, 1997
    Article ID: Q177674
    The information in this article applies to:
    Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
    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 both Windows 95 and Windows NT.



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

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

    - Determine which operating system is being used with the GetVersionEx API.

    (Windows 95 and Windows NT require different methods for toggling these
    keys.)


    - Under Windows 95, use the SetKeyboardState API function to set the state
    of the key. Under Windows NT, 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

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

    Add a CommandButton to Form1.

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

    VB Code:
    1. ' Declare Type for API call:
    2.       Private Type OSVERSIONINFO
    3.         dwOSVersionInfoSize As Long
    4.         dwMajorVersion As Long
    5.         dwMinorVersion As Long
    6.         dwBuildNumber As Long
    7.         dwPlatformId As Long
    8.         szCSDVersion As String * 128   '  Maintenance string for PSS usage
    9.       End Type
    10.  
    11.       ' API declarations:
    12.       Private Declare Function GetVersionEx Lib "kernel32" _
    13.          Alias "GetVersionExA" _
    14.          (lpVersionInformation As OSVERSIONINFO) As Long
    15.  
    16.       Private Declare Sub keybd_event Lib "user32" _
    17.          (ByVal bVk As Byte, _
    18.           ByVal bScan As Byte, _
    19.           ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    20.  
    21.       Private Declare Function GetKeyboardState Lib "user32" _
    22.          (pbKeyState As Byte) As Long
    23.  
    24.       Private Declare Function SetKeyboardState Lib "user32" _
    25.          (lppbKeyState As Byte) As Long
    26.  
    27.       ' Constant declarations:
    28.       Const VK_NUMLOCK = &H90
    29.       Const VK_SCROLL = &H91
    30.       Const VK_CAPITAL = &H14
    31.       Const KEYEVENTF_EXTENDEDKEY = &H1
    32.       Const KEYEVENTF_KEYUP = &H2
    33.       Const VER_PLATFORM_WIN32_NT = 2
    34.       Const VER_PLATFORM_WIN32_WINDOWS = 1


    Add the following code to the Click event of the CommandButton:
    VB Code:
    1. Private Sub Command1_Click()
    2.         Dim o As OSVERSIONINFO
    3.         Dim NumLockState As Boolean
    4.         Dim ScrollLockState As Boolean
    5.         Dim CapsLockState As Boolean
    6.  
    7.         o.dwOSVersionInfoSize = Len(o)
    8.         GetVersionEx o
    9.         Dim keys(0 To 255) As Byte
    10.         GetKeyboardState keys(0)
    11.  
    12.         ' NumLock handling:
    13.         NumLockState = keys(VK_NUMLOCK)
    14.         If NumLockState <> True Then    'Turn numlock on
    15.           If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '===== Win95
    16.             keys(VK_NUMLOCK) = 1
    17.             SetKeyboardState keys(0)
    18.           ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '===== WinNT
    19.           'Simulate Key Press
    20.             keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    21.           'Simulate Key Release
    22.             keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
    23.                Or KEYEVENTF_KEYUP, 0
    24.           End If
    25.         End If
    26.  
    27.         ' CapsLock handling:
    28.         CapsLockState = keys(VK_CAPITAL)
    29.         If CapsLockState <> True Then    'Turn capslock on
    30.           If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '===== Win95
    31.             keys(VK_CAPITAL) = 1
    32.             SetKeyboardState keys(0)
    33.           ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '===== WinNT
    34.           'Simulate Key Press
    35.             keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    36.           'Simulate Key Release
    37.             keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
    38.                Or KEYEVENTF_KEYUP, 0
    39.           End If
    40.         End If
    41.  
    42.         ' ScrollLock handling:
    43.         ScrollLockState = keys(VK_SCROLL)
    44.         If ScrollLockState <> True Then    'Turn Scroll lock on
    45.           If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '===== Win95
    46.             keys(VK_SCROLL) = 1
    47.             SetKeyboardState keys(0)
    48.           ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '===== WinNT
    49.           'Simulate Key Press
    50.             keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    51.           'Simulate Key Release
    52.             keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY _
    53.               Or KEYEVENTF_KEYUP, 0
    54.           End If
    55.         End If
    56.       End Sub


    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:


    ARTICLE-ID: Q127190
    TITLE : HOWTO: Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys




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

    Additional query words: numlock capslock scrolllock key state
    Keywords : vb432 VB4WIN vb5all vb5howto
    Version : WINDOWS:4.0,5.0
    Platform : WINDOWS
    Issue type : kbhowto


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  4. #4
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Ok, how do you turn off the Caps lock, etc via the code?
    Don't Rate my posts.

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Exactly the same... just comment the "If CapsLockState <> True Then" line (and its "End If" and you get a toggle function (the same for Scroll-Lock and Number-Lock
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I believe the lines like "keys(VK_CAPITAL) = 1" should be change to "keys(VK_CAPITAL) = 0" to turn it off.... but don't have Win95 at hand to test it.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    tnx alot, I have tried that setKeyboardState functoin and it wouldnt work and it took me a while to figure out that it doesnt work for win NT but I didnt know what else to use.... tnx again

  8. #8
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Any time... I also learned from that code.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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