简体   繁体   中英

Emulating keyboard enter key in C#

I am trying to emulate various tasks in Win7 and i have some problems with this function :

LeftMouseClick(Cursor.Position.X - 720, Cursor.Position.Y - 45);
System.Threading.Thread.Sleep(1000);

// Simulate each key stroke
InputSimulator.SimulateKeyDown(VirtualKeyCode.RETURN);
InputSimulator.SimulateKeyUp(VirtualKeyCode.RETURN);

InputSimulator.SimulateTextEntry("cmd");
System.Threading.Thread.Sleep(1000);


InputSimulator.SimulateKeyDown(VirtualKeyCode.RETURN);
InputSimulator.SimulateKeyUp(VirtualKeyCode.RETURN);

All i want to do is to press START, write cmd, hit enter. All works smooth except hitting the enter key.

All of this are happening on a RDP ActiveX, here is the code :

var client = (IMsRdpClient7)rdp.GetOcx();
    // client.RemoteProgram2.RemoteProgramMode = true;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).DisplayConnectionBar = true;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).ConnectionBarShowPinButton = true;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).BitmapVirtualCache32BppSize = 48;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).ConnectionBarShowRestoreButton = false;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).ConnectionBarShowMinimizeButton = true;

((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).EnableWindowsKey = 1;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).GrabFocusOnConnect = true;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).RedirectDrives = true;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).RedirectClipboard = true;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).RedirectPrinters = true;
((MSTSCLib.IMsRdpClientAdvancedSettings5)rdp.AdvancedSettings).RedirectPOSDevices = true;

rdp.Server = "1.2.3.4";
rdp.UserName = "Rmlabuser2";
IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = "Rmlabuser2";
// rdp.FullScreenTitle = "Full Screen";
// rdp.SecuredSettings.FullScreen = 1;
// rdp.SecuredSettings.StartProgram = "calc";
rdp.Connect();

I repeat, keys works, i cannot hit enter.

Thanks.

You could try P/Invoking Windows's keybd_event method instead.

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
int dwExtraInfo);

const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
const uint KEYEVENTF_KEYUP = 0x0002;

Then when you want to press it:

keybd_event((byte)System.Windows.Forms.Keys.Enter, 0x45, KEYEVENTF_EXTENDEDKEY, 0); //Key down
keybd_event((byte)System.Windows.Forms.Keys.Enter, 0x45, KEYEVENTF_EXTENDEDKEY |KEYEVENTF_KEYUP, 0); //Key up

For anyone finding this, I was also having the same problem. The only solution that worked for me was answered by Mitch here: Can't send a single key function to remote desktop

In short, send the characters directly using the KEYEVENTF_UNICODE flag. Not all characters are representable as Virtual Keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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