3

I would like to simulate a key that is being pressed for few seconds and then it was released. You all know the effect of holding key for a moment ;)

I tried something like this:

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    ...

    const uint WM_KEYDOWN = 0x0100;
    const uint WM_KEYUP = 0x0101;
    intKeyCode = 'a';

    ...

    PostMessage(windowPointer, WM_KEYDOWN, (IntPtr)(intKeyCode - 0x020), IntPtr.Zero);
    Thread.Sleep(5000); // Sleep for 5 sec
    PostMessage(windowPointer, WM_KEYUP, (IntPtr)(intKeyCode - 0x020), IntPtr.Zero);

Result is easy to predict. It shows only two letters instead of typing it few times for first 5 seconds.

Does anyone tried something like this?

localhost
  • 93
  • 8
  • I guess this question is duplicate of [this one](http://stackoverflow.com/questions/1777184/simulating-a-keypress-and-keyrelease-in-another-application) not sure, see if that helps. – Sriram Sakthivel May 06 '14 at 18:05
  • Sorry about the title, I am a beginner. @SriramSakthivel: it's not a duplicate. I tested the code, and it does exactly the same was mine. The difference is that I send key inputs directly to window handler, so it can be in the background. – localhost May 06 '14 at 18:15
  • I think that system itself is causing the hold&write effect by sending signals to the process. I will record every key stroke in desired application and tell you how it works. – localhost May 07 '14 at 14:45

0 Answers0