How To Map Joypad Axis To Button?
-
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 https://source.android.com/devices/input/key-layout-files 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 followingsplit
, in this case0x7f
.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?
-
From the https://source.android.com/devices/input/key-layout-files 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
andBRAKE
axes:axis 0x01 split 0x7f GAS BRAKE
Axis index
0x01
is the Linux input event code https://github.com/torvalds/linux/blob/136057256686de39cc3a07c2e39ef6bc43003ff6/include/uapi/linux/input-event-codes.h#L816 . We're taking theABS_X
axis with a logical range of0x00
< x <0xFF
and splitting it so that x >0x7F
is mapped to https://developer.android.com/reference/android/view/MotionEvent#AXIS_GAS and values below0x7F
are mapped to https://developer.android.com/reference/android/view/MotionEvent#AXIS_BRAKE .In yours:
axis 0x10 split 0x7f BUTTON_L1 BUTTON_R1
Axis index
0x10
is special, it's the Linux input event code https://github.com/torvalds/linux/blob/136057256686de39cc3a07c2e39ef6bc43003ff6/include/uapi/linux/input-event-codes.h#L827 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 intoABS_HAT0X
andABS_HAT0Y
axes normalized to -1 < x < 1. These are seen as axis0x10
and axis0x11
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.