mirror of
https://github.com/SimpleMobileTools/Simple-Keyboard.git
synced 2025-06-05 21:49:26 +02:00
adding an initial implementation of the keyboard
This commit is contained in:
@ -1,7 +0,0 @@
|
||||
package com.simplemobiletools.keyboard.helpers
|
||||
|
||||
import android.inputmethodservice.InputMethodService
|
||||
|
||||
public class SimpleKeyboardIME : InputMethodService() {
|
||||
|
||||
}
|
@ -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() {}
|
||||
}
|
Reference in New Issue
Block a user