Simulating a keypress AND keyrelease in another ap

2019-01-19 08:09发布

问题:

I need to interact with an external application running, and send specific keypresses & releases. I've tried to use the SendKeys class, but it does only half of the job, as the keypress is being sent with an immediate keyrelease to the external applications.

I need to be able to simulate a "key hold down" for the external app. I'm now trying to use the SendMessage thing, but for now it won't work at all :( and I don't even get errors.

回答1:

Ok, case solved. I actually installed VC++ to try the core keybd_event() function, and after it worked I was able to use it wisely in C#.

Here's the code, and surprisingly it's very simple. You'll need to add this using to your code to be able to import dll's: using System.Runtime.InteropServices;

This code will press and hold the '1' button for 3 secs, and then will release for 1 second and repeat the process.

(the code highlight got messed up :/, copy from 'namespace ...' to the last bracket '}')

namespace ConsoleApplication1 
{
class Program 
{ 
    [DllImport("user32.dll")] 
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,UIntPtr dwExtraInfo);

    static void Main(string[] args)
    {            
        while (true)
        {
            keybd_event((byte)0x31, (byte)0x02, 0, UIntPtr.Zero);
            Thread.Sleep(3000);

            keybd_event((byte)0x31, (byte)0x82, (uint)0x2, UIntPtr.Zero);
            Thread.Sleep(1000);
        }
    }
}

}


回答2:

have you tried using PostMessage to send WM_KEYDOWN and WM_KEYUP ?

Edit

You would use it this way (I am writing in C++, but you can easily use PInvoke and ..NET)

HWND hwnd = FindWindow(NULL,_T("Mywindow"));
PostMessage(hwnd,WM_KEYDOWN,VK_A,0);


回答3:

The official API is SendInput.



回答4:

You can use the WSH Scripting Shell to do this:

        WshShell shell = new WshShellClass();
        object missing=System.Reflection.Missing.Value;

        shell.SendKeys("MOO!!!", ref missing);

All you need to do is add a COM reference to "Windows Scripting Host Object", version 1.0. Everything is in the namespace IWshRuntimeLibrary.