Results 1 to 12 of 12

Thread: [2008] turn on off caps lock

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Resolved [2008] turn on off caps lock

    Banging my head all morning about this, as seems to be something you cannot do without calling windows api functions.
    Have scraped over the internet and come up with this

    h Code:
    1. Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    2.         SetKeyState(System.Windows.Forms.Keys.CapsLock, True)
    3.  
    4.     End Sub
    5.  
    6.     Private Declare Auto Function SetKeyboardState Lib "user32.dll" _
    7.       (ByVal lppbKeyState As Byte) As Long
    8.     Private Declare Auto Function GetKeyState Lib "user32.dll" _
    9.       (ByVal nVirtKey As Long) As Boolean
    10.     Private Declare Auto Function GetKeyboardState Lib "user32.dll" _
    11.         (ByVal pbKeyState As Byte) As Long
    12.  
    13.      Private Sub SetKeyState(ByVal Key As Long, ByVal State As Boolean)
    14.  
    15.         Dim Keys(0 To 255) As Byte
    16.         Call GetKeyboardState(Keys(0))
    17.         Keys(Key) = Math.Abs(CInt(State))
    18.         Call SetKeyboardState(Keys(0))
    19.  
    20.     End Sub

    But it doesn't work and am well out of my depth.
    I just want to check if caps lock is pressed and if so turn it off.
    Any help would be greatly appreciated.
    Last edited by Oliver1; Sep 1st, 2008 at 05:53 AM. Reason: esolved

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] turn on off caps lock

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form2
    4.  
    5.     Private Declare Sub keybd_event Lib "user32" ( _
    6.         ByVal bVk As Byte, _
    7.         ByVal bScan As Byte, _
    8.         ByVal dwFlags As Integer, _
    9.         ByVal dwExtraInfo As Integer _
    10.     )
    11.     Private Const VK_CAPITAL As Integer = &H14
    12.     Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
    13.     Private Const KEYEVENTF_KEYUP As Integer = &H2
    14.  
    15.     Private Structure OSVERSIONINFO
    16.         Dim dwOSVersionInfoSize As Integer
    17.         Dim dwMajorVersion As Integer
    18.         Dim dwMinorVersion As Integer
    19.         Dim dwBuildNumber As Integer
    20.         Dim dwPlatformId As Integer
    21.         <VBFixedString(128), MarshalAs(UnmanagedType.ByValArray, SizeConst:=128)> Public szCSDVersion() As Char
    22.     End Structure
    23.  
    24.     Private Sub Button1_Click( _
    25.         ByVal sender As System.Object, _
    26.         ByVal e As System.EventArgs _
    27.     ) Handles Button1.Click
    28.  
    29.         ' Toggle CapsLock
    30.         keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0) ' Simulate Key Press
    31.         keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) ' Simulate Key Release
    32.     End Sub
    33.  
    34. End Class

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: [2008] turn on off caps lock

    Thanks loads for this, will check it out on Monday, as am off for the weekend now.

  4. #4
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2008] turn on off caps lock

    Deepak or you able using that code to simulate any key?

  5. #5

  6. #6
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2008] turn on off caps lock

    Though don't i just replace the &H45? I have found a submission in the codebank by .paul. which retrieves that value of any key you press. As to get &H..

  7. #7
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2008] turn on off caps lock

    The &H denotes that the number is in hex. You can go with the dec version of hex 14 which is 20.
    Private Const VK_CAPITAL As Integer = &H14 is the same as:
    Private Const VK_CAPITAL As Integer = 20

    Take a look at winuser.h - it is a kind of an include in the C++ environment when developing windows programs. It is very possible that you also have it buried somewhere in your computer too.

    The file lists many numeric constants that Windows uses for it's api functions. Whether you are using C++ , VB or whatever, the OS needs those numbers and not their names. In winuser.h scroll down to the '* Virtual Keys, Standard Set' and you'll find
    #define VK_CAPITAL 0x14. As i said 0x denotes hex in C++ just like &H denotes hex in VB. If you need to press the Pause key you take 0x13 or &H13 respectively...or just 19 in dec.
    VB 2005, Win Xp Pro sp2

  8. #8
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] turn on off caps lock

    Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission...
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  9. #9
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2008] turn on off caps lock

    Quote Originally Posted by obi1kenobi
    Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission...
    Ya i would agree.

  10. #10
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] turn on off caps lock

    Quote Originally Posted by obi1kenobi
    Wow, great piece of code Deepak, I think it's worthy of a CodeBank submission...
    Quote Originally Posted by noahssite
    Ya i would agree.
    Thanks . I will submit it into the Code Bank if there is no matching code/thread.

  11. #11
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2008] turn on off caps lock

    You should include the whole code. As it is, the OSVERSIONINFO structure is not needed. It is used to check if the current OS is not from the WinNT family. If that is the case, the keybd_event api won't work. One would have to use the SetKeyboardState api.

    Here is the complete snippet: pinvoke.net
    VB 2005, Win Xp Pro sp2

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: [2008] turn on off caps lock

    Thanks for this, worked a treat.

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