0

I have an application that automatically simulates some keystrokes. I am using following code for this:

public void goNextPage()
{
    WindowsInput.InputSimulator ip = new WindowsInput.InputSimulator();
    ip.Keyboard.ModifiedKeyStroke(new[] { WindowsInput.Native.VirtualKeyCode.CONTROL, WindowsInput.Native.VirtualKeyCode.MENU }, WindowsInput.Native.VirtualKeyCode.VK_Y);
    Thread.Sleep(5000);
}

Input Simulator is an alternative to SendKeys(). I shifted to this because I was having the same problem with SendKeys().

The problem is that sometimes the form on which keys are simulated needs to be minimized by the user. I tried minimizing the form but instead of sending keys to the its owner form, it sends them to the newly activated form or the active application like Visual Studio and Google Chrome. For example: When I started to type something in Chrome's address bar and at the same time my application sends SHIFT key, my typed data shifted to capital letters or symbols on number keys.

So is it possible to send keys to a deactivated form? Or is it against the operating system rules? Can it be overridden?

Aishwarya Shiva
  • 3,460
  • 15
  • 58
  • 107
  • 1
    you need to find the handle of the window and then send a message to that handle – Steve Jan 04 '17 at 21:27
  • 1
    look at SendMessage() https://msdn.microsoft.com/en-us/library/ms644950(VS.85).aspx or at PostMessage() https://msdn.microsoft.com/en-us/library/windows/desktop/ms644944(v=vs.85).aspx – Jacobr365 Jan 04 '17 at 21:29
  • 2
    If you want to set text for a specific window, for example a specific text box in a form, addressbar of browser or the text editor of notepad, you can send `WM_SETTEXT` message to that window. Take a look at [this example](http://stackoverflow.com/a/39706017/3110834). – Reza Aghaei Jan 04 '17 at 21:31
  • @RezaAghaei no I don't want to set text I want to fire a combination of keys like CTRL+Y – Aishwarya Shiva Jan 05 '17 at 07:08
  • I am curious to know the reason of voting -1 – Aishwarya Shiva Jan 05 '17 at 07:09
  • @Steve any example? I will really appreciate a code example. – Aishwarya Shiva Jan 05 '17 at 07:13
  • @Jacobr365 the link is a c++ example. can you give me some sample in c#? – Aishwarya Shiva Jan 05 '17 at 07:15
  • @AishwaryaShiva I don't have any idea about the downvote, unfortunately downvoters used to downvote without comment which is annoying most times. Never mind ... – Reza Aghaei Jan 05 '17 at 07:16
  • 1
    Take a look at [this post](http://stackoverflow.com/q/1220820/3110834). – Reza Aghaei Jan 05 '17 at 07:21
  • @RezaAghaei yeah StackExchange must put a little textbox for reason after someone votes. – Aishwarya Shiva Jan 05 '17 at 07:21
  • @RezaAghaei Thanks for the link. Looks like its not possible. Isn't it? I mean sending the message to window handle might not work too. Its not in normal OS behavior. – Aishwarya Shiva Jan 05 '17 at 07:30
  • They are both in user32. so c++ doesn't matter. this link has the c# signature and some examples. http://www.pinvoke.net/default.aspx/user32.sendmessage – Jacobr365 Jan 05 '17 at 14:00

0 Answers0