/// <summary> /// Provide the state of the dpad or hat switch if any. /// If no dpad is found we return 'at rest'. /// </summary> /// <returns></returns> public DirectionPadState GetDirectionPadState() { int index = GetValueCapabilitiesIndex((ushort)Hid.UsagePage.GenericDesktopControls, (ushort)GenericDesktop.HatSwitch); if (index < 0) { //No hat switch found return(DirectionPadState.Rest); } HIDP_VALUE_CAPS caps = Device.InputValueCapabilities[index]; if (caps.IsRange) { //Defensive return(DirectionPadState.Rest); } uint dpadUsageValue = UsageValues[caps]; if (dpadUsageValue < caps.LogicalMin || dpadUsageValue > caps.LogicalMax) { //Out of range means at rest return(DirectionPadState.Rest); } //Normalize value to start at zero //TODO: more error check here? DirectionPadState res = (DirectionPadState)((int)dpadUsageValue - caps.LogicalMin); return(res); }
void OnHidEvent(object aSender, Event aHidEvent) { if (!aHidEvent.Device.IsGamePad) { return; } #if DEBUG if (aHidEvent.IsRepeat) { Logger.Debug("HID: Repeat"); } #endif HashSet <ushort> buttons = new HashSet <ushort>(); foreach (ushort usage in aHidEvent.Usages) { buttons.Add(usage); } //For each axis Dictionary <ushort, HidAxisState> axisStates = new Dictionary <ushort, HidAxisState>(); foreach (KeyValuePair <HIDP_VALUE_CAPS, uint> entry in aHidEvent.UsageValues) { //HatSwitch is handled separately as direction pad state if (entry.Key.IsRange || entry.Key.NotRange.Usage == (ushort)GenericDesktop.HatSwitch) { continue; } //Get our usage type Type usageType = HidUtils.UsageType((UsagePage)entry.Key.UsagePage); if (usageType == null) { continue; } //Get the name of our axis string name = Enum.GetName(usageType, entry.Key.NotRange.Usage); ushort index = entry.Key.NotRange.DataIndex; axisStates[index] = new HidAxisState(name, index, entry.Value, entry.Key.BitSize); } DirectionPadState directionPadState = aHidEvent.GetDirectionPadState(); HidState state = new HidState { VendorId = aHidEvent.Device.VendorId, ProductId = aHidEvent.Device.ProductId, Name = aHidEvent.Device.Name, FriendlyName = aHidEvent.Device.FriendlyName, Buttons = buttons, AxisStates = axisStates, DirectionPadState = directionPadState }; OnStateChanged(new HidStateEventArgs(state)); }
public static bool IsDirectionPadPressed(DirectionPadState directionPadState, HidState state) { return(state.DirectionPadState == directionPadState); }