How to send keyboard key in my application?
I'm currently using SendKeys.SendWait(text);
in C#
but SendKeys
sending the key global and I have to activate my application and then send it. And another problem is when 开发者_运维技巧I type something in my keyboard (in another app) and the SendKeys
function activates (in my app) mistakes happen.
So how can I send a message to my application regardless what application is active and what I type in my keyboard?
SendMessage()
does what you want. You'll need to use it like:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
const UInt32 WM_CHAR = 0x0102;
const int VK_Q = 0x51; // taken from http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx
SendMessage(handleToTheInputForm, WM_CHAR, VK_Q, 1);
You will need to get a handle
on the other application's window so that you can bring it to focus and reliably send your keystrokes to it,
Have a look at this tutorial
http://www.codeproject.com/KB/cs/SendKeys.aspx
精彩评论