0

I'm trying to "press a button" programmatically in a Fragment with no success.

Any ideas of how to do it?

This is what I've tried so far:

private fun pressKey(editText: View) {

        // Prepare a list of different events to test //

        val list = listOf(
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_NUMPAD_ENTER, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_NUMPAD_ENTER, 0, 0, 0, 0, KeyEvent.ACTION_UP),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_NUMPAD_ENTER, 0, 0, 0, 0, KeyEvent.ACTION_DOWN),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_ENTER, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_ENTER, 0, 0, 0, 0, KeyEvent.ACTION_UP),
                KeyEvent(0, 0, 0, KeyEvent.KEYCODE_ENTER, 0, 0, 0, 0, KeyEvent.ACTION_DOWN),
                KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_NUMPAD_ENTER),
                KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER),
                KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_NUMPAD_ENTER),
                KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER),
                KeyEvent(KeyEvent.KEYCODE_ENDCALL, KeyEvent.KEYCODE_NUMPAD_ENTER),
                KeyEvent(KeyEvent.KEYCODE_ENDCALL, KeyEvent.KEYCODE_ENTER))

        val eventList = listOf(
                KeyEvent.KEYCODE_ENTER,
                KeyEvent.KEYCODE_NUMPAD_ENTER,
                KeyEvent.KEYCODE_ENTER)

        // Try each one of the events in a different way //

        list.forEach { activity!!.dispatchKeyEvent(it) }
        // similar to above
        list.forEach { view!!.dispatchKeyEvent(it) }
        // similar to above
        list.forEach { editText.dispatchKeyEvent(it) }

        ///////////////////////////////////////
        val inputConnectionEditText = BaseInputConnection(editText, true)
        list.forEach { inputConnectionEditText.sendKeyEvent(it) }
        // similar to above
        val inputConnection = BaseInputConnection(view!!, true)
        list.forEach { inputConnection.sendKeyEvent(it) }

        ///////////////////////////////////////
        val instrumentation = Instrumentation()
//      eventList.forEach { instrumentation.sendKeyDownUpSync(it) } // E/UncaughtException: java.lang.RuntimeException: This method can not be called from the main application thread
    }

Thanks in advance.

off-topic: I'm force to write more text otherwise I'm not allow to publish, I hope there is enough info in the post, if not, just let me know

miguel_rossi
  • 53
  • 2
  • 7
  • 1
    This feels like it could be an [XY Problem](https://en.wikipedia.org/wiki/XY_problem)... why are you trying to "press buttons" instead of just invoking whatever code would be triggered when the buttons are pressed? – TripeHound Jun 13 '19 at 12:50
  • Then you probably want to call something like `inputConnectionEditText.requestFocus()`. _Possibly_ with a call to `setSoftInputMode()` – see [this answer](https://stackoverflow.com/a/14327901/2096401). – TripeHound Jun 13 '19 at 13:33
  • I have several EditText which should have only one number. I implemented a TextWatcher which, when the user introduces a number, moves the focus to the next EditText an when it arrives to the last one triggers a backend call. That was the original idea. I solved it in a different way but still wonder how to "press keys" programmatically. – miguel_rossi Jun 13 '19 at 13:35
  • Sorry, @TripeHound, the response was posted by mistake and couldn't edit it. See the previous one with the whole context. – miguel_rossi Jun 13 '19 at 13:40
  • Then possibly `.performClick()` – see [this question and answers](https://stackoverflow.com/q/4553374/2096401) or [this one](https://stackoverflow.com/q/18154203/2096401) (but I'm only going by what they say... I've not tried it). – TripeHound Jun 13 '19 at 13:52

0 Answers0