Native Support for Input Devices in Android with Their Own Hands
Many owners of Android-devices faced with the fact that the plug on the USB input devices such as joysticks, keyboards, mice, etc., do not always work as expected. That joystick buttons are reversed, then mediaklavishi on the keyboard or the mouse does not work, etc. In my case gamepad defined as a keyboard, and had to use the software “crutch” type USB BT Joystick center or Tincore keymapper. Which, using its IME (input method in Android), allows you to override button. Also use PerApp, which can automatically switch the IME depending on the active application. This method irritated me that when the whole of the economy, I could use your favorite keyboard, and can not be turned off when the joystick to navigate through the interface system. Out I’ve seen only one: to do their native gamepad support in the system or, roughly, “driver”.
The first thing I looked into the folder / system / usr / keylayout, where he found the files with very evocative names:
Vendor_xxxx_Product_xxxx.kl, where xxxx – is a set of numbers and letters. Inasmuch as many have guessed, this is not nothing but a device identifiers (VID and PID). Opening the first indexed file in a text editor, I almost cried, “BINGO!”. But I cried, because the need to work, not sticks to the plate screwed. And that’s what I saw:
# Classic NES Controller
key 289 BUTTON_A
key 290 BUTTON_B
key 297 BUTTON_START
key 296 BUTTON_SELECT
axis 0x00 HAT_X
axis 0x01 HAT_Y
Yes, it is setting buttons for joystick from Nintendo (NES). View adjacent files, I found configs for different devices. Basically Game Controllers (Play Station 3, X-box, ASUS gamepad and others), but there is also the keyboard.
Based on the foregoing, it is already possible to realize that to add support for the device at the system level is necessary to create a file with the VID and PID in the name of the device and provide it with the value buttons.
From theory to practice
The device on Android 4+ (tested only on 5.0 and 4.4);
Device Support USB OTG;
the presence of root-access;
file manager with root (in my case it is Total Commander);
text editor (in my case it is again Total Sommander);
a sheet of paper and a pencil;
USB Device Info (to determine the properties of the device);
KeyTest (to display the codes of buttons the device).
1. Create a file with the VID and PID
We connect our device input and start the application USB Device Info. We are interested tab Linux, in which you select your device (for me it was the last in the list) and are looking for value lines Vendor ID and Product ID. In my case it was 0c45 and 7700, respectively.
Copy any file from the / system / usr / keylayout on the sdcard and rename it according to the findings, the joystick for my name happened so: Vendor_0c45_Product_7700.kl. (Note that the letter case is important)
2. Getting the code buttons
Run the application KeyTest and armed with paper and pencil, starting alternately press the buttons on the device by writing the numbers displayed after scanCode =. I wrote code, placing them on paper as a button on the controller. Because the program can be accessed only by the button “Home”.
3. Mapping buttons
Now, when it became known the codes of all the keys you need to register their function. There are two ways:
a) to find among the available Android device configs closest to you, and just change it in the codes of buttons, copy the contents into a file;
b) to register all himself.
I went to the second way, because my joystick control has only spider and four buttons. First you need to understand the syntax of the file layout, it is very simple:
key kod_knopki deystvie_knopki
But then the question arose: what action buttons to write in config? Over the years, the use of different gamepads on Android for myself, I found that the most versatile configuration – a layout of the Sony Xperia Play, it is equally well-behaved in the games and in the system interface.
Ok Google, key mapping for Xperia Play
Key code constant scancode
304 X 23 KEYCODE_DPAD_CENTER
circle 305 4 KEYCODE_BACK
99 307 square KEYCODE_BUTTON_X
KEYCODE_BUTTON_Y triangle 100 308
up 19 KEYCODE_DPAD_UP 106
down 20 KEYCODE_DPAD_DOWN 105
KEYCODE_DPAD_LEFT left 21 103
right KEYCODE_DPAD_RIGHT 22 108
select KEYCODE_BUTTON_SELECT 109 314
start 108 28 KEYCODE_BUTTON_START
L trigger KEYCODE_BUTTON_L1 102 310
R trigger KEYCODE_BUTTON_R1 103 311
We are interested in the third column, but the record of action in it is slightly different from those that were found in the system – it is not a problem, simply remove KEYCODE_.
Prescribes their buttons (remember sheet with pencil notes? It’s time to find it …)
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, not without nuances. The Sony on the “circle» (circle) scheduled action KEYCODE_BACK (familiar to us “Back” button). It is not difficult to guess that such pressing a button in the game can cause a nervous breakdown. And the thing is that when you play the Xplay this button takes alternative value – KEYCODE_BUTTON_B. That’s it we prescribe.
4. Moment of Truth
Save your file and copy it into the folder / system / usr / keylayout. Expose rights -rw -r -r (644). Restarting the device (I myself did not do it, just lock and unlock, but the purity of conscience, and the experiment had to write it).
We connect our device, and if done correctly, can instantly “crosspiece” to navigate through the menu Android. The button that was appointed DPAD_CENTER, will select the button, and BUTTON_B – “Back” button.
The tablet was less than in the two programs;
joystick is now a real Plug and Play;
You can play games with Market without dancing with a tambourine (if the game supports gamepads);
You can navigate through the interface of Android;
your settings for each device;
increased speed of response buttons (placebo?).
Customize you can not only press but also the displacement of the axes (analog controllers, mouse).