5

In my current View, a keyboard is open with numbers 0 to 9, a delete key and an enter key. I added both an onKeyDown and an onKeyUp event. Both should log the keycode to the debug console when pressed.

@Override
public boolean onKeyDown(int pKeyCode, KeyEvent pKeyEvent) {
    Log.d("KEYDOWN", pKeyCode+"");
    return true; // also tried false
}
@Override
public boolean onKeyUp(int pKeyCode, KeyEvent pKeyEvent) {
    Log.d("KEYUP", pKeyCode+"");
    return true;
}

I pressed every single key on the keyboard with the following result:

D/KEYUP: 8
D/KEYUP: 9
D/KEYUP: 10
D/KEYUP: 11
D/KEYUP: 12
D/KEYUP: 13
D/KEYUP: 14
D/KEYUP: 15
D/KEYUP: 16
D/KEYDOWN: 67
D/KEYUP: 67
D/KEYUP: 7
D/KEYUP: 66

Can someone explain why the keydown event is never triggered for 0-9 and enter? I really need it to trigger when 0-9 are pressed.

EDIT: I added a solution that fixed the issue by replacing both functions with dispatchKeyEvent. This doesn't explain why onKeyDowndidn't recognize those keys, though.

Someone got an answer for this?

keinabel
  • 1,002
  • 3
  • 15
  • 33

2 Answers2

4

I only found a solution to work around this issue, but I did not find the reason WHY this happens in the first place.

This code did the job:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction()==KeyEvent.ACTION_DOWN) {
        Log.d("KEYDOWN", event.getKeyCode()+"");
    }
    else if (event.getAction()==KeyEvent.ACTION_UP) {
        Log.d("KEYUP", event.getKeyCode()+"");
    }
    Log.d("KEY", event.getKeyCode()+"");
    return false;
}
keinabel
  • 1,002
  • 3
  • 15
  • 33
  • What exact View you are using? – Yurii Tsap Oct 17 '16 at 17:27
  • I am quite new to Android, so I actually do not really know how to answer your question @YuriiTsap. I just created a standard project in Android Studio and this is the MainActivity. Don't know what View is used there. – keinabel Oct 18 '16 at 15:04
  • Please describe your exact problem or what you are trying to solve. – Yurii Tsap Oct 19 '16 at 05:21
  • I am logging sensor data during runtime. And in this data log I need to capture what keys are being (if any) pressed for each of the logged data entries. Since, theoretically, more than one data entry could be logged during a key-press, I need both the `onKeyDown` and `onKeyUp` events, so I can enable/disable a flag that will then put the corresponding keys into my data entry. – keinabel Oct 19 '16 at 12:00
  • Have you tried to override EditText and handle this inside of it? – Yurii Tsap Oct 19 '16 at 12:20
1

To understand the workflow you need to dive inside the code a little bit, it's not so obvious but nevertheless. Your particular case depends on which view you are extending and the IME. From docs :

In general, the framework cannot guarantee that the key events it delivers to a view always constitute complete key sequences since some events may be dropped or modified by containing views before they are delivered. The view implementation should be prepared to handle FLAG_CANCELED and should tolerate anomalous situations such as receiving a new ACTION_DOWN without first having received an ACTION_UP for the prior key press.

You can use TextWatcher - this is a good solution, but I'm not aware of your needs. In any case if you are overriding onKeyDown() - it will be triggered, and you need to handle it by yourself or call parent implementation. In the case with EditText - onKeyDown will be handled by TextView, despite few cases(Back Button i.e.).

Yurii Tsap
  • 3,554
  • 3
  • 24
  • 33