0

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.

AGB
  • 2,230
  • 1
  • 14
  • 21
Damian
  • 761
  • 1
  • 9
  • 26
  • Have you tried SendKeys.Send("{ENTER}"); ? I found it here if that helps: https://msdn.microsoft.com/en-us/library/ms171548(v=vs.110).aspx – Dr. Aaron Dishno Apr 21 '16 at 22:21
  • gives me break error, i can only use SendKeys.SendWait ... and it is just waiting, i found out that the enter command is sent, i ALT + TAB as soon as the script end, and i see enters going, but not in that ActiveX RDP Panel ... any ideea? – Damian Apr 21 '16 at 22:47
  • Perhaps this could be something? https://msdn.microsoft.com/en-us/library/windows/desktop/aa381294(v=vs.85).aspx – Visual Vincent Apr 22 '16 at 07:08
  • i don't quite understand, i use `IMsRdpClientAdvancedSettings5` not `IMsRdpClientNonScriptable` ... – Damian Apr 22 '16 at 12:31
  • I've never used the OCX library, so I just gave you what seemed to be something... I don't know which classes go together. :/ – Visual Vincent Apr 23 '16 at 14:11

1 Answers1

0

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
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • i tried, still don't work, i found out that the enter command is sent, i ALT + TAB as soon as the script end, and i see enters going, but not in that ActiveX RDP Panel ... any ideea? – Damian Apr 21 '16 at 22:47
  • @Damian : I included the enter key in my code... But how do you mean? How do you know it doesn't work? If the panel is focused it should send the key press to it. – Visual Vincent Apr 22 '16 at 06:59
  • i believe it is focused, i mean if i press enter from keyboard, it is openning the cmd, but if i do it from the code nothing happens, see here http://pastebin.com/yHR8tsSK , what do you think? – Damian Apr 22 '16 at 12:28
  • @Damian : Don't know... Perhaps try using SendMessage on the control? Take this for example: http://stackoverflow.com/questions/3047375/simulating-key-press-c-sharp – Visual Vincent Apr 22 '16 at 13:38
  • this is how i use it http://pastebin.com/Fq7e99S2 , i cannot find a solution, if i press enter from the keyboard, it works, but not from code it seems ... – Damian Apr 23 '16 at 22:28
  • So try `SendMessage`, I've already seen that piece of code. :) – Visual Vincent Apr 24 '16 at 08:48
  • where should i put `static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);` function and what parameter should i use? sorry, don't quite understand, thanks – Damian Apr 25 '16 at 00:19
  • The `static extern` part is the declaration, so that should go outside any method (just like keybd_event). As for the usage you can find that in the answer of the question I linked you to. He's using the F5 key so you gotta use the key code for enter. It can be found [here](http://www.pinvoke.net/default.aspx/Enums/VirtualKeys.html) under the name `Return`. – Visual Vincent Apr 25 '16 at 06:12
  • **`hWnd`**: window/control handle to send the keypress to (can be obtained via `.Handle`). **`wParam`**: (here) the key to be pressed, look up the keycode for `Return` in the link i referred to in my latest comment. **`lParam`**: Unused, set to `0` (zero). --- I don't know if the `SendMessage` method will work, but it's worth a shot. – Visual Vincent Apr 25 '16 at 22:03