• how to turn on caps lock in c#

    how to turn on caps lock in c#

    how to turn on caps lock in c#

     

    1. Firstly add namespace and DllImport!!

     

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

     

    2. Make a function with the code below.

    private void capslock_on_off(string on_off = "on")
    {
    const int KEYEVENTF_EXTENDEDKEY = 0x1;
    const int KEYEVENTF_KEYUP = 0x2;
    
    //on
    if (on_off.ToLower().Equals("on"))
    {
    if (Control.IsKeyLocked(Keys.CapsLock))
    {
    
    }
    else
    {
    keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
    keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
    }
    }
    else
    {
    //off
    if (Control.IsKeyLocked(Keys.CapsLock)) 
    {
    keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
    keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
    }
    }
    
    }

     

    3. How to code using code!!

    Invoke a function by putting lowercase and upper case options into the Enter, TextChanged event each respectively.

     capslock_on_off("off");