wpf 我如何关闭大写锁定键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13623245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:24:56  来源:igfitidea点击:

How do i Turn OFF the Caps lock key

c#wpf

提问by Bülent Ala?am

How do i Turn OFF the Caps lock key in textbox. I am using WPF forms.

如何关闭文本框中的大写锁定键。我正在使用 WPF 表单。

When textbox is focused I want to turn off caps lock.

当文本框聚焦时,我想关闭大写锁定。

Thanks

谢谢

回答by Sunny

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

最后,在 textBox_Enter 事件添加此代码

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!

此代码将关闭 Capslock .. 我在输入事件中使用过它,您可以根据需要添加它!

Checkout this link here

在此处查看此链接

回答by Bülent Ala?am

Use this code for WPF froms.

将此代码用于 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);
        }

    }