The goal I want to achieve is send keystroke to another application, which is the core function of my new open source application TouchAssistant. And I want the keystroke can be customized easily by configuration from user interaction.Long Version:
- Use WinAPI
keybd_eventas this Link suggested. example as followed:
[DllImport("user32.dll")] static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
So far, I’m using this API, no shortcoming except no one liner to simulate keystroke with modifier.
- Use WinAPI
SendInputas this Link suggested. Note this API is available after Vista.
[DllImport("user32.dll")] static extern uint SendInput (uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT pInputs, int cbSize);
Much complex than previous one, however, it may get better compatibility especially with future version of Windows.
- Original WinForm has a method like following:
Very powerful function, and can be configured to use
However, it’s not so good to reference
System.Windows.Forms in WPF application.
- There is an open source project Windows Input Simulator.
Good wrapper, however, cannot gain enough flexibility for user customization like AHK or SendKeys did.
- Integrate AHK by using https://github.com/amazing-andrew/AutoHotkey.Interop