Android KeyEvent down and up triggered simultaneously

42 views Asked by At

I'm trying to use the keyboard down event and up for use in a game where you can move. The idea is press down to start moving and up to stop moving which is simple enough.

The problem I'm having is that on the same millisecond that the DOWN is triggered another UP is triggered as well.

This is the code I have in the Activity

    @Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (!keyBoardEnabled)
        return super.dispatchKeyEvent(event);


    Log.d("KeyEvent", "Key" + keyEvent(event.getAction()) +
        ": KeyCode=" + event.getKeyCode() +
        ", Repeat=" + event.getRepeatCount() +
        ", Action=" + event.getAction() +
        ", UnicodeChar=" + event.getUnicodeChar() +
        ", Source=" + event.getSource() +
        ", DeviceId=" + event.getDeviceId() +
        ", ScanCode=" + event.getScanCode() +
        ", MetaState=" + event.getMetaState() +
        ", Flags=" + event.getFlags() +
        ", TimeStamp=" + event.getEventTime());

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (onKeyDown2(event.getKeyCode()))
            return true;
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        if (onKeyUp2(event.getKeyCode()))
            return true;
    }
    return super.dispatchKeyEvent(event);
}

I have been debugging this for a while now and is not getting anywhere, check below for some logs from me holding down a button:

2024-01-13 16:18:20.396 12244-12244/MYAPP D/KeyEvent: KeyDown: KeyCode=47, Repeat=0, Action=0, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569652
2024-01-13 16:18:20.396 12244-12244/MYAPP D/KeyEvent: KeyUp: KeyCode=47, Repeat=0, Action=1, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569652
2024-01-13 16:18:20.427 12244-12244/MYAPP D/KeyEvent: KeyDown: KeyCode=47, Repeat=0, Action=0, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569683
2024-01-13 16:18:20.428 12244-12244/MYAPP D/KeyEvent: KeyUp: KeyCode=47, Repeat=0, Action=1, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569683
2024-01-13 16:18:20.475 12244-12244/MYAPP D/KeyEvent: KeyDown: KeyCode=47, Repeat=0, Action=0, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569731
2024-01-13 16:18:20.475 12244-12244/MYAPP D/KeyEvent: KeyUp: KeyCode=47, Repeat=0, Action=1, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569731
2024-01-13 16:18:20.506 12244-12244/MYAPP D/KeyEvent: KeyDown: KeyCode=47, Repeat=0, Action=0, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569762
2024-01-13 16:18:20.506 12244-12244/MYAPP D/KeyEvent: KeyUp: KeyCode=47, Repeat=0, Action=1, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569762
2024-01-13 16:18:20.538 12244-12244/MYAPP D/KeyEvent: KeyDown: KeyCode=47, Repeat=0, Action=0, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569794
2024-01-13 16:18:20.538 12244-12244/MYAPP D/KeyEvent: KeyUp: KeyCode=47, Repeat=0, Action=1, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569794
2024-01-13 16:18:20.569 12244-12244/MYAPP D/KeyEvent: KeyDown: KeyCode=47, Repeat=0, Action=0, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569826
2024-01-13 16:18:20.570 12244-12244/MYAPP D/KeyEvent: KeyUp: KeyCode=47, Repeat=0, Action=1, UnicodeChar=115, Source=257, DeviceId=0, ScanCode=31, MetaState=0, Flags=8, TimeStamp=2569826

I'm using a physicals keyboard against the Emulator using Android studio, I have tested the keyboard and it does not do this anywhere else. Min SDK is 19 and compile is 33, have tested with a few different emulator devices using API 25 and 28 but no difference. This is the only place I use the keyboard events in this app Any suggestions would be appriciated.

0

There are 0 answers