Johannes
Johannes

Reputation: 440

Sending keys fails - Window active

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);

Upvotes: 1

Views: 113

Answers (0)

Related Questions