Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i Turn OFF the Caps lock key

Tags:

c#

wpf

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

like image 701
Bülent Alaçam Avatar asked Nov 29 '12 10:11

Bülent Alaçam


People also ask

Why is my keyboard typing caps when Caps Lock is off?

This is a case of the default keys in Microsoft being changed. To fix them, do the following: Press both SHIFT KEYS at the same time. This will cancel the change in the keyboard configuration.

Why won't Caps Lock turn off?

You can fix this by going to Settings > Language > Keyboard. Under the Keyboard setting, find the Input language hotkeys at the prompt under the “Advanced Key Settings” make sure to turn off Caps Lock is selected with Press the CAPS LOCK key instead of Press the SHIFT key.

How do I turn my Caps Lock back to normal?

The Caps Lock reversed issue can also be fixed via a shortcut. Press Ctrl + Shift while you pressing the Caps Lock button. The Caps Lock will work properly after pressing this combination of keys again.


2 Answers

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

like image 59
Sunny Avatar answered Oct 13 '22 13:10

Sunny


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);
        }

    }
like image 26
Bülent Alaçam Avatar answered Oct 13 '22 12:10

Bülent Alaçam