Results 1 to 5 of 5

Thread: Caps Lock On

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    102

    Caps Lock On

    hi

    Form load Caps Lock turn on....
    How to do?
    plzz help

  2. #2
    New Member
    Join Date
    Nov 2005
    Posts
    13

    Re: Caps Lock On

    Found these links about caps lock. C++ example seems to show how to turn it on, but only c# example was more about monitoring the state


    C++ Turn Caps Lock On
    http://www.greatis.com/delphicb/tips...apslockon.html

    Monitor Key States
    http://www.codecomments.com/archive2...-5-489956.html

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Caps Lock On

    I'm not sure how to toggle Caps Lock for the system, but the following code will control it for your app specifically:
    VB Code:
    1. Private Const VK_CAPITAL As Integer = 20
    2.  
    3.     Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (ByRef pbKeyState As Byte) As Integer
    4.     Declare Function SetKeyboardState Lib "user32" Alias "SetKeyboardState" (ByVal lppbKeyState As Byte()) As Integer
    5.  
    6.     Private Sub onButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles onButton.Click
    7.         Dim keyState(255) As Byte
    8.  
    9.         Me.GetKeyboardState(keyState(0))
    10.         keyState(VK_CAPITAL) = 1
    11.         Me.SetKeyboardState(keyState)
    12.     End Sub
    13.  
    14.     Private Sub offButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles offButton.Click
    15.         Dim keyState(255) As Byte
    16.  
    17.         Me.GetKeyboardState(keyState(0))
    18.         keyState(VK_CAPITAL) = 0
    19.         Me.SetKeyboardState(keyState)
    20.     End Sub
    That code will not change the Caps Lock light on your keyboard or affect any other apps, but it will turn Caps Lock on and off in your app. I would have thought that simply SendKeys.Send("{CAPSLOCK}") would have done the job of toggling Caps Lock for the system but apparently not.
    Last edited by jmcilhinney; Dec 22nd, 2005 at 07:25 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Caps Lock On

    Quote Originally Posted by jmcilhinney
    I'm not sure how to toggle Caps Lock for the system, but the following code will control it for your app specifically:
    VB Code:
    1. Private Const VK_CAPITAL As Integer = 20
    2.  
    3.     Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (ByRef pbKeyState As Byte) As Integer
    4.     Declare Function SetKeyboardState Lib o"user32" Alias "SetKeyboardState" (ByVal lppbKeyState As Byte()) As Integer
    5.  
    6.     Private Sub onButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles onButton.Click
    7.         Dim keyState(255) As Byte
    8.  
    9.         Me.GetKeyboardState(keyState(0))
    10.         keyState(VK_CAPITAL) = 1
    11.         Me.SetKeyboardState(keyState)
    12.     End Sub
    13.  
    14.     Private Sub offButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles offButton.Click
    15.         Dim keyState(255) As Byte
    16.  
    17.         Me.GetKeyboardState(keyState(0))
    18.         keyState(VK_CAPITAL) = 0
    19.         Me.SetKeyboardState(keyState)
    20.     End Sub
    That code will not change the Caps Lock light on your keyboard or affect any other apps, but it will turn Caps Lock on and off in your app. I would have thought that simply SendKeys.Send("{CAPSLOCK}") would have done the job of toggling Caps Lock for the system but apparently not.
    This would work in C#?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Caps Lock On

    Oops. I forget I'm in the C# forum sometimes. Here's the direct translation, which I've tested and it works:
    Code:
    private const int VK_CAPITAL = 0x14;
    
    [DllImport("user32.dll")]
    private static extern int GetKeyboardState(ref byte pbKeyState);
    [DllImport("user32.dll")]
    private static extern int SetKeyboardState(byte[] lppbKeyState);
    
    private void onButton_Click(object sender, System.EventArgs e)
    {
        byte[] keyState = new byte[256];
    
        GetKeyboardState(ref keyState[0]);
        keyState[VK_CAPITAL] = 1;
        SetKeyboardState(keyState);
    }
    
    private void offButton_Click(object sender, System.EventArgs e)
    {
        byte[] keyState = new byte[256];
    
        GetKeyboardState(ref keyState[0]);
        keyState[VK_CAPITAL] = 0;
        SetKeyboardState(keyState);
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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