1

I am sending a keystroke (ctrl+c) to an active Windows using InputSimulator To achive this I minimize my own form and set the application i wish to send the key to the foreground using the WIN32 API and SetForegroundWindow. Now this all works on most applications. I however am encoutering a problem with the Windows Mail Application for example. Before sending the keycombo GetForegroundwindow assures me that the mail app is indeed active. The copy command however is not executed. If i wait for 1-2 seconds (using Thread.sleep) before sending the command it works. Does anyone have an idea how to fix this?

Making my form inactive

this.Hide();
this.WindowState = FormWindowState.Minimized; //this seem to be the most reliable way

Setting foreground and pressing keys

if (foreGroundHandle != null && foreGroundHandle != IntPtr.Zero)
SetForegroundWindow(foreGroundHandle);

new InputSimulator().Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
Johannes
  • 441
  • 5
  • 15
  • Are you sure the forgrundhandle is set? – BugFinder Apr 11 '18 at 08:00
  • Yes, in the actual code i checked it using getForegroundwindow right before simulating the key press – Johannes Apr 11 '18 at 08:03
  • Provide a complete minimal viable example then. The above code is not sufficient to do that – BugFinder Apr 11 '18 at 11:56
  • I provided all the relevant information in my description and code. Given that the code works if waiting 1-2 seconds prior to sending the keys using thread.sleep, suggests that the OS is still busy with something regarding the window. – Johannes Apr 11 '18 at 12:25
  • How can it be complete when youve mentioned it only doesnt work in your "windows mail application" which one? how are you capturing the window handle for it? which version of windows are you running? part of the SO requirement is https://stackoverflow.com/help/mcve so that we dont have to guess how your problem works, we can recreate it - I couldnt because I dont have the info. – BugFinder Apr 11 '18 at 12:30
  • There is only one windows mail application. I am capturing the window handle using the Getforegroundwindow. The windows version is not relevant to the question. I already wrote that the active window is the correct one. – Johannes Apr 11 '18 at 13:10
  • As you wont help me help you.. and im the only one whos responded, seems youre on your own. Ps there over 30 mail apps for windows – BugFinder Apr 11 '18 at 22:22

0 Answers0