0

How do I map a joypad (specifically the HAT switch/DPad) axis to a button with a key-layout file in /system/usr/keylayout/? I currently have this line in the appropriate file: axis 0x10 split 0x7f BUTTON_L1 BUTTON_R1.

I'll explain the line token by token. First token refers to the input type. axis means I'm binding an axis.

Second token is the axis ID. The axis I want to remap is 0x10.

The Android developer's wiki page concerning key-layout files mention you can split an input axis into two outputs with split so I'm using it. The split is at the value immediately following split, in this case 0x7f.

Input values below the split threshold map as the 1st token after the split threshold token. Here it is BUTTON_L1.

Input values above the split threshold map as the 2nd token after the split threshold token. Here it is BUTTON_R1.

However, when I use the line metioned above the axis does not map to anything anymore. Are you able to map device axes to Android buttons in the first place? If so, what is the proper syntax for it?

1

From the Split axes documentation:

A split axis maps a Linux axis code to two Android axis code names, such that values less than or greater than a threshold are split across two different axes when mapped. This mapping is useful when a single physical axis reported by the device encodes two different mutually exclusive logical axes.

In the example they split a combined gas/brake axis into separate GAS and BRAKE axes:

axis 0x01 split 0x7f GAS BRAKE

Axis index 0x01 is the Linux input event code ABS_X. We're taking the ABS_X axis with a logical range of 0x00 < x < 0xFF and splitting it so that x > 0x7F is mapped to AXIS_GAS and values below 0x7F are mapped to AXIS_BRAKE.

In yours:

axis 0x10 split 0x7f BUTTON_L1 BUTTON_R1

Axis index 0x10 is special, it's the Linux input event code ABS_HAT0X which is a synthetic axis generated by the Linux input subsystem to make it easier to deal with hat switch inputs. Internally, HID gamepads don't represent D-pads as buttons or as separate X and Y axes. They're usually represented as a single rotational "hat switch" axis. Linux converts the rotational value into ABS_HAT0X and ABS_HAT0Y axes normalized to -1 < x < 1. These are seen as axis 0x10 and axis 0x11 in the key layout mapping.

So, the first problem is your threshold is wrong. To split the ABS_HAT0X axis you should use a threshold of 0.

The second problem is you're trying to map the split axes to button events, which isn't supported. You can only split an axis into separate axes.

2
  • Is there a way to map specific directions on the hat switch to buttons, since I can't split an axis? Or am I completely out of luck? Nov 23 at 20:58
  • I don't think you can do this with key layout files.
    – nondebug
    2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.