none
Toggle capslock

    Question

  • How can I toggle capslock? Visual basic 2005. Windows XP. I'd like to capslock on when the form load. This code doesn't work:

    Public Class Form1

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 1, 0)

    Call keybd_event(System.Windows.Forms.Keys.CapsLock, &H14, 3, 0)

    End Sub

    End Class

    The error is: PinvokeStackImbalance was detected.

    Thursday, September 29, 2005 5:12 PM

Answers

  • Very close! Just change the declare line to:

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    If you find VB6 code that declared Win32 API functions, you need to change Long parameters to Integer to use in VB.Net

    Thursday, September 29, 2005 5:58 PM

All replies

  • Very close! Just change the declare line to:

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    If you find VB6 code that declared Win32 API functions, you need to change Long parameters to Integer to use in VB.Net

    Thursday, September 29, 2005 5:58 PM
  • Thank you very much!!! O God, I even didn't think about variables. Thank you again.

    Thursday, September 29, 2005 6:38 PM
  • YOU ARE A GENIUS>. I TRIED FOR 3 DAYS..

    THANK YOU SO MUCH
    Friday, December 09, 2005 6:47 PM
  • Thank you, thank you so very much!
    Wednesday, February 21, 2007 3:57 PM
  • Hi,

    You could also use.>>

    Sendkeys.Send("{CAPSLOCK}")

    Which is supposed to work but doesn't on my P.C. , i can't see the keyboard led change anyway.

     

    Regards,

    S_DS

     

    Thursday, February 22, 2007 3:09 AM
  • Hi all..

    John i think caps lock , ctrl , shift .. etc that keys are seems not trigger that code i think.. numerical and other keys works well with that code..

    and about my question.. can anybody tell me that..

     how can i toggle caps lock depends on its state..

    like this..

    Public Class Form1
      Public DSet As New DataSet()
      Public Kartvizit_BSource As New BindingSource()
      Public Firma_BSource As New BindingSource()
      Public KartvizitVeriOkuyucu As New OleDbDataAdapter()
      Public FirmaVeriOkuyucu As New OleDbDataAdapter()
      Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
      Private Const VK_CAPITAL As Integer = &H14
      Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1
      Private Const KEYEVENTF_KEYUP As Integer = &H2
    
      Sub CapsLockOn() 'Caps Locku açık duruma getir
    
        GetCapsLockState'(here how can i determine if capslock on or off?)
        if GetCapsLockState = 0(off) then
           keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)
           keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
           'capsLock Toggled..
        EndIf
        
      End Sub
    
    
    
    

    Thanks a Lot..

    Şakir


    1 is Possible 0 is imposible.. That is all of the matter..
    Wednesday, July 28, 2010 2:34 PM
  • Hi again i found my questions answer..

    here code blocks..

    Sub CapsLockOn() 'Enables Caps Lock 
        If Not Control.IsKeyLocked(Keys.CapsLock) Then 'here we determine status of CapsLock
          keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)
          keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
        End If
      End Sub
    
      Sub CapsLockOff() 'Disables Caps Lock
        If Control.IsKeyLocked(Keys.CapsLock) Then 'here we determine status of CapsLock
          keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)
          keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
        End If
      End Sub
    

    have Nice days.. Good works..

    Şakir ÜNLÜ


    1 is Possible 0 is imposible.. That is all of the matter..
    Thursday, July 29, 2010 5:33 AM