Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 209
stoj Avatar asked Jun 05 '12 18:06

stoj


4 Answers

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".

like image 112
TCS Avatar answered Oct 19 '22 22:10

TCS


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.

like image 38
Ben Voigt Avatar answered Oct 19 '22 23:10

Ben Voigt


How about just using

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

?

like image 21
Beska Avatar answered Oct 19 '22 23:10

Beska


This worked for me:

Keyboard.SendKeys("^(AB)"); 
like image 39
Kitti Avatar answered Oct 19 '22 23:10

Kitti