Skip to content

Commit

Permalink
Fix and test
Browse files Browse the repository at this point in the history
Better formatted tests
  • Loading branch information
dkwingsmt committed May 30, 2022
1 parent ae0897f commit 4d6ae33
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,27 @@ public KeyEmbedderResponder(BinaryMessenger messenger) {
}
}

private static long keyOfPlane(long key, long plane) {
// Apply '& kValueMask' in case the key is a negative number before being converted to long.
return plane | (key & KeyboardMap.kValueMask);
}

// Get the physical key for this event.
//
// The returned value is never null.
private Long getPhysicalKey(@NonNull KeyEvent event) {
final Long byMapping = KeyboardMap.scanCodeToPhysical.get((long) event.getScanCode());
final long scancode = event.getScanCode();
// Scancode 0 can occur during emulation using `adb shell input keyevent`. Synthesize a physical
// key from the key code so that keys can be told apart.
if (scancode == 0) {
// The key code can't also be 0, since those events have been filtered.
return keyOfPlane(event.getKeyCode(), KeyboardMap.kLogicalPlane);
}
final Long byMapping = KeyboardMap.scanCodeToPhysical.get(scancode);
if (byMapping != null) {
return byMapping;
}
return KeyboardMap.kAndroidPlane & event.getScanCode();
return keyOfPlane(event.getScanCode(), KeyboardMap.kAndroidPlane);
}

// Get the logical key for this event.
Expand All @@ -77,7 +89,7 @@ private Long getLogicalKey(@NonNull KeyEvent event) {
if (byMapping != null) {
return byMapping;
}
return KeyboardMap.kAndroidPlane & event.getKeyCode();
return keyOfPlane(event.getKeyCode(), KeyboardMap.kAndroidPlane);
}

// Update `pressingRecords`.
Expand Down Expand Up @@ -241,6 +253,10 @@ void synchronizeTogglingKey(
// Returns whether any events are dispatched.
private boolean handleEventImpl(
@NonNull KeyEvent event, @NonNull OnKeyEventHandledCallback onKeyEventHandledCallback) {
// Events with no codes at all can not be recognized.
if (event.getScanCode() == 0 && event.getKeyCode() == 0) {
return false;
}
final Long physicalKey = getPhysicalKey(event);
final Long logicalKey = getLogicalKey(event);

Expand Down Expand Up @@ -324,7 +340,9 @@ private void synthesizeEvent(boolean isDown, Long logicalKey, Long physicalKey,
output.physicalKey = physicalKey;
output.character = null;
output.synthesized = true;
updatePressingState(physicalKey, isDown ? logicalKey : null);
if (physicalKey != 0 && logicalKey != 0) {
updatePressingState(physicalKey, isDown ? logicalKey : null);
}
sendKeyEvent(output, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,5 +614,6 @@ public static TogglingGoal[] getTogglingGoals() {

public static final long kValueMask = 0x000ffffffffL;
public static final long kUnicodePlane = 0x00000000000L;
public static final long kLogicalPlane = 0x00300000000L;
public static final long kAndroidPlane = 0x01100000000L;
}
Loading

0 comments on commit 4d6ae33

Please sign in to comment.