Jeffrey
Jeffrey

Reputation: 1

(Android)Tracking the time between key release and key press in a textEdit

I'm trying to measure the time that a key is pressed in a textbox, and nothing I have tried has worked. I plan on getting the time when the keyevent is triggered for keydown and when the keyevent is triggered for keyup and subtracting them. I've tried using onKeyUp and onKeyDown, but those do not work for textboxes. So, I pivoted towards an OnKeyListener, but that only seems to catch the ACTION_UP and not ACTION_DOWN.

Here is my code

        EditText journalText = (EditText) findViewById(R.id.journalText);
        journalText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN) {
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_E:
                            Date date = new Date();
                            long time = date.getTime();
                            Log.d("apple", "Time: " + time);
                            timedown.add(Long.toString(time));
                            Log.d("apple", "E was pressed Down");
                    }
                }
                else if(event.getAction() == KeyEvent.ACTION_UP){
                    switch(keyCode){
                        case KeyEvent.KEYCODE_R:
                            Date date = new Date();
                            long time = date.getTime();
                            Log.d("apple", "Time: " + time);
                            timedown.add(Long.toString(time));
                            Log.d("apple", "R was pressed Up");
                    }
                }
                return false;
            }
        });

The result of alternating r and e presses only show the 'R was pressed Up' output and no E was pressed down.

When using onKeyUp and onKeyDown on different pages that do not have text boxes, both types of events are registered, so I do not think this issue is caused by the soft keyboard in Android Studio.

Upvotes: 0

Views: 768

Answers (1)

Jean
Jean

Reputation: 10610

What you have already works for hardware keyboards.

But you can't do this with on-screen keyboards in Android, because while software keyboards may present themselves like hardware keyboards, they are under no obligation to, and so they don't work with keypresses but with batch input. See InputConnection.

Simply put, you can't assume the user is typing with something that has keys, and Android apps are expected to work with the user's choice of keyboard, so Android does not let you force users to input text in the way you'd prefer them to.

Some questions you'd need to answer :

  • How would your program work with gesture typing ? (swipe, glide typing, however you choose to call it)
  • How would your program work with flick input, where users press a key and flick in one of the four directions to input a char ?
  • How would your program work with handwriting keyboards ?
  • How would your program work with dictation ?
  • How would your program work with transliteration IMEs, which more than half the world population uses ?

For these styles of input, the notion of a keypress doesn't make sense, and requiring the keyboard authors to simulate keypresses would limit inventing new, better input methods.

If your answer is that users of these keyboards can't use your app, then Android says you should change your app to support these users.

Upvotes: 0

Related Questions