Step three. Mapping buttons.
3. Mapping buttons Now that the codes for all keys have become known, it is necessary to prescribe their functions. There are two ways:
a) find a device among the configs available in Android that is as close as possible to yours, and simply change the button codes in it by copying the contents to your file;
b) prescribe everything yourself.
I went the second way, because my joystick has only a control cross and four buttons. First you need to understand the syntax of the file layout, it is very simple:
key button code action_keyBut then the question arose: what actions of the buttons to write in the config? For many years of using various gamepads on Android, I have determined for myself that the most universal config is the Sony Xperia Play layout, it behaves equally well in games and in the system interface.
Ok Google key mapping for Xperia PlayKey code constant scancode
X 23 KEYCODE_DPAD_CENTER 304
circle 4 KEYCODE_BACK 305
square 99 KEYCODE_BUTTON_X 307
triangle 100 KEYCODE_BUTTON_Y 308
up 19 KEYCODE_DPAD_UP 106
down 20 KEYCODE_DPAD_DOWN 105
left 21 KEYCODE_DPAD_LEFT 103
right 22 KEYCODE_DPAD_RIGHT 108
select 109 KEYCODE_BUTTON_SELECT 314
start 108 KEYCODE_BUTTON_START 28
L trigger 102 KEYCODE_BUTTON_L1 310
R trigger 103 KEYCODE_BUTTON_R1 311
We are interested in the third column, but the record of actions in it is slightly different from those that were found in the system - this is not an obstacle, just remove KEYCODE_.
I register my buttons (remember the sheet of pencil notes? It's time to find it ...):
!!! ATTENTION !!! Need a text editor with support for unix encoding!
Notepad from Windows is NOT suitable,
Use, for example, Notepad ++
key 147 DPAD_CENTER
key 149 BUTTON_B
key 148 BUTTON_Y
key 146 BUTTON_X
key 157 DPAD_DOWN
key 145 DPAD_LEFT
key 155 DPAD_UP
key 156 DPAD_RIGHT
But here it was not without nuances. At Sony, the KEYCODE_BACK action (the “back” button, which is familiar to us, is assigned to the “circle” button). It is easy to guess that pressing such a button in the game can cause a nervous breakdown. And the thing is that during the game on Xplay, this button takes an alternative value - KEYCODE_BUTTON_B. Here we prescribe it.
The offset along the axis is written as:
axis 0x00 HAT_X
axis 0x01 HAT_Y