# Gamepad Hats - what should be possible?

**URL:** <https://forum.defold.com/t/gamepad-hats-what-should-be-possible/79373>\
**Category:** Source Code\
**Created:** [January 8, 2025, 4:38pm UTC](https://forum.defold.com/t/gamepad-hats-what-should-be-possible/79373 "2025-01-08T16:38:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jay/32/35079_2.png) [@Jay](https://forum.defold.com/u/Jay)\
**Post date:** [January 8, 2025, 4:38pm UTC](https://forum.defold.com/t/gamepad-hats-what-should-be-possible/79373/1 "2025-01-08T16:38:47Z")

</div>

I’m writing a GUI tool to create gamepad mappings from the RAW gamepad input. The detector looks for a single input by counting all the `gamepad_buttons`, `gamepad_axis`, and `gamepad_hats` where `math.abs(value)` is greater than a small threshold. If exactly one input is detected it can then create the mapping for it.

However, when a `hat` is present, it seems that there is always a `button` also present (at least on all the controllers I have physical access to for testing). I have this code to detect this case, after looping through and counting all the inputs:

```lua
-- Hats seem to be remapped buttons, so when a hat is present a button will also be present.
-- That is also why hats are counted last - so that the detected_data will be the hat.
if hats_count == buttons_count then
    buttons_count = buttons_count - hats_count
end
local input_count = axis_count + buttons_count + hats_count

```

Is this a sufficient check? Or is it also possible that hats might be sometimes remapped from an axis value, and I need to handle that case too?

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [January 9, 2025, 11:11am UTC](https://forum.defold.com/t/gamepad-hats-what-should-be-possible/79373/2 "2025-01-09T11:11:14Z")

</div>

Not sure what you are asking.

Each hat “button” is handled the same way as a regular button. So, our gamepads only have axis and buttons.

---

<div class="post-metadata">

**Author:** ![Jay](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jay/32/35079_2.png) [@Jay](https://forum.defold.com/u/Jay)\
**Post date:** [January 9, 2025, 11:58am UTC](https://forum.defold.com/t/gamepad-hats-what-should-be-possible/79373/3 "2025-01-09T11:58:57Z")

</div>

> [@Mathias\_Westerdahl](#):
>
> Not sure what you are asking.

The code is [here](https://github.com/j-h-a/defold-gamepad-mapper/blob/c1b39225770e26957371f9bd549ca7db6b504606/main/detector.lua#L76). It looks at the gamepad raw data, loops through all possible axis (32), button (32), and hats (4), and tries to check that one and only one input is currently being pressed.

However, whenever a hat was present there was always a corresponding button present. So I had to add the above code to ignore the button input that was present with the hat.

The question is about if this check is sufficient to handle all controller types. A counter-example might be a controller where the D-pad comes in on an axis and gets somehow mapped to the hat values. If that were the case and the raw data included a hat plus an axis, it would not be possible with the code above to detect that state, because it would be seen as two inputs not one.

I guess another thought is that, if hats are ALWAYS simultaneously mapped as buttons… what’s the point of having hats in the `.gamepads` files at all? Why not just use the button indices?
