matt0667
matt0667

Reputation: 21

SendKey command working with vbs but not C#

I am trying to send Ctrl+Alt+Left from a windows form button (C#) to rotate my screen. It is an intel hotkey. When I press the button nothing is happening, but if I use almost the same command in a vbs file it works.

This is the c# code which does not work:

private void button1_Click(object sender, EventArgs e)
{
    SendKeys.Send("^%{LEFT}");
}

vb script which does work:

Set objShell = CreateObject("WScript.Shell") 
objShell.SendKeys "^%{LEFT}"

I'm very new to all this so maybe I'm missing something obvious?

Upvotes: 2

Views: 987

Answers (1)

Paolo
Paolo

Reputation: 2254

maybe is a parenthesis issue:

SendKeys.Send("^(%{LEFT})");

you can find more details in the documentation where is stated that:

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

not sure about the behaviour of VBS: i would expect the very same behavior of C# because the VBS documentation for SendKeys say so...

Upvotes: 1

Related Questions