New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Physical keyboard to android support #6097
Changes from 6 commits
bbc2417
22165ec
b3e2ab0
28c1180
26e77ba
9a903a1
d900f2c
dd33c0e
cbe9b9c
07bdf02
67b2a43
e77edc5
9076f21
7b24835
70dd3f3
6fdce63
9521ac6
47d57dd
100967c
0a94b74
c49853e
b155cd9
409d5b1
6d82724
e474b59
b522de3
32a29a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,21 @@ import android.accessibilityservice.AccessibilityService | |
import android.accessibilityservice.GestureDescription | ||
import android.graphics.Path | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.os.Handler | ||
import android.os.Looper | ||
import android.util.Log | ||
import android.widget.EditText | ||
import android.view.accessibility.AccessibilityEvent | ||
import android.view.accessibility.AccessibilityNodeInfo | ||
import android.accessibilityservice.AccessibilityServiceInfo | ||
import android.accessibilityservice.AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR | ||
import androidx.annotation.RequiresApi | ||
import java.util.* | ||
import kotlin.math.abs | ||
import kotlin.math.max | ||
import hbb.MessageOuterClass.KeyEvent; | ||
import hbb.KeyEventConverter; | ||
|
||
const val LIFT_DOWN = 9 | ||
const val LIFT_MOVE = 8 | ||
|
@@ -252,9 +261,27 @@ class InputService : AccessibilityService() { | |
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.N) | ||
fun onKeyEvent(data: ByteArray) { | ||
val keyEvent = KeyEvent.parseFrom(data); | ||
getInputMethod()?.let { inputMethod -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getInputMethod added in API level 33 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may have some modification to make it compatible with lower level target device. @mcfans There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked the wrong field, I thought we were using API 33. My bad. So I just keep this for higher version and add the old code back for lower API? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not familiar with kotlin about how to call higher API on high target device but still can run on low target device. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My android device may be the lower version. The input does not work now, and it reports:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you try it again? I added old code back with a version check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The keyboard input works now. |
||
inputMethod.getCurrentInputConnection()?.let { inputConnection -> | ||
KeyEventConverter.toAndroidKeyEvent(keyEvent).let { event -> | ||
inputConnection.sendKeyEvent(event) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
override fun onAccessibilityEvent(event: AccessibilityEvent) {} | ||
|
||
override fun onServiceConnected() { | ||
super.onServiceConnected() | ||
ctx = this | ||
val info = AccessibilityServiceInfo() | ||
info.flags = FLAG_INPUT_METHOD_EDITOR | ||
setServiceInfo(info) | ||
Log.d(logTag, "onServiceConnected!") | ||
} | ||
|
||
|
@@ -263,7 +290,5 @@ class InputService : AccessibilityService() { | |
super.onDestroy() | ||
} | ||
|
||
override fun onAccessibilityEvent(event: AccessibilityEvent?) {} | ||
|
||
override fun onInterrupt() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package hbb; | ||
import android.view.KeyEvent | ||
import android.view.KeyCharacterMap | ||
import hbb.MessageOuterClass.KeyboardMode | ||
import hbb.MessageOuterClass.ControlKey | ||
|
||
object KeyEventConverter { | ||
fun toAndroidKeyEvent(keyEventProto: hbb.MessageOuterClass.KeyEvent): KeyEvent { | ||
var chrValue = 0 | ||
var modifiers = 0 | ||
|
||
val keyboardMode = keyEventProto.getMode() | ||
|
||
if (keyEventProto.hasChr()) { | ||
if (keyboardMode == KeyboardMode.Map || keyboardMode == KeyboardMode.Translate) { | ||
chrValue = keyEventProto.getChr() | ||
} else { | ||
chrValue = convertUnicodeToKeyCode(keyEventProto.getChr() as Int) | ||
} | ||
} else if (keyEventProto.hasControlKey()) { | ||
chrValue = convertControlKeyToKeyCode(keyEventProto.getControlKey()) | ||
} | ||
|
||
var modifiersList = keyEventProto.getModifiersList() | ||
|
||
if (modifiersList != null) { | ||
for (modifier in keyEventProto.getModifiersList()) { | ||
val modifierValue = convertModifier(modifier) | ||
modifiers = modifiers or modifierValue | ||
} | ||
} | ||
|
||
var action = 0 | ||
if (keyEventProto.getDown()) { | ||
action = KeyEvent.ACTION_DOWN | ||
} else { | ||
action = KeyEvent.ACTION_UP | ||
} | ||
|
||
return KeyEvent(0, 0, action, chrValue, 0, modifiers) | ||
} | ||
|
||
private fun convertModifier(controlKey: hbb.MessageOuterClass.ControlKey): Int { | ||
// Add logic to map ControlKey values to Android KeyEvent key codes. | ||
// You'll need to provide the mapping for each key. | ||
return when (controlKey) { | ||
ControlKey.Alt -> KeyEvent.META_ALT_ON | ||
ControlKey.Control -> KeyEvent.META_CTRL_ON | ||
ControlKey.CapsLock -> KeyEvent.META_CAPS_LOCK_ON | ||
ControlKey.Meta -> KeyEvent.META_META_ON | ||
ControlKey.NumLock -> KeyEvent.META_NUM_LOCK_ON | ||
ControlKey.RShift -> KeyEvent.META_SHIFT_RIGHT_ON | ||
ControlKey.Shift -> KeyEvent.META_SHIFT_ON | ||
ControlKey.RAlt -> KeyEvent.META_ALT_RIGHT_ON | ||
ControlKey.RControl -> KeyEvent.META_CTRL_RIGHT_ON | ||
else -> 0 // Default to unknown. | ||
} | ||
} | ||
|
||
private val tag = "KeyEventConverter" | ||
|
||
private fun convertUnicodeToKeyCode(unicode: Int): Int { | ||
val charMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD) | ||
android.util.Log.d(tag, "unicode: $unicode") | ||
val events = charMap.getEvents(charArrayOf(unicode.toChar())) | ||
if (events != null && events.size > 0) { | ||
android.util.Log.d(tag, "keycode ${events[0].keyCode}") | ||
return events[0].keyCode | ||
} | ||
return 0 | ||
} | ||
|
||
private fun convertControlKeyToKeyCode(controlKey: hbb.MessageOuterClass.ControlKey): Int { | ||
// Add logic to map ControlKey values to Android KeyEvent key codes. | ||
// You'll need to provide the mapping for each key. | ||
return when (controlKey) { | ||
ControlKey.Alt -> KeyEvent.KEYCODE_ALT_LEFT | ||
ControlKey.Backspace -> KeyEvent.KEYCODE_DEL | ||
ControlKey.Control -> KeyEvent.KEYCODE_CTRL_LEFT | ||
ControlKey.CapsLock -> KeyEvent.KEYCODE_CAPS_LOCK | ||
ControlKey.Meta -> KeyEvent.KEYCODE_META_LEFT | ||
ControlKey.NumLock -> KeyEvent.KEYCODE_NUM_LOCK | ||
ControlKey.RShift -> KeyEvent.KEYCODE_SHIFT_RIGHT | ||
ControlKey.Shift -> KeyEvent.KEYCODE_SHIFT_LEFT | ||
ControlKey.RAlt -> KeyEvent.KEYCODE_ALT_RIGHT | ||
ControlKey.RControl -> KeyEvent.KEYCODE_CTRL_RIGHT | ||
ControlKey.DownArrow -> KeyEvent.KEYCODE_DPAD_DOWN | ||
ControlKey.LeftArrow -> KeyEvent.KEYCODE_DPAD_LEFT | ||
ControlKey.RightArrow -> KeyEvent.KEYCODE_DPAD_RIGHT | ||
ControlKey.UpArrow -> KeyEvent.KEYCODE_DPAD_UP | ||
ControlKey.End -> KeyEvent.KEYCODE_MOVE_END | ||
ControlKey.Home -> KeyEvent.KEYCODE_MOVE_HOME | ||
ControlKey.Insert -> KeyEvent.KEYCODE_INSERT | ||
ControlKey.Escape -> KeyEvent.KEYCODE_ESCAPE | ||
ControlKey.F1 -> KeyEvent.KEYCODE_F1 | ||
ControlKey.F2 -> KeyEvent.KEYCODE_F2 | ||
ControlKey.F3 -> KeyEvent.KEYCODE_F3 | ||
ControlKey.F4 -> KeyEvent.KEYCODE_F4 | ||
ControlKey.F5 -> KeyEvent.KEYCODE_F5 | ||
ControlKey.F6 -> KeyEvent.KEYCODE_F6 | ||
ControlKey.F7 -> KeyEvent.KEYCODE_F7 | ||
ControlKey.F8 -> KeyEvent.KEYCODE_F8 | ||
ControlKey.F9 -> KeyEvent.KEYCODE_F9 | ||
ControlKey.F10 -> KeyEvent.KEYCODE_F10 | ||
ControlKey.F11 -> KeyEvent.KEYCODE_F11 | ||
ControlKey.F12 -> KeyEvent.KEYCODE_F12 | ||
ControlKey.Space -> KeyEvent.KEYCODE_SPACE | ||
ControlKey.Tab -> KeyEvent.KEYCODE_TAB | ||
ControlKey.Return -> KeyEvent.KEYCODE_ENTER | ||
ControlKey.Delete -> KeyEvent.KEYCODE_FORWARD_DEL | ||
ControlKey.Clear -> KeyEvent.KEYCODE_CLEAR | ||
ControlKey.Pause -> KeyEvent.KEYCODE_BREAK | ||
else -> 0 // Default to unknown. | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please merge to https://github.com/fufesou/rdev
@fufesou please review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will open a PR for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finish this first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fufesou Could you take a look at this?