Skip to content

Commit

Permalink
Added support for mapping key combos on devices with a hardware keybo…
Browse files Browse the repository at this point in the history
…ard, Fixes termux#731.

  Fn+Left  -> Home
  Fn+Right -> End
  Fn+Up    -> Page Up
  Fn+Down  -> Page Down
  • Loading branch information
zevv committed Jul 25, 2018
1 parent fdae272 commit 50fe003
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public final class KeyHandler {
public static final int KEYMOD_ALT = 0x80000000;
public static final int KEYMOD_CTRL = 0x40000000;
public static final int KEYMOD_SHIFT = 0x20000000;
public static final int KEYMOD_FUNCTION = 0x10000000;

private static final Map<String, Integer> TERMCAP_TO_KEYCODE = new HashMap<>();

Expand Down Expand Up @@ -154,13 +155,29 @@ public static String getCode(int keyCode, int keyMode, boolean cursorApp, boolea
return "\015";

case KEYCODE_DPAD_UP:
return (keyMode == 0) ? (cursorApp ? "\033OA" : "\033[A") : transformForModifiers("\033[1", keyMode, 'A');
if((keyMode & KEYMOD_FUNCTION) != 0) {
return transformForModifiers("\033[5", keyMode, '~');
} else {
return (keyMode == 0) ? (cursorApp ? "\033OA" : "\033[A") : transformForModifiers("\033[1", keyMode, 'A');
}
case KEYCODE_DPAD_DOWN:
return (keyMode == 0) ? (cursorApp ? "\033OB" : "\033[B") : transformForModifiers("\033[1", keyMode, 'B');
if((keyMode & KEYMOD_FUNCTION) != 0) {
return transformForModifiers("\033[6", keyMode, '~');
} else {
return (keyMode == 0) ? (cursorApp ? "\033OB" : "\033[B") : transformForModifiers("\033[1", keyMode, 'B');
}
case KEYCODE_DPAD_RIGHT:
return (keyMode == 0) ? (cursorApp ? "\033OC" : "\033[C") : transformForModifiers("\033[1", keyMode, 'C');
if((keyMode & KEYMOD_FUNCTION) != 0) {
return ((keyMode & ~KEYMOD_FUNCTION) == 0) ? "\033[F" : transformForModifiers("\033[1", keyMode, 'F');
} else {
return (keyMode == 0) ? (cursorApp ? "\033OC" : "\033[C") : transformForModifiers("\033[1", keyMode, 'C');
}
case KEYCODE_DPAD_LEFT:
return (keyMode == 0) ? (cursorApp ? "\033OD" : "\033[D") : transformForModifiers("\033[1", keyMode, 'D');
if((keyMode & KEYMOD_FUNCTION) != 0) {
return ((keyMode & ~KEYMOD_FUNCTION) == 0) ? "\033[H" : transformForModifiers("\033[1", keyMode, 'H');
} else {
return (keyMode == 0) ? (cursorApp ? "\033OD" : "\033[D") : transformForModifiers("\033[1", keyMode, 'D');
}

case KEYCODE_MOVE_HOME:
// Note that KEYCODE_HOME is handled by the system and never delivered to applications.
Expand Down Expand Up @@ -283,7 +300,7 @@ public static String getCode(int keyCode, int keyMode, boolean cursorApp, boolea

private static String transformForModifiers(String start, int keymod, char lastChar) {
int modifier;
switch (keymod) {
switch (keymod & (KEYMOD_SHIFT | KEYMOD_ALT | KEYMOD_CTRL)) {
case KEYMOD_SHIFT:
modifier = 2;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
if (controlDownFromEvent) keyMod |= KeyHandler.KEYMOD_CTRL;
if (event.isAltPressed()) keyMod |= KeyHandler.KEYMOD_ALT;
if (event.isShiftPressed()) keyMod |= KeyHandler.KEYMOD_SHIFT;
if (event.isFunctionPressed()) keyMod |= KeyHandler.KEYMOD_FUNCTION;
if (handleKeyCode(keyCode, keyMod)) {
if (LOG_KEY_EVENTS) Log.i(EmulatorDebug.LOG_TAG, "handleKeyCode() took key event");
return true;
Expand Down

0 comments on commit 50fe003

Please sign in to comment.