7

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
HeWhoWas
  • 601
  • 1
  • 10
  • 22

2 Answers2

12

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");
ojlovecd
  • 4,812
  • 1
  • 20
  • 22
  • I just tested this, and it doesn't change the state of the CapsLock key at all. With the other method, it would turn the CAPS LED off, however this one doesn't even produce that. – HeWhoWas Nov 01 '11 at 19:56
  • Let me redact that statement. Using the code exactly as you wrote it solved the problem. I was calling SendKeys.SendWait("{CAPSLOCK}"); and then sending the string on another line. The key is in having the CAPSLOCK toggle in the same SendKeys statement as the rest of the text you're sending. Thanks! – HeWhoWas Nov 01 '11 at 20:39
  • The reason is that a CAPSLOCK in a Sendkey causes an extra CAPSLOCK to be sent AFTER the first Sendkey is completed. Add some Debug writes to see what happens. – Martin Feb 10 '18 at 15:18
0

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
Martin
  • 1,430
  • 10
  • 19