private void OnReceiveKeyboardData(RawInputKeyboardData data) { Keys key = (Keys)data.Keyboard.VirutalKey; logToUi.AddLog( $"Keyboard: {data.Device.ProductName}, keyCode={data.Keyboard.VirutalKey}, key={key}, flag={data.Keyboard.Flags}" ); }
public void ParseKeyboardGestures(RawInputKeyboardData data) { var key = KeyInterop.KeyFromVirtualKey(data.Keyboard.VirutalKey); var pressed = (data.Keyboard.Flags & RawKeyboardFlags.Up) == 0; if (pressed) { _keyboardState[key] = true; } else { var pressedKeys = _keyboardState.Where(x => x.Value).Select(x => x.Key).ToList(); if (pressedKeys.Count > 0) { HandleGesture(KeyboardGesture.Create(pressedKeys)); } foreach (var pressedKey in pressedKeys) { _keyboardState[pressedKey] = false; } } }
/// <summary> /// Sets text box text and tag. /// </summary> /// <param name="key"></param> private void SetTextBoxText(string text, RawInputData data) { Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => { bool save = true; var txt = GetActiveTextBox(); if (txt == null) { return; } // Ignore first if (txt == _lastActiveTextBox) { string path = "null"; if (data != null && data.Device != null && data.Device.DevicePath != null) { path = data.Device.DevicePath; } var button = new RawInputButton { DevicePath = path, DeviceType = RawDeviceType.None, MouseButton = RawMouseButton.None, KeyboardKey = Keys.None }; if (data is RawInputMouseData) { RawInputMouseData mouse = data as RawInputMouseData; button.MouseButton = GetButtonFromFlags(mouse.Mouse.Buttons); button.DeviceType = RawDeviceType.Mouse; } else if (data is RawInputKeyboardData) { RawInputKeyboardData kb = data as RawInputKeyboardData; button.KeyboardKey = (Keys)kb.Keyboard.VirutalKey; button.DeviceType = RawDeviceType.Keyboard; if (button.KeyboardKey == Keys.Escape) { save = false; } } // Save? if (save) { txt.ToolTip = text; txt.Text = text; var t = txt.Tag as JoystickButtons; t.RawInputButton = button; t.BindNameRi = text; } else { txt.ToolTip = ""; txt.Text = ""; } // Unfocus textbox Keyboard.ClearFocus(); FocusManager.SetFocusedElement(Application.Current.Windows[0], null); _lastActiveTextBox = null; } else { _lastActiveTextBox = txt; } })); }
public void Setup() { var devices = RawInputDevice.GetDevices(); var hid = devices.OfType <RawInputHid>(); var keyboards = devices.OfType <RawInputKeyboard>(); //RawInputKeyboard keyboard1=keyboards.ElementAt(0); // var window1 = new RawInputReceiverWindow(); var window2 = new RawInputReceiverWindow(); window2.Input += (sender, e) => { // Catch your input here! var data1 = e.Data; RawInputKeyboardData data_2 = (RawInputKeyboardData)data1; // messageBx.Text = data_2.Device.DeviceType.ToString(); // X_axis.Text = data_2.Keyboard.ScanCode.ToString(); // messageBx.Text = data.ToString(); }; // Register the HidUsageAndPage to watch any device. window1.Input += (sender, e) => { // Catch your input here! data = e.Data; data_1 = (RawInputHidData)data; joy_x = data_1.Hid.RawData.ElementAt(4) - 128; joy_y = data_1.Hid.RawData.ElementAt(5) - 128; joy_z = data_1.Hid.RawData.ElementAt(2) - 128; axis_speed = 0.05f; if (data_1.Hid.RawData.ElementAt(7) == 4 || data_1.Hid.RawData.ElementAt(7) == 8) { axis_speed = 0.1f; } // if (data_1.Hid.RawData.ElementAt(4) != 128) // x_axis_var += joy_x * axis_speed; //port.WriteLine("G1 " + "X" + (joy_x * axis_speed) + "F" + fBox.Text); // if (data_1.Hid.RawData.ElementAt(5) != 128) // y_axis_var += joy_y * axis_speed; //Y_axis.Text = y_axis_var.ToString("F3"); // if (data_1.Hid.RawData.ElementAt(2) != 128) // z_axis_var += joy_z * axis_speed; // Z_axis.Text = z_axis_var.ToString("F3"); //Z_axis.Text = data_1.Hid.RawData.ElementAt(6).ToString(); if (port != null && port.IsOpen) { int z_max = 50; int y_max = 50; int x_max = 50; //MessageBox.Show("Joystick is online!"); //v.LeftMotorSpeed = (ushort)(controller.GetState().Gamepad.LeftTrigger * 255); // v.RightMotorSpeed = (ushort)(controller.GetState().Gamepad.RightTrigger * 255); if (joy_x != 0) { if (x_axis_var < x_max) { if (joy_x < 0) { axis_speed = -axis_speed; } //X_axis.Text = x_axis_var.ToString("F3"); //x_axis_var += joy_x * axis_speed; x = axis_speed; //x = speed_xy * x; port.WriteLine("G91"); port.WriteLine("G0 X" + x.ToString("F3") + "F" + fBox.Text); //port.WriteLine("G90"); x_axis_var += x; X_axis.Text = x_axis_var.ToString("F3"); } } if (joy_y != 0) { if (y_axis_var < y_max) { if (joy_y > 0) { axis_speed = -axis_speed; } //y = speed_xy * y; y = axis_speed; port.WriteLine("G91"); port.WriteLine("G0 Y" + y.ToString() + "F" + fBox.Text); //port.WriteLine("G90"); y_axis_var += y; Y_axis.Text = y_axis_var.ToString("F3"); } } if (joy_z != 0) { if (z_axis_var < z_max) { // z = speed_z * z; if (joy_z > 0) { axis_speed = -axis_speed; } z = axis_speed; port.WriteLine("G91"); port.WriteLine("G0 Z" + z.ToString() + "F" + fBox.Text); //port.WriteLine("G90"); z_axis_var += z; Z_axis.Text = z_axis_var.ToString("F3"); } } // controller.SetVibration(v); if (data_1.Hid.RawData.ElementAt(6) == 143) //X buttun { //messageBx.Text = controller.GetState().Gamepad.Buttons + "\r\n"; x_axis_var = 0; X_axis.Text = x_axis_var.ToString("F3"); port.WriteLine("G92 X0"); } if (data_1.Hid.RawData.ElementAt(6) == 31) //Y buttun { //messageBx.Text = controller.GetState().Gamepad.Buttons + "\r\n"; y_axis_var = 0; Y_axis.Text = y_axis_var.ToString("F3"); port.WriteLine("G92 Y0"); } if (data_1.Hid.RawData.ElementAt(6) == 47) //Z buttun { // messageBx.Text = controller.GetState().Gamepad.Buttons + "\r\n"; z_axis_var = 0; Z_axis.Text = z_axis_var.ToString("F3"); port.WriteLine("G92 Z0"); } if (data_1.Hid.RawData.ElementAt(6) == 79) //A buttun { // messageBx.Text = controller.GetState().Gamepad.Buttons + "\r\n"; x_axis_var = 0; X_axis.Text = x_axis_var.ToString("F3"); y_axis_var = 0; Y_axis.Text = y_axis_var.ToString("F3"); z_axis_var = 0; Z_axis.Text = z_axis_var.ToString("F3"); port.WriteLine("G92 X0 Y0 Z0"); } } // set ports //port.WriteLine("G91"); //messageBx.Text += "G91 \r\n"; //port.WriteLine("G1 " + sendBx.Text + "F" + fBox.Text); // messageBx.Text += "G1 " + sendBx.Text + "\r\n"; //port.WriteLine("G90"); //messageBx.Text += "G90 \r\n"; // move the axis }; // RawInputDevice.RegisterDevice(HidUsageAndPage.Keyboard, RawInputDeviceFlags.ExInputSink | RawInputDeviceFlags.NoLegacy, window2.Handle); //RawInputDevice.RegisterDevice(HidUsageAndPage.Joystick, RawInputDeviceFlags.ExInputSink, window1.Handle); //RawInputDevice.UnregisterDevice(HidUsageAndPage.Joystick); //RawInputDevice.UnregisterDevice(HidUsageAndPage.Keyboard); foreach (var device in hid) { if (device.UsageAndPage.Usage == 0x04 || device.UsageAndPage.Usage == 0x05) { string productid = device.ProductId.ToString("X4"); string vendorid = device.VendorId.ToString("X4"); // messageBx.Text += device.ProductName + " ::" + productid + "::" + vendorid + "\r\n";// +device.VendorId:X4+device.ProductId:X4+device.ProductName+device.ManufacturerName; } } }
private void HandleKeyboardData(RawInputData data, RawInputKeyboardData keyboardData) { KeyboardKey key = (KeyboardKey)KeyInterop.KeyFromVirtualKey(keyboardData.Keyboard.VirutalKey); // Debug.WriteLine($"VK: {key} ({keyboardData.Keyboard.VirutalKey}), Flags: {keyboardData.Keyboard.Flags}, Scan code: {keyboardData.Keyboard.ScanCode}"); // Sometimes we get double hits and they resolve to None, ignore those if (key == KeyboardKey.None) { return; } // Right alt triggers LeftCtrl with a different scan code for some reason, ignore those if (key == KeyboardKey.LeftCtrl && keyboardData.Keyboard.ScanCode == 56) { return; } string identifier = data.Device?.DevicePath; // Let the core know there is an identifier so it can store new identifications if applicable if (identifier != null) { OnIdentifierReceived(identifier, InputDeviceType.Keyboard); } ArtemisDevice device = null; if (identifier != null) { try { device = _inputService.GetDeviceByIdentifier(this, identifier, InputDeviceType.Keyboard); } catch (Exception e) { _logger.Warning(e, "Failed to retrieve input device by its identifier"); } } // Duplicate keys with different positions can be identified by the LeftKey flag (even though its set of the key that's physically on the right) if (keyboardData.Keyboard.Flags == RawKeyboardFlags.LeftKey || keyboardData.Keyboard.Flags == (RawKeyboardFlags.LeftKey | RawKeyboardFlags.Up)) { if (key == KeyboardKey.Enter) { key = KeyboardKey.NumPadEnter; } if (key == KeyboardKey.LeftCtrl) { key = KeyboardKey.RightCtrl; } if (key == KeyboardKey.LeftAlt) { key = KeyboardKey.RightAlt; } } if (key == KeyboardKey.LeftShift && keyboardData.Keyboard.ScanCode == 54) { key = KeyboardKey.RightShift; } bool isDown = keyboardData.Keyboard.Flags != RawKeyboardFlags.Up && keyboardData.Keyboard.Flags != (RawKeyboardFlags.Up | RawKeyboardFlags.LeftKey) && keyboardData.Keyboard.Flags != (RawKeyboardFlags.Up | RawKeyboardFlags.RightKey); OnKeyboardDataReceived(device, key, isDown); UpdateToggleStatus(); }