adding an initial implementation of the keyboard

This commit is contained in:
tibbi 2022-01-05 17:15:08 +01:00
parent 4c0fdfda81
commit 4fca061d12
8 changed files with 267 additions and 13 deletions

View file

@ -24,7 +24,7 @@
<activity android:name=".activities.MainActivity" />
<service
android:name=".helpers.SimpleKeyboardIME"
android:name=".services.SimpleKeyboardIME"
android:label="@string/app_name"
android:permission="android.permission.BIND_INPUT_METHOD">
<meta-data

View file

@ -1,7 +0,0 @@
package com.simplemobiletools.keyboard.helpers
import android.inputmethodservice.InputMethodService
public class SimpleKeyboardIME : InputMethodService() {
}

View file

@ -0,0 +1,70 @@
package com.simplemobiletools.keyboard.services
import android.inputmethodservice.InputMethodService
import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import com.simplemobiletools.keyboard.R
// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
class SimpleKeyboardIME : InputMethodService(), KeyboardView.OnKeyboardActionListener {
private var keyboard: Keyboard? = null
private var keyboardView: KeyboardView? = null
private var caps = false
override fun onCreateInputView(): View {
keyboardView = layoutInflater.inflate(R.layout.keyboard_view, null) as KeyboardView
keyboard = Keyboard(this, R.xml.keys_layout)
keyboardView!!.keyboard = keyboard
keyboardView!!.setOnKeyboardActionListener(this)
return keyboardView!!
}
override fun onPress(primaryCode: Int) {}
override fun onRelease(primaryCode: Int) {}
override fun onKey(primaryCode: Int, keyCodes: IntArray?) {
val inputConnection = currentInputConnection
if (inputConnection != null) {
when (primaryCode) {
Keyboard.KEYCODE_DELETE -> {
val selectedText = inputConnection.getSelectedText(0)
if (TextUtils.isEmpty(selectedText)) {
inputConnection.deleteSurroundingText(1, 0)
} else {
inputConnection.commitText("", 1)
}
caps = !caps
keyboard!!.isShifted = caps
keyboardView!!.invalidateAllKeys()
}
Keyboard.KEYCODE_SHIFT -> {
caps = !caps
keyboard!!.isShifted = caps
keyboardView!!.invalidateAllKeys()
}
Keyboard.KEYCODE_DONE -> inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER))
else -> {
var code = primaryCode.toChar()
if (Character.isLetter(code) && caps) {
code = Character.toUpperCase(code)
}
inputConnection.commitText(code.toString(), 1)
}
}
}
}
override fun onText(text: CharSequence?) {}
override fun swipeLeft() {}
override fun swipeRight() {}
override fun swipeDown() {}
override fun swipeUp() {}
}

View file

@ -17,4 +17,12 @@
android:text="@string/change_keyboard" />
</RelativeLayout>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/text_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/change_keyboard_holder"
android:layout_margin="@dimen/activity_margin" />
</RelativeLayout>

View file

@ -0,0 +1,7 @@
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_primary_dark"
android:textAlignment="center"
android:textColor="@color/colorPrimary"
android:textSize="30sp" />

View file

@ -0,0 +1,7 @@
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:keyPreviewLayout="@layout/keyboard_preview" />

View file

@ -0,0 +1,172 @@
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="3px"
android:keyWidth="10%p"
android:keyHeight="60dp"
android:keyBackground="@color/color_primary_dark"
android:keyTextColor="@color/color_accent"
android:verticalGap="3px">
<Row>
<Key
android:codes="49"
android:keyEdgeFlags="left"
android:keyLabel="1" />
<Key
android:codes="50"
android:keyLabel="2" />
<Key
android:codes="51"
android:keyLabel="3" />
<Key
android:codes="52"
android:keyLabel="4" />
<Key
android:codes="53"
android:keyLabel="5" />
<Key
android:codes="54"
android:keyLabel="6" />
<Key
android:codes="55"
android:keyLabel="7" />
<Key
android:codes="56"
android:keyLabel="8" />
<Key
android:codes="57"
android:keyLabel="9" />
<Key
android:codes="48"
android:keyLabel="0" />
<Key
android:codes="-5"
android:isRepeatable="true"
android:keyWidth="20%p"
android:keyEdgeFlags="right"
android:keyLabel="DEL" />
</Row>
<Row>
<Key
android:codes="113"
android:keyEdgeFlags="left"
android:keyLabel="q" />
<Key
android:codes="119"
android:keyLabel="w" />
<Key
android:codes="101"
android:keyLabel="e" />
<Key
android:codes="114"
android:keyLabel="r" />
<Key
android:codes="116"
android:keyLabel="t" />
<Key
android:codes="121"
android:keyLabel="y" />
<Key
android:codes="117"
android:keyLabel="u" />
<Key
android:codes="105"
android:keyLabel="i" />
<Key
android:codes="111"
android:keyLabel="o" />
<Key
android:codes="112"
android:keyEdgeFlags="right"
android:keyLabel="p" />
</Row>
<Row>
<Key
android:codes="97"
android:keyEdgeFlags="left"
android:keyLabel="a" />
<Key
android:codes="115"
android:keyLabel="s" />
<Key
android:codes="100"
android:keyLabel="d" />
<Key
android:codes="102"
android:keyLabel="f" />
<Key
android:codes="103"
android:keyLabel="g" />
<Key
android:codes="104"
android:keyLabel="h" />
<Key
android:codes="106"
android:keyLabel="j" />
<Key
android:codes="107"
android:keyLabel="k" />
<Key
android:codes="108"
android:keyLabel="l" />
<Key
android:codes="35,64"
android:keyEdgeFlags="right"
android:keyLabel="\# \@" />
</Row>
<Row>
<Key
android:codes="-1"
android:keyEdgeFlags="left"
android:keyLabel="CAPS" />
<Key
android:codes="122"
android:keyLabel="z" />
<Key
android:codes="120"
android:keyLabel="x" />
<Key
android:codes="99"
android:keyLabel="c" />
<Key
android:codes="118"
android:keyLabel="v" />
<Key
android:codes="98"
android:keyLabel="b" />
<Key
android:codes="110"
android:keyLabel="n" />
<Key
android:codes="109"
android:keyLabel="m" />
<Key
android:codes="46"
android:keyLabel="." />
<Key
android:codes="63,33,58"
android:keyEdgeFlags="right"
android:keyLabel="\? ! :" />
</Row>
<Row android:rowEdgeFlags="bottom">
<Key
android:codes="44"
android:keyWidth="10%p"
android:keyEdgeFlags="left"
android:keyLabel="," />
<Key
android:codes="47"
android:keyWidth="10%p"
android:keyLabel="/" />
<Key
android:codes="32"
android:isRepeatable="true"
android:keyWidth="60%p"
android:keyLabel="SPACE" />
<Key
android:codes="-4"
android:keyWidth="20%p"
android:keyBackground="@color/colorPrimary"
android:keyEdgeFlags="right"
android:keyLabel="DONE" />
</Row>
</Keyboard>

View file

@ -1,11 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android"
android:icon="@mipmap/ic_launcher"
android:settingsActivity="com.simplemobiletools.keyboard.activities.SettingsActivity"
android:supportsSwitchingToNextInputMethod="true">
android:settingsActivity="com.simplemobiletools.keyboard.activities.SettingsActivity">
<subtype
android:imeSubtypeMode="Keyboard"
android:label="english" />
<subtype android:imeSubtypeMode="Keyboard" />
</input-method>