Zhelyazko Atanasov
05/30/2021, 6:07 PMTextField
/ text input. I'm building an app for mobile phones that have hardware barcode scanners. When the user scans a barcode, the actual barcode is transmitted as a sequence of key events. In a View
world I have an EditText
that is focused, so when a user scans a barcode, its value is entered into the EditText
and I can just do editText.text.toString()
to get the value of the scanned barcode + the user sees what was scanned. As the user is expected to use the barcode scanner for input, the on-screen software keyboard must not be shown. This was easy to do with EditText
.
Now I wanted to replicate the same functionality in Compose using a TextField
. Unfortunately, I can't find a way how to do that. If you specify readOnly = true
, then the CoreTextField
doesn't use the TextInputService
and the TextField is not listening for any input key events. It seems that readOnly
is suited for cases where we'll just display data, like a Calculator app.
Then I went deeper into `CoreTextField`'s implementation and decided to try another approach. I created a composable that didn't use any TextField
. Instead, I got a reference to the LocalTextInputService.current
, created an EditProcessor
instance and called startInput()
like so:
@Composable
fun ScannerInput() {
val textInputService = LocalTextInputService.current
var textFieldValue = remember { mutableStateOf(TextFieldValue()) }
val editProcessor = EditProcessor()
textInputService?.startInput(
value = textFieldValue.value,
imeOptions = ImeOptions.Default.copy(singleLine = true),
onEditCommand = {
val newValue = editProcessor.apply(it)
Log.d("Scanner", "onEditCommand: $newValue")
},
onImeActionPerformed = {
Log.e("Scanner", "onImeActionPerformed: $it")
}
)
}
The thing is that down into the implementation of TextInputService
, it's making a call to showSoftwareKeyboard()
and the keyboard is shown š
Any idea what else I could try, as this is a core part of my app and the requirement is strictly never to show the on-screen software keyboard š¤