以编程方式禁用大写锁定

发布于 2024-12-12 20:43:54 字数 886 浏览 6 评论 0原文

我在自动化程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2024-12-19 20:43:56

我有一个应用程序,我经常需要在左 SHIFT 和 TAB 之间切换。在我的键盘上,CAPSLOCK 位于这 2 个键之间,我时不时会错误地输入 CAPSLOCK 而不是 TAB。我的解决方案是反转 CAPSLOCK 并提交 TAB。令我惊讶的是,程序循环直到堆栈溢出。我发现CAPSLOCK键被发送了两次。这是我的最终解决方案:

Dim CapsLockProg As Integer = 0 ' after Send Capslock arrives 2 times!!!!!
Private Sub Description_KeyDown(sender As Object, e As KeyEventArgs) Handles Description.KeyDown 
    If e.KeyCode = Keys.Capital Then
        If CapsLockProg < 2 Then
            CapsLockProg += 1
            If CapsLockProg = 1 Then
                Windows.Forms.SendKeys.SendWait("{TAB}{CAPSLOCK}")
            'Else
            '   ignore 2nd Capslock
            End If 
        Else
            CapsLockProg = 0
        End If
    End If
    If e.KeyCode = Keys.Tab Then 
    rest of code

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:

Dim CapsLockProg As Integer = 0 ' after Send Capslock arrives 2 times!!!!!
Private Sub Description_KeyDown(sender As Object, e As KeyEventArgs) Handles Description.KeyDown 
    If e.KeyCode = Keys.Capital Then
        If CapsLockProg < 2 Then
            CapsLockProg += 1
            If CapsLockProg = 1 Then
                Windows.Forms.SendKeys.SendWait("{TAB}{CAPSLOCK}")
            'Else
            '   ignore 2nd Capslock
            End If 
        Else
            CapsLockProg = 0
        End If
    End If
    If e.KeyCode = Keys.Tab Then 
    rest of code
苹果你个爱泡泡 2024-12-19 20:43:55

这对你有用吗?

    if(Control.IsKeyLocked(Keys.CapsLock))
        SendKeys.SendWait("{CAPSLOCK}This Is An Over Capitalized Test String");
    else
        SendKeys.SendWait("This Is An Over Capitalized Test String");

does this work for you?

    if(Control.IsKeyLocked(Keys.CapsLock))
        SendKeys.SendWait("{CAPSLOCK}This Is An Over Capitalized Test String");
    else
        SendKeys.SendWait("This Is An Over Capitalized Test String");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文