最近遇到一个需求:需要在网站的文本输入栏上输入条码和回车; 查了一下资料,记录如下:
最后的方案: 两台电脑用串口连接,从A机器发送信息到串口, B机器从串口读到信息,并模拟键盘输出。
- public class CKeyController
- {
- [System.Runtime.InteropServices.DllImport("user32")]
- static extern void keybd_event(
- byte bVk,
- byte bScan,
- uint dwFlags,
- uint dwExtraInfo
- );
- const uint KEYEVENTF_EXTENDEDKEY = 0x1;
- const uint KEYEVENTF_KEYUP = 0x2;
- private static Dictionary<string, byte> keycode = new Dictionary<string, byte>();
- [DllImport("user32.dll")]
- public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
- [DllImport("User32.DLL")]
- public static extern int SendMessage(IntPtr hWnd,uint Msg, int wParam, string lParam);
- [DllImport("User32.DLL")]
- public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
- [DllImport("User32.DLL")]
- public static extern IntPtr FindWindowEx(IntPtr hwndParent,
- IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
- public const uint WM_SETTEXT = 0x000C;
- public const uint WM_CHAR = 0x0102;
- public static int WM_KEYDOWN = 0x0100;
- //释放一个键
- public static int WM_KEYUP = 0x0101;
- public void InitKey()
- {
- keycode = new Dictionary<string, byte>();
- keycode.Add("A", 65);
- keycode.Add("B", 66);
- keycode.Add("C", 67);
- keycode.Add("D", 68);
- keycode.Add("E", 69);
- keycode.Add("F", 70);
- keycode.Add("G", 71);
- keycode.Add("H", 72);
- keycode.Add("I", 73);
- keycode.Add("J", 74);
- keycode.Add("K", 75);
- keycode.Add("L", 76);
- keycode.Add("M", 77);
- keycode.Add("N", 78);
- keycode.Add("O", 79);
- keycode.Add("P", 80);
- keycode.Add("Q", 81);
- keycode.Add("R", 82);
- keycode.Add("S", 83);
- keycode.Add("T", 84);
- keycode.Add("U", 85);
- keycode.Add("V", 86);
- keycode.Add("W", 87);
- keycode.Add("X", 88);
- keycode.Add("Y", 89);
- keycode.Add("Z", 90);