private void btCalibration_Click(object sender, EventArgs e) { if (this.btCalibration.Text == "开始标定") { _eventQ.Clear(); _isMonitoring = true; this.txtDeviceID.ReadOnly = true; this.txtInput.Focus(); this.btCalibration.Text = "结束标定"; } else { string str_Out = string.Empty; ThreadPool.QueueUserWorkItem((o) => { while (_eventQ.Count > 0) { RawInput_dll.Win32.KeyAndState keyAndState = _eventQ.Dequeue(); str_Out += COMM.Utils.Chr(keyAndState.Key).ToString(); System.Threading.Thread.Sleep(5); // might need adjustment } DoShowDeviceInfo(str_Out); // this.DialogResult = DialogResult.Yes; }); if (device_id == null || device_id == string.Empty) { MessageBox.Show("未获取到设备信息,请重试!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); this.DialogResult = DialogResult.No; } else { this.txtDeviceID.Text = device_id; this.btCalibration.Text = "开始标定"; this.DialogResult = DialogResult.Yes; } } }
private void Btn_OK_Click(object sender, RoutedEventArgs e) { string str_Out = string.Empty; ThreadPool.QueueUserWorkItem((o) => { while (_eventQ.Count > 0) { RawInput_dll.Win32.KeyAndState keyAndState = _eventQ.Dequeue(); str_Out += Chr(keyAndState.Key).ToString(); System.Threading.Thread.Sleep(5); // might need adjustment } Application.Current.Dispatcher.BeginInvoke((Action)(() => { textBox_KeyData.Text = str_Out; })); }); }
protected override void WndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case Win32.WM_USB_DEVICECHANGE: if (_keyboardDriver != null) { _keyboardDriver.EnumerateDevices(); } break; case Win32.WM_INPUT: { KeyPressEvent keyPressEvent; if (_keyboardDriver == null) { base.WndProc(ref m); return; } _keyboardDriver.ProcessRawInput(m.LParam, out keyPressEvent); if (_workSpace.ScanGanID == null || _workSpace.ScanGanID == String.Empty) { if (SCanErrorEvent != null) { SCanErrorEvent("未添加扫描枪", EventArgs.Empty); } //MessageBox.Show("未添加扫描枪!"); return; } // textBox_ScanGunInfoNow.Text = keyPressEvent.DeviceName; //只处理一次事件,不然有按下和弹起事件 if (keyPressEvent.KeyPressState == "MAKE" && keyPressEvent.DeviceName == _workSpace.ScanGanID && _workSpace.ScanGanID != string.Empty) { // textBox_Output.Focus(); // this.lbStatus.Focus(); //找到结尾标志的时候,就不加入队列了,然后就发送到界面上赋值 if (KeyInterop.KeyFromVirtualKey(keyPressEvent.VKey) == Key.Enter) { _isMonitoring = false; string str_Out = string.Empty; ThreadPool.QueueUserWorkItem((o) => { while (_eventQ.Count > 0) { RawInput_dll.Win32.KeyAndState keyAndState = _eventQ.Dequeue(); str_Out += COMM.Utils.Chr(keyAndState.Key).Trim(); System.Threading.Thread.Sleep(5); // might need adjustment } //Application.Current.Dispatcher.BeginInvoke((Action)(() => //{ // textBox_Output.Text = str_Out; //})); // this.lbStatus.Text = str_Out; ProcessUSBScanerCommand(str_Out.Trim()); // DoChangePnCode(str_Out.Trim()); _eventQ.Clear(); _isMonitoring = true; }); } // 回车 作为结束标志 if (_isMonitoring) { //存储 Win32 按键的int值 int key = keyPressEvent.VKey; byte[] state = new byte[256]; Win32.GetKeyboardState(state); _eventQ.Enqueue(new RawInput_dll.Win32.KeyAndState(key, state)); } } } break; default: base.WndProc(ref m); // 调用基类函数处理其他消息。 break; } }
void ComponentDispatcher_ThreadFilterMessage(ref System.Windows.Interop.MSG msg, ref bool handled) { switch (msg.message) { //这里只能以 INPUT 来截取,不支持 KEYDOWN 来截取,不然后面的 RawInput 获取值的时候无效 case Win32.WM_INPUT: { // Should never get here if you are using PreMessageFiltering KeyPressEvent keyPressEvent; _keyboardDriver.ProcessRawInput(msg.lParam, out keyPressEvent); textBox_ScanGunInfoNow.Text = keyPressEvent.DeviceName; //只处理一次事件,不然有按下和弹起事件 if (keyPressEvent.KeyPressState == "MAKE" && keyPressEvent.DeviceName == str_ScanGunID && str_ScanGunID != string.Empty) { textBox_Output.Focus(); //找到结尾标志的时候,就不加入队列了,然后就发送到界面上赋值 if (KeyInterop.KeyFromVirtualKey(keyPressEvent.VKey) == Key.Enter) { _isMonitoring = false; string str_Out = string.Empty; ThreadPool.QueueUserWorkItem((o) => { while (_eventQ.Count > 0) { RawInput_dll.Win32.KeyAndState keyAndState = _eventQ.Dequeue(); str_Out += CalibrationScanGun.Chr(keyAndState.Key).ToString(); System.Threading.Thread.Sleep(5); // might need adjustment } Application.Current.Dispatcher.BeginInvoke((Action)(() => { textBox_Output.Text = str_Out; })); _eventQ.Clear(); _isMonitoring = true; }); } // 回车 作为结束标志 if (_isMonitoring) { //存储 Win32 按键的int值 int key = keyPressEvent.VKey; byte[] state = new byte[256]; Win32.GetKeyboardState(state); _eventQ.Enqueue(new RawInput_dll.Win32.KeyAndState(key, state)); } } } break; case Win32.WM_KEYDOWN: { KeyPressEvent keyPressEvent; _keyboardDriver.ProcessRawInput(msg.lParam, out keyPressEvent); if (keyPressEvent.DeviceName == str_ScanGunID && str_ScanGunID != string.Empty) { handled = true; } } break; //这里截获这个消息有问题,替代方法是放到 WndProc 中去获取 case Win32.WM_USB_DEVICECHANGE: { } break; default: break; } return; }