Capslock ON&关闭应用程序

时间:2013-02-20 18:36:45

标签: c# capslock

我有以下代码在应用程序启动时更改Caps Lock键的状态。

我想在启动应用程序时将Caps Lock状态更改为ON(如果它已经打开,那么它应该保持为ON)。应用程序关闭时,Caps Lock状态应更改为OFF。有关如何实现这一目标的任何建议吗?

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

1 个答案:

答案 0 :(得分:1)

请查看此帖子以了解用于发现大写锁定是否已启用的API: -

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

参见示例评论: -

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

public void OnExit(object sender, EventArgs e)
{
    // check and turn caps off if neccessary
}