void ReleaseDesignerOutlets() { if (DetectionImageView != null) { DetectionImageView.Dispose(); DetectionImageView = null; } if (DetectButton != null) { DetectButton.Dispose(); DetectButton = null; } }
public void AssignButton(DetectButton callback) { //detect the state of all current buttons Task.Run(() => { var initial = new int[_inputDevices.Count, 128 + 4]; // for POV for (var i = 0; i < _inputDevices.Count; i++) { if (_inputDevices[i] is Joystick) { _inputDevices[i].Poll(); var state = (_inputDevices[i] as Joystick).GetCurrentState(); for (var j = 0; j < state.Buttons.Length; j++) { initial[i, j] = state.Buttons[j] ? 1 : 0; } var pov = state.PointOfViewControllers; for (var j = 0; j < pov.Length; j++) { initial[i, j + 128] = pov[j]; } } else if (_inputDevices[i] is Keyboard) { var keyboard = _inputDevices[i] as Keyboard; keyboard.Poll(); var state = keyboard.GetCurrentState(); for (var j = 0; j < 128; j++) { initial[i, j] = state.IsPressed(state.AllKeys[j]) ? 1 : 0; } } else if (_inputDevices[i] is Mouse) { var mouse = _inputDevices[i] as Mouse; mouse.Poll(); var state = mouse.GetCurrentState(); for (var j = 0; j < state.Buttons.Length; j++) { initial[i, j] = state.Buttons[j] ? 1 : 0; } } } var device = string.Empty; var button = 0; var deviceGuid = Guid.Empty; var buttonValue = -1; var found = false; while (!found) { Thread.Sleep(100); for (var i = 0; i < _inputDevices.Count; i++) { if (_inputDevices[i] is Joystick) { _inputDevices[i].Poll(); var state = (_inputDevices[i] as Joystick).GetCurrentState(); for (var j = 0; j < 128 + 4; j++) { if (j >= 128) { //handle POV var pov = state.PointOfViewControllers; if (pov[j - 128] != initial[i, j]) { found = true; var inputDevice = new InputDevice { DeviceName = _inputDevices[i].Information.ProductName.Trim().Replace("\0", ""), Button = j, InstanceGuid = _inputDevices[i].Information.InstanceGuid, ButtonValue = pov[j - 128] }; Application.Current.Dispatcher.Invoke( () => { callback(inputDevice); }); return; } } else { var buttonState = state.Buttons[j] ? 1 : 0; if (buttonState != initial[i, j]) { found = true; var inputDevice = new InputDevice { DeviceName = _inputDevices[i].Information.ProductName.Trim().Replace("\0", ""), Button = j, InstanceGuid = _inputDevices[i].Information.InstanceGuid, ButtonValue = buttonState }; Application.Current.Dispatcher.Invoke( () => { callback(inputDevice); }); return; } } } } else if (_inputDevices[i] is Keyboard) { var keyboard = _inputDevices[i] as Keyboard; keyboard.Poll(); var state = keyboard.GetCurrentState(); for (var j = 0; j < 128; j++) { if (initial[i, j] != (state.IsPressed(state.AllKeys[j]) ? 1 : 0)) { found = true; var inputDevice = new InputDevice { DeviceName = _inputDevices[i].Information.ProductName.Trim().Replace("\0", ""), Button = j, InstanceGuid = _inputDevices[i].Information.InstanceGuid, ButtonValue = 1 }; Application.Current.Dispatcher.Invoke( () => { callback(inputDevice); }); return; } // if (initial[i, j] == 1) // { // Console.WriteLine("Pressed: "+j); // MessageBox.Show("Keyboard!"); // } } } else if (_inputDevices[i] is Mouse) { _inputDevices[i].Poll(); var state = (_inputDevices[i] as Mouse).GetCurrentState(); //skip left mouse button - start at 1 with j 0 is left, 1 is right, 2 is middle for (var j = 1; j < state.Buttons.Length; j++) { var buttonState = state.Buttons[j] ? 1 : 0; if (buttonState != initial[i, j]) { found = true; var inputDevice = new InputDevice { DeviceName = _inputDevices[i].Information.ProductName.Trim().Replace("\0", ""), Button = j, InstanceGuid = _inputDevices[i].Information.InstanceGuid, ButtonValue = buttonState }; Application.Current.Dispatcher.Invoke( () => { callback(inputDevice); }); return; } } } } } }); }
public void AssignButton(DetectButton callback) { //detect the state of all current buttons Task.Run(() => { var deviceList = _inputDevices.Values.ToList(); var initial = new int[deviceList.Count, 128 + 4]; // for POV for (var i = 0; i < deviceList.Count; i++) { if (deviceList[i] == null || deviceList[i].IsDisposed) { continue; } try { if (deviceList[i] is Joystick) { deviceList[i].Poll(); var state = (deviceList[i] as Joystick).GetCurrentState(); for (var j = 0; j < state.Buttons.Length; j++) { initial[i, j] = state.Buttons[j] ? 1 : 0; } var pov = state.PointOfViewControllers; for (var j = 0; j < pov.Length; j++) { initial[i, j + 128] = pov[j]; } } else if (deviceList[i] is Keyboard) { var keyboard = deviceList[i] as Keyboard; keyboard.Poll(); var state = keyboard.GetCurrentState(); for (var j = 0; j < 128; j++) { initial[i, j] = state.IsPressed(state.AllKeys[j]) ? 1 : 0; } } else if (deviceList[i] is Mouse) { var mouse = deviceList[i] as Mouse; mouse.Poll(); var state = mouse.GetCurrentState(); for (var j = 0; j < state.Buttons.Length; j++) { initial[i, j] = state.Buttons[j] ? 1 : 0; } } } catch (Exception e) { Logger.Error(e, $"Failed to get current state of input device {deviceList[i].Information.ProductName.Trim().Replace("\0", "")} " + $"(ID: {deviceList[i].Information.ProductGuid}) while assigning button, ignoring until next restart/rediscovery"); deviceList[i].Unacquire(); deviceList[i].Dispose(); deviceList[i] = null; } } var device = string.Empty; var button = 0; var deviceGuid = Guid.Empty; var buttonValue = -1; var found = false; while (!found) { Thread.Sleep(100); for (var i = 0; i < _inputDevices.Count; i++) { if (deviceList[i] == null || deviceList[i].IsDisposed) { continue; } try { if (deviceList[i] is Joystick) { deviceList[i].Poll(); var state = (deviceList[i] as Joystick).GetCurrentState(); for (var j = 0; j < 128 + 4; j++) { if (j >= 128) { //handle POV var pov = state.PointOfViewControllers; if (pov[j - 128] != initial[i, j]) { found = true; var inputDevice = new InputDevice { DeviceName = deviceList[i].Information.ProductName.Trim().Replace("\0", ""), Button = j, InstanceGuid = deviceList[i].Information.InstanceGuid, ButtonValue = pov[j - 128] }; Application.Current.Dispatcher.Invoke( () => { callback(inputDevice); }); return; } } else { var buttonState = state.Buttons[j] ? 1 : 0; if (buttonState != initial[i, j]) { found = true; var inputDevice = new InputDevice { DeviceName = deviceList[i].Information.ProductName.Trim().Replace("\0", ""), Button = j, InstanceGuid = deviceList[i].Information.InstanceGuid, ButtonValue = buttonState }; Application.Current.Dispatcher.Invoke( () => { callback(inputDevice); }); return; } } } } else if (deviceList[i] is Keyboard) { var keyboard = deviceList[i] as Keyboard; keyboard.Poll(); var state = keyboard.GetCurrentState(); for (var j = 0; j < 128; j++) { if (initial[i, j] != (state.IsPressed(state.AllKeys[j]) ? 1 : 0)) { found = true; var inputDevice = new InputDevice { DeviceName = deviceList[i].Information.ProductName.Trim().Replace("\0", ""), Button = j, InstanceGuid = deviceList[i].Information.InstanceGuid, ButtonValue = 1 }; Application.Current.Dispatcher.Invoke( () => { callback(inputDevice); }); return; } // if (initial[i, j] == 1) // { // Console.WriteLine("Pressed: "+j); // MessageBox.Show("Keyboard!"); // } } } else if (deviceList[i] is Mouse) { deviceList[i].Poll(); var state = (deviceList[i] as Mouse).GetCurrentState(); //skip left mouse button - start at 1 with j 0 is left, 1 is right, 2 is middle for (var j = 1; j < state.Buttons.Length; j++) { var buttonState = state.Buttons[j] ? 1 : 0; if (buttonState != initial[i, j]) { found = true; var inputDevice = new InputDevice { DeviceName = deviceList[i].Information.ProductName.Trim().Replace("\0", ""), Button = j, InstanceGuid = deviceList[i].Information.InstanceGuid, ButtonValue = buttonState }; Application.Current.Dispatcher.Invoke( () => { callback(inputDevice); }); return; } } } } catch (Exception e) { Logger.Error(e, $"Failed to get current state of input device {deviceList[i].Information.ProductName.Trim().Replace("\0", "")} " + $"(ID: {deviceList[i].Information.ProductGuid}) while discovering button press while assigning, ignoring until next restart/rediscovery"); deviceList[i].Unacquire(); deviceList[i].Dispose(); deviceList[i] = null; } } } }); }