以编程方式禁用大写锁定
我在自动化程序中使用 SendKeys 进行工作。我一直在缓慢地前进,现在正试图消除我所创建的所有错误:-)
其中之一是,当我使用 SendKeys.Send("Test") 时,如果 CapsLock 打开,它将产生“tEST”而不是“Test”。
我使用以下代码尝试检测大写锁定状态,并在必要时切换它:
bool tmp = Control.IsKeyLocked(Keys.CapsLock);
if (tmp)
{
keybd_event(0x14, 0x45, KEYEVENTF_EXTENTEDKEY, (UIntPtr)0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENTEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
//Application.DoEvents(); <-Testing.
}
然后立即使用 SendKeys 发送一些文本:
SendKeys.SendWait("This Is An Over Capitalized Test String");
结果仍然是:“这是一个大写的测试字符串”。
有什么办法可以解决这个问题吗?
回答了!只是为了向其他人澄清,问题是通过使用
SendKeys.SendWait("{CAPSLOCK}" + text);
我第一次尝试使用来解决的:
SendKeys.SendWait("{CAPSLOCK}");
SendKeys.SendWait("This Is An Over Capitalized Test String");
这根本不起作用。
I'm using SendKeys in an automation program for work. I've been plodding along, and am now trying to iron out all the bugs that I've created :-)
One of which, is that when I used SendKeys.Send("Test"), if the CapsLock is on, it will produce "tEST" as opposed to "Test".
I've used the following code to attempt to detect the capsLock state, and toggle it if necessary:
bool tmp = Control.IsKeyLocked(Keys.CapsLock);
if (tmp)
{
keybd_event(0x14, 0x45, KEYEVENTF_EXTENTEDKEY, (UIntPtr)0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENTEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
//Application.DoEvents(); <-Testing.
}
And then immediately use SendKeys to send some text:
SendKeys.SendWait("This Is An Over Capitalized Test String");
Which STILL comes out as: "tHIS iS aN oVER cAPITALIZED tEST sTRING".
Is there any way to get around this problem?
Answered! Just to clarify for anyone else, the problem was resolved by using
SendKeys.SendWait("{CAPSLOCK}" + text);
I first attempted to use:
SendKeys.SendWait("{CAPSLOCK}");
SendKeys.SendWait("This Is An Over Capitalized Test String");
Which did not work at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个应用程序,我经常需要在左 SHIFT 和 TAB 之间切换。在我的键盘上,CAPSLOCK 位于这 2 个键之间,我时不时会错误地输入 CAPSLOCK 而不是 TAB。我的解决方案是反转 CAPSLOCK 并提交 TAB。令我惊讶的是,程序循环直到堆栈溢出。我发现CAPSLOCK键被发送了两次。这是我的最终解决方案:
I have an application, where I frequently need to switch between left-SHIFT and TAB. On my keyboard CAPSLOCK is between those 2 keys and I mistake now and then, typing a CAPSLOCK instead of a TAB. My solution is to reverse CAPSLOCK and submit a TAB instead. To my surprise the program loops until stack-overflow. I found out that the CAPSLOCK-key is send twice. This is my final solution:
这对你有用吗?
does this work for you?