C # analog keyboard input (1 ),
The Windows API is used for implementation. You can find user32.dll In the system32 folder on your drive C. The function description is available in MSDN. You only need to search the user32.dll by name.
Obtain the window handle based on the class name and window name of the window. A Window handle is returned successfully. Otherwise, 0 is returned:
[DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
Find the first subwindow that matches the specified condition in the window list.
[DllImport("user32.dll", EntryPoint = "FindWindowEx")] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClass, string lpszWindow);
Find the handle and send a message to the window. The SendMessage method has many reloads.
[DllImport("user32.dll ", EntryPoint = "SendMessage")] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
Const int WM_CHAR = 0x0102; const int WM_SETTEXT = 0x000C; const int VK_RETURN = 0x0d; static void Main (string [] args) {IntPtr handle = FindWindow (null, "Hello.txt-Notepad"); handle = find+wex (handle, IntPtr. zero, "Edit", null); if (handle = IntPtr. zero) {Console. writeLine ("no handle found"); return;} SendMessage (handle, WM_SETTEXT, IntPtr. zero, "Hello word! ");
SendMessage (handle, WM_CHAR, (IntPtr) VK_RETURN, IntPtr. Zero); // Enter
}
Related links:
Virtual key table: http://baike.baidu.com/view/555571.htm
SendMessage type: http://baike.baidu.com/view/1080187.htm