简体   繁体   English

以编程方式禁用大写锁定

[英]Programmatically disable caps lock

I'm using SendKeys in an automation program for work. 我在自动化程序中使用SendKeys进行工作。 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". 其中之一就是当我使用SendKeys.Send(“Test”)时,如果CapsLock打开,它将产生“tEST”而不是“Test”。

I've used the following code to attempt to detect the capsLock state, and toggle it if necessary: 我使用以下代码尝试检测capsLock状态,并在必要时切换它:

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发送一些文本:

SendKeys.SendWait("This Is An Over Capitalized Test String");

Which STILL comes out as: "tHIS iS aN oVER cAPITALIZED tEST sTRING". 其中仍然出现:“tHIS不能通过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. 这根本不起作用。

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

I have an application, where I frequently need to switch between left-SHIFT and TAB. 我有一个应用程序,我经常需要在左-SHIFT和TAB之间切换。 On my keyboard CAPSLOCK is between those 2 keys and I mistake now and then, typing a CAPSLOCK instead of a TAB. 在我的键盘上,CAPSLOCK位于这两个键之间,我偶尔会出错,键入CAPSLOCK而不是TAB。 My solution is to reverse CAPSLOCK and submit a TAB instead. 我的解决方案是反转CAPSLOCK并提交TAB。 To my surprise the program loops until stack-overflow. 令我惊讶的是程序循环直到堆栈溢出。 I found out that the CAPSLOCK-key is send twice. 我发现CAPSLOCK键发送了两次。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM