Customize the layout of the external keyboard on Android without root

I like the keyboard layout on Mac: Cmd (Ctrl) under the thumb and the ability, without shamanism, to change the behavior of CapsLock directly in the settings. The same result is easily achieved in Linux using setxkbmapthe console or, for example, gnome-tweak-toolin the UI. But what if the keyboard connects to Android?



In Android, there are several ways to customize an external keyboard:


  1. Installing a third-party keyboard. For example, External Keyboard Helper .
  2. / kl kcm ( root). , , .
  3. , .

. β€” . .



.


Key Layout


Key layout (.kl) (Linux Key Code), .. , , (Android  Key), .. TAB, ENTER F. - . , , , , Gamepad Tester.


Key Character Map


Key Character Map (.kcm) , , English(US).



4.1 Android . Settings -> Language & input -> Physical keyboard. , , " ".



:


  • Esc CapsLock.
  • Ctrl/Win/Alt Win/Alt/Ctrl Alt/PrintScreen/Ctrl Ctrl/Alt/Ctrl .
  • Alt+Tab Ctrl+Tab.
  • Ctrl+Shift+3.
  • Win+Space.
  • .


.. ( Ctrl CapsLock, Vim?), " ", apk-. custom-keyboard-layout β€” Android.



git clone git@github.com:ris58h/custom-keyboard-layout.git

app/src/main/AndroidManifest.xml:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ris58h.custom_keyboard_layout">

    <application android:label="@string/app_name">
        <receiver
            android:name=".InputDeviceReceiver"
            android:label="@string/keyboard_layouts_label">
            <intent-filter>
                <action android:name="android.hardware.input.action.QUERY_KEYBOARD_LAYOUTS" />
            </intent-filter>
            <meta-data
                android:name="android.hardware.input.metadata.KEYBOARD_LAYOUTS"
                android:resource="@xml/keyboard_layouts" />
        </receiver>
    </application>
</manifest>

reciever. , ( InputDeviceReceiver) β€” , . reciever , app/src/main/res/xml/keyboard_layouts.xml:


<?xml version="1.0" encoding="utf-8"?>
<keyboard-layouts xmlns:android="http://schemas.android.com/apk/res/android">
    <keyboard-layout
        android:name="keyboard_layout_en_us"
        android:keyboardLayout="@raw/keyboard_layout_en_us"
        android:label="@string/keyboard_layout_en_us_label" />
</keyboard-layouts>

β€” keyboard_layout_en_us.



app/src/main/res/raw/keyboard_layout_en_us.kcm , :


type OVERLAY

, , - Generic.kcm. .. β€” .


Key Layout . kcm-, , , Ctrl Alt kl-. : map kl- kcm-.


keyboard_layout_en_us.kcm :


type OVERLAY

map key 58  ESCAPE

map key 29  META_LEFT
map key 56  CTRL_LEFT
map key 125 ALT_LEFT

map key 99  ALT_RIGHT
map key 100 CTRL_RIGHT

key TAB {
    label:                              '\t'
    base:                               '\t'
    ctrl:                               fallback APP_SWITCH
}

key 3 {
    label:                              '3'
    base:                               '3'
    shift:                              '#'
    ctrl+shift:                         fallback SYSRQ
}

, Win+Space β€” .



, English(US), kcm- , . . keyboard_layout_russian.kcm, app/src/main/res/raw/ , , app/src/main/res/xml/keyboard_layouts.xml:


<?xml version="1.0" encoding="utf-8"?>
<keyboard-layouts xmlns:android="http://schemas.android.com/apk/res/android">
    <keyboard-layout
        android:name="keyboard_layout_en_us"
        android:keyboardLayout="@raw/keyboard_layout_en_us"
        android:label="@string/keyboard_layout_en_us_label" />
    <keyboard-layout
        android:name="keyboard_layout_ru"
        android:keyboardLayout="@raw/keyboard_layout_ru"
        android:label="@string/keyboard_layout_ru_label" />
</keyboard-layouts>

keyboard_layout_ru_label app/src/main/res/values/strings.xml.
, , . '3', , :


key 3 {
    label:                              '3'
    base:                               '3'
    shift:                              '\u2116'
    ralt:                               '#'
    ctrl+shift:                         fallback SYSRQ
}

Vendor_17ef_Product_6048.



We collect and install our application. The easiest way to do this is with Android Studio following the official documentation .


If everything is done correctly, then Settings -> Language & input -> Physical keyboardour layouts will appear, and in the list of applications - Custom Keyboard Layout.


Conclusion


Customization of an external keyboard without root is possible. Not all Wishlist are achievable at the same time: switching languages ​​on Win + Space did not work, but this can be a firmware problem.


The article is purposely made brief - all the details can be found on the links.


All Articles