/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Matches(object obj) { if (obj is EventHid) { EventHid e = (EventHid)obj; return(e.Key == Key && e.Usage == Usage && e.UsagePage == UsagePage && e.UsageCollection == UsageCollection && e.IsKeyUp == IsKeyUp && e.IsGeneric == IsGeneric && e.IsKeyboard == IsKeyboard && e.IsMouse == IsMouse && e.HasModifierAlt == HasModifierAlt && e.HasModifierControl == HasModifierControl && e.HasModifierShift == HasModifierShift && e.HasModifierWindows == HasModifierWindows); } return(false); }
/// <summary> /// Here we receive HID events from our HID library. /// </summary> /// <param name="aSender"></param> /// <param name="aHidEvent"></param> public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent) { if (aHidEvent.IsStray || !aHidEvent.IsValid) { //Stray event just ignore it return; } if (this.InvokeRequired) { //Not in the proper thread, invoke ourselves OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe); this.Invoke(d, new object[] { aSender, aHidEvent }); } else { //Trigger corresponding EAR event if any EventHid e = new EventHid(); e.Copy(aHidEvent); Properties.Settings.Default.EarManager.TriggerEvents(e); } }
/// <summary> /// In our EAR framework this just checks if an object matches another one. /// However an HID event also uses this to manage various states when receiving incoming events. /// TODO: Recode that logic to support mouse, remote control and other generic device key up and down event /// TODO: Maybe implement a sub class to support multiple buttons press for gamePads and joysticks /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Matches(object obj) { // TODO: have an option to make it device specific and test the Device InstancePath if (obj is EventHid) { EventHid e = (EventHid)obj; bool joystick = e.IsJoystick == IsJoystick; bool isJoystick = joystick && IsJoystick; bool gamepad = e.IsGamePad == IsGamePad; bool isGamepad = gamepad && IsGamePad; bool sameUsage = e.Usage == Usage; bool sameDevice = e.Device.CurrentItem.Equals(Device.CurrentItem, StringComparison.OrdinalIgnoreCase); bool match = e.Key == Key && e.UsagePage == UsagePage && e.UsageCollection == UsageCollection && e.IsGeneric == IsGeneric && e.IsKeyboard == IsKeyboard && e.IsMouse == IsMouse && joystick && gamepad && e.HasModifierAlt == HasModifierAlt && e.HasModifierControl == HasModifierControl && e.HasModifierShift == HasModifierShift && e.HasModifierWindows == HasModifierWindows; if (match) { if (isJoystick || isGamepad) { if (sameDevice) { if (e.Usages.Contains(Usage)) { if (!ButtonDown) { //Trace.WriteLine("ButtonDown: " + Brief()); ButtonDown = true; if (!IsKeyUp) { return(true); } } } else { if (ButtonDown) { //Trace.WriteLine("ButtonUp: " + Brief()); ButtonDown = false; if (IsKeyUp) { return(true); } } } } } else { // Not a joystick or a gamepad // Just check if our key up is a match and we are done here // TODO: We should only do that for keyboard I guess as remote control and other generic HID may need same treatment as joysticks to support key up and down return(e.IsKeyUp == IsKeyUp && sameUsage); } } } return(false); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Matches(object obj) { if (!(obj is EventHid)) { return(false); } EventHid e = (EventHid)obj; bool joystick = e.IsJoystick == IsJoystick; bool isJoystick = joystick && IsJoystick; bool gamepad = e.IsGamePad == IsGamePad; bool isGamepad = gamepad && IsGamePad; if (!isJoystick && !isGamepad) { return(false); } // TODO: Avoid string comparison here? Compute a hash maybe? bool sameDevice = e.Device.CurrentItem.Equals(Device.CurrentItem, StringComparison.OrdinalIgnoreCase); if (!sameDevice) { // That's not the device we are interested in return(false); } //For each axis on that device foreach (KeyValuePair <HIDP_VALUE_CAPS, uint> entry in e.HidEvent.UsageValues) { if (!SharpDisplayManager.Axis.IsAxis(entry.Key)) { continue; } if (SharpDisplayManager.Axis.IdFromValueCaps(entry.Key) != AxisId) { // That's not the axis we are interested in continue; } // First time here if (TargetAxis == null) { TargetAxis = new Axis(entry.Key); } var axis = TargetAxis; axis.Value = (int)entry.Value; if (Invert) { axis.Value = axis.Range - axis.Value; } // Compute trigger boundaries from our range int lowBound = (axis.Range * Size / 100); int highBound = axis.Range - lowBound; // bool match = false; if (axis.Value > highBound && !(iLastValue > highBound)) { // We passed our threshold trigger that event iActionIndex = 0; match = true; } else if (axis.Value < lowBound && !(iLastValue < lowBound)) { // We passed our threshold trigger that event iActionIndex = 3; match = true; } // We are between our two boundaries somewhere in the middle of our axis else if (axis.Value >= lowBound && axis.Value <= highBound) { if (iLastValue > highBound) { // We were just going down iActionIndex = 1; match = true; } else if (iLastValue < lowBound) { // We were just going up iActionIndex = 2; match = true; } } iLastValue = axis.Value; return(match); } return(false); }