7

How do i Turn OFF the Caps lock key in textbox. I am using WPF forms.

When textbox is focused I want to turn off caps lock.

Thanks

Bülent Alaçam
  • 187
  • 1
  • 1
  • 11
  • 2
    even google with 'programmatically turn off caps lock c#' gives first answer in stack overflow. – Gustav Klimt Nov 29 '12 at 10:08
  • Maybe this could help you: [http://stackoverflow.com/questions/10534664/how-do-i-turn-on-off-the-caps-lock-key][1] [1]: http://stackoverflow.com/questions/10534664/how-do-i-turn-on-off-the-caps-lock-key – Alex Filipovici Nov 29 '12 at 10:08
  • 1
    @user1811846 Have a heart, at least save the trouble of OP googling it themselves :) http://stackoverflow.com/questions/7962211/programatically-disable-caps-lock – Rotem Nov 29 '12 at 10:11
  • 1
    Do you actually want to turn off the caps lock key, or do you want to enforce a lower case string? – RichK Nov 29 '12 at 13:20

2 Answers2

14

Its easy , Firstly add namespace

using System.Runtime.InteropServices;

then declare this in the class

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);

Finally , at textBox_Enter event add this code

private void textBox1_Enter(object sender, EventArgs e)
    {
        if (Control.IsKeyLocked(Keys.CapsLock)) // Checks Capslock is on
        {
            const int KEYEVENTF_EXTENDEDKEY = 0x1;
            const int KEYEVENTF_KEYUP = 0x2;
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
            (UIntPtr)0);
        }
    }

this code will turn off the Capslock .. I have used it at the enter event you can add it according to your requirement!

Checkout this link here

Sunny
  • 618
  • 1
  • 7
  • 22
3

Use this code for WPF froms.

private void txt_KeyDown(object sender, KeyEventArgs e)
    {

        if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled) // Checks Capslock is on
        {
            const int KEYEVENTF_EXTENDEDKEY = 0x1;
            const int KEYEVENTF_KEYUP = 0x2;
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
            (UIntPtr)0);
        }

    }
Bülent Alaçam
  • 187
  • 1
  • 1
  • 11