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
2 个解决方案
#1
9
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 ..我在enter事件中使用它,您可以根据您的要求添加它!
Checkout this link here
在这里查看此链接
#2
2
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);
}
}
#1
9
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 ..我在enter事件中使用它,您可以根据您的要求添加它!
Checkout this link here
在这里查看此链接
#2
2
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);
}
}