How to send keys Control + A + B? (keep Control modifier "pressed")
Asked Answered
C

5

6

When I record this sequence it fails. I know I can send Control + A using Keyboard.SendKeys(control, "A",ModifierKeys.Control) but how do I send a sequence that holds control and releases the letter before pressing the next letter.

Note: the sequence I am looking for is similar to the default Visual Studio shortcut for commenting out a line Control + K + C

Is this maybe something that I just need to use the WinApi for?

Cle answered 5/6, 2012 at 18:9 Comment(2)
Trick question! The modifier doesn't need to remain pressed in this particular case :) Try Ctrl+K Ctrl+C in VS. However, I can imagine cases where releasing Control would trigger an event/reset so this still stands as a valid questions...Inessential
Apparently Visual Studio accepts either version. I guess it has just been habit to hold the control key in there. I need to get the control_down a_down, a_up, b_down, control_up b_upCle
S
4

keybd_event is very convenient for this (much easier to use than the "replacement" SendInput).

keybd_event(Keys.Control, MapVirtualKey(Keys.Control, 0), 0, 0);
keybd_event(Keys.A, MapVirtualKey(Keys.A, 0), 0, 0);
keybd_event(Keys.A, MapVirtualKey(Keys.A, 0), KEYEVENTF_KEYUP, 0);
keybd_event(Keys.B, MapVirtualKey(Keys.B, 0), 0, 0);
keybd_event(Keys.B, MapVirtualKey(Keys.B, 0), KEYEVENTF_KEYUP, 0);
keybd_event(Keys.Control, MapVirtualKey(Keys.Control, 0), KEYEVENTF_KEYUP, 0);

If you only ever need to hold down control, alt, and/or shift, check TCS's answer of SendKeys.Send. keybd_event is more powerful and will let you hold down any key, and release in any order.

Soviet answered 5/6, 2012 at 18:15 Comment(2)
I'd go with keybd_event() in C++ (which I usually use) but in C# that means you need p/invoke, and you don't have the "VK_" #defines that you have in .h files. In most cases in C# I think is it easier to use SendKeys, but all in all, keybd_event() is far more flexable...Douse
@TCS: You can use System.Windows.Forms.Keys, since the documentation says "the high-order bits containing the key code (which is the same as a Windows virtual key code)"Soviet
D
7

From what I understand from the SendKeys.Send documentation it would be:

SendKeys.Send("^(KC)")

The following can be found in the remarks:

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

Douse answered 5/6, 2012 at 18:18 Comment(1)
msdn.microsoft.com/en-us/library/…. Check out the remarks section.Douse
S
4

keybd_event is very convenient for this (much easier to use than the "replacement" SendInput).

keybd_event(Keys.Control, MapVirtualKey(Keys.Control, 0), 0, 0);
keybd_event(Keys.A, MapVirtualKey(Keys.A, 0), 0, 0);
keybd_event(Keys.A, MapVirtualKey(Keys.A, 0), KEYEVENTF_KEYUP, 0);
keybd_event(Keys.B, MapVirtualKey(Keys.B, 0), 0, 0);
keybd_event(Keys.B, MapVirtualKey(Keys.B, 0), KEYEVENTF_KEYUP, 0);
keybd_event(Keys.Control, MapVirtualKey(Keys.Control, 0), KEYEVENTF_KEYUP, 0);

If you only ever need to hold down control, alt, and/or shift, check TCS's answer of SendKeys.Send. keybd_event is more powerful and will let you hold down any key, and release in any order.

Soviet answered 5/6, 2012 at 18:15 Comment(2)
I'd go with keybd_event() in C++ (which I usually use) but in C# that means you need p/invoke, and you don't have the "VK_" #defines that you have in .h files. In most cases in C# I think is it easier to use SendKeys, but all in all, keybd_event() is far more flexable...Douse
@TCS: You can use System.Windows.Forms.Keys, since the documentation says "the high-order bits containing the key code (which is the same as a Windows virtual key code)"Soviet
C
1

How about just using

Keyboard.SendKeys(control, "A",ModifierKeys.Control); 
Keyboard.SendKeys(control, "B",ModifierKeys.Control); 

?

Crumble answered 5/6, 2012 at 18:12 Comment(2)
I believe that ought to work in the VS case, but some applications might reset the sequence on "key up"....Inessential
The application I am working with cycles when the control key is released.Cle
N
1

This worked for me:

Keyboard.SendKeys("^(AB)"); 
Normative answered 24/3, 2017 at 10:40 Comment(0)
Y
0

Simply use the code like below:

Keyboard.SendKeys("^AB"); 
Yurik answered 30/9, 2015 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.