6

I think the title explains what I am asking.

I need the SendKeys to finish sending the keys before the DoMouseClick method or my entire program is useless.

// Simulate the keyboard typing the value of 'i'
SendKeys.SendWait(i.ToString());

// Simulate mouse click so the Accept button in-game is clicked
DoMouseClick();

I tried using Thread.Sleep but I was hoping you guys had some better suggestions on how to fix my problem.

DooMLaZyPro
  • 93
  • 1
  • 7
  • We could compute the `SendKeys` in another thread and `Join()` this thread. http://stackoverflow.com/questions/1584062/how-to-wait-for-thread-to-finish-with-net – Arthur Rey Jul 19 '15 at 23:44
  • I thought `SendKeys` did wait before returning? MSDN: _"[Use SendWait to send keystrokes or combinations of keystrokes to the active application **and wait for the keystroke messages to be processed**. You can use this method to send keystrokes to an application and wait for any processes that are started by the keystrokes to be completed. This can be important if the other application must finish before your application can continue.](https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx)"_ –  Jul 20 '15 at 00:25
  • 1
    About half way down the documentation: `Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process. ` – Xenolightning Jul 20 '15 at 00:53
  • 1
    And further down. ```To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.``` Put in your appSettings – Liam Mitchell Jan 27 '18 at 06:40
  • However, testing appears that on win7 using `````` is actually the one that waits. – Liam Mitchell Jan 27 '18 at 06:46

2 Answers2

7

Use Input Simulator. It's much better than SendKeys, and handles the edge cases internally.

Install-Package InputSimulator

var simulator = new InputSimulator();

//simulate what you need
simulator.Keyboard.KeyPress(VirtualKeyCode.VK_I);
simulator.Keyboard.TextEntry(i.ToString());

simulator.Mouse.LeftButtonDoubleClick();
Xenolightning
  • 4,140
  • 1
  • 26
  • 34
0

I am not sure if this helps, but if not critique this post so I can try to help in your situation.

 private void Form1_DoubleClick(object sender, MouseEventArgs e)
    {
      // Send the enter key; since the tab stop of Button1 is 0, this 
      // will trigger the click event.
      SendKeys.SendWait("{ENTER}");
      CallAfterSend();
    }

    private void CallAfterSend()
    {
      MessageBox.Show("Had to hit enter first");
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
      MessageBox.Show("Click here!");
    }
  • 1
    Thanks for posting! but I don't see what difference this would make, this is basically already what my code looks like. – DooMLaZyPro Jul 20 '15 at 00:07
  • So you down voted me for that. That's cheap. Anyways, maybe you should try posting why is doesn't work. That question seems vague to me. It works like it should. How are you trying to use it? – Eric Bowser Jul 20 '15 at 00:46
  • I know, I think I should have researched your question a bit more anyway. I was confused on your problem. – Eric Bowser Jul 21 '15 at 05:38