3

I have the code below to change the state of the Caps Lock key when the application starts.

I'd like to change the Caps Lock state to ON when I start the application (if it's already ON, then it should remain ON). When the application closes, the Caps Lock state should change to OFF. Any advice for how to achieve this?

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
        UIntPtr dwExtraInfo);



        public Form1()
        {
            InitializeComponent();

            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)1);
}
JDB
  • 25,172
  • 5
  • 72
  • 123

1 Answers1

1

Take a look at this post for the API for discovering whether caps lock is on or not: -

How can I find the state of NumLock, CapsLock and ScrollLock in .net?

See example for comment: -

AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnExit);

public void OnExit(object sender, EventArgs e)
{
    // check and turn caps off if neccessary
}
Community
  • 1
  • 1
ed_me
  • 3,338
  • 2
  • 24
  • 34
  • OK @chrisw69 but what about when i close my application capslock still On plz help me new on c# – Muhammad Azeem Khan Feb 20 '13 at 20:17
  • You could register an event that would check whether caps lock is on using the above suggestion and then turn caps lock off using your original code. – ed_me Feb 20 '13 at 20:49
  • this method is working, i put a message box that is displayed when application closes.But capslock still ON.plz tell me exact coding for it.because above one is not working when closes the application. Thanks – Muhammad Azeem Khan Feb 21 '13 at 11:44