문제

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);
}
도움이 되었습니까?

해결책

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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top