Sending keys to inactive application in C#/.NET [duplicate]
I have an ap开发者_开发知识库plication with combobox that contains names of currently running applications. As I understood from msdn library, SendKeys method can send keys only to active application. Is it somehow possible in .NET, to send keys also to inactive app? Or at least in WinAPI ?
You can use the SendMessage()
API function to send keystrokes to an inactive window.
With C# <3 is everthing possible :D
No need to be active window, as u wished.
Also here a usefull list of Virtual Key Codes
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
const int WM_SYSKEYDOWN = 0x0104;
const int VK_KEY_A = 0x41;
IntPtr WindowToFind = FindWindow(null, "Window Name");
//In ur case u have to write a code that translates the combobox into Virtual Key Codes. Will take time but it shouls be easy
PostMessage(WindowToFind, WM_SYSKEYDOWN, VK_KEY_A, 0);
//PostMessage(WindowToFind, WM_SYSKEYDOWN, ((int)Keys.NumPad7), 0);
}
精彩评论