Skip to content

Instantly share code, notes, and snippets.

@Demon000
Created December 13, 2023 16:21
Show Gist options
  • Save Demon000/a0e7d007363d1ec035b3ec24b61ead54 to your computer and use it in GitHub Desktop.
Save Demon000/a0e7d007363d1ec035b3ec24b61ead54 to your computer and use it in GitHub Desktop.
JavaScript KeyboardEvent to Android KEYCODE
private browserKeyToKeycode(
data: BrowserKeyboardEvent,
): KeyCode | undefined {
switch (data.code) {
case 'ShiftLeft':
return KeyCode.KEYCODE_SHIFT_LEFT;
case 'ShiftRight':
return KeyCode.KEYCODE_SHIFT_RIGHT;
case 'MetaLeft':
return KeyCode.KEYCODE_META_LEFT;
case 'MetaRight':
return KeyCode.KEYCODE_META_RIGHT;
case 'Numpad0':
return KeyCode.KEYCODE_NUMPAD_0;
case 'Numpad1':
return KeyCode.KEYCODE_NUMPAD_1;
case 'Numpad2':
return KeyCode.KEYCODE_NUMPAD_2;
case 'Numpad3':
return KeyCode.KEYCODE_NUMPAD_3;
case 'Numpad4':
return KeyCode.KEYCODE_NUMPAD_4;
case 'Numpad5':
return KeyCode.KEYCODE_NUMPAD_5;
case 'Numpad6':
return KeyCode.KEYCODE_NUMPAD_6;
case 'Numpad7':
return KeyCode.KEYCODE_NUMPAD_7;
case 'Numpad8':
return KeyCode.KEYCODE_NUMPAD_8;
case 'Numpad9':
return KeyCode.KEYCODE_NUMPAD_9;
case 'NumpadDivide':
return KeyCode.KEYCODE_NUMPAD_DIVIDE;
case 'NumpadMultiply':
return KeyCode.KEYCODE_NUMPAD_MULTIPLY;
case 'NumpadSubtract':
return KeyCode.KEYCODE_NUMPAD_SUBTRACT;
case 'NumpadAdd':
return KeyCode.KEYCODE_NUMPAD_ADD;
case 'NumpadDecimal':
return KeyCode.KEYCODE_NUMPAD_DOT;
case 'NumpadComma':
return KeyCode.KEYCODE_NUMPAD_COMMA;
case 'NumpadEnter':
return KeyCode.KEYCODE_NUMPAD_ENTER;
case 'NumpadEqual':
return KeyCode.KEYCODE_NUMPAD_EQUALS;
case 'NumpadParenLeft':
return KeyCode.KEYCODE_NUMPAD_LEFT_PAREN;
case 'NumpadParenRight':
return KeyCode.KEYCODE_NUMPAD_RIGHT_PAREN;
}
if (data.key === 'Control') {
if (data.location === 1) {
return KeyCode.KEYCODE_CTRL_LEFT;
} else {
return KeyCode.KEYCODE_CTRL_RIGHT;
}
}
switch (data.key) {
case 'GoHome':
return KeyCode.KEYCODE_HOME;
case 'GoBack':
return KeyCode.KEYCODE_BACK;
case 'Call':
return KeyCode.KEYCODE_CALL;
case 'EndCall':
return KeyCode.KEYCODE_ENDCALL;
case '*':
return KeyCode.KEYCODE_STAR;
case '#':
return KeyCode.KEYCODE_POUND;
case 'ArrowUp':
return KeyCode.KEYCODE_DPAD_UP;
case 'ArrowDown':
return KeyCode.KEYCODE_DPAD_DOWN;
case 'ArrowLeft':
return KeyCode.KEYCODE_DPAD_LEFT;
case 'ArrowRight':
return KeyCode.KEYCODE_DPAD_RIGHT;
case 'Accept':
return KeyCode.KEYCODE_DPAD_CENTER;
case 'AudioVolumeUp':
return KeyCode.KEYCODE_VOLUME_UP;
case 'AudioVolumeDown':
return KeyCode.KEYCODE_VOLUME_DOWN;
case 'Power':
return KeyCode.KEYCODE_POWER;
case 'Camera':
return KeyCode.KEYCODE_CAMERA;
case 'Clear':
return KeyCode.KEYCODE_CLEAR;
case ',':
return KeyCode.KEYCODE_COMMA;
case '.':
return KeyCode.KEYCODE_PERIOD;
case 'Tab':
return KeyCode.KEYCODE_TAB;
case ' ':
return KeyCode.KEYCODE_SPACE;
case 'Symbol':
return KeyCode.KEYCODE_SYM;
case 'LaunchWebBrowser':
return KeyCode.KEYCODE_EXPLORER;
case 'LaunchMail':
return KeyCode.KEYCODE_ENVELOPE;
case 'Enter':
return KeyCode.KEYCODE_ENTER;
case 'Backspace':
return KeyCode.KEYCODE_DEL;
case '`':
return KeyCode.KEYCODE_GRAVE;
case '-':
return KeyCode.KEYCODE_MINUS;
case '=':
return KeyCode.KEYCODE_EQUALS;
case '[':
return KeyCode.KEYCODE_LEFT_BRACKET;
case ']':
return KeyCode.KEYCODE_RIGHT_BRACKET;
case '\\':
return KeyCode.KEYCODE_BACKSLASH;
case ';':
return KeyCode.KEYCODE_SEMICOLON;
// eslint-disable-next-line @typescript-eslint/quotes
case "'":
return KeyCode.KEYCODE_APOSTROPHE;
case '/':
return KeyCode.KEYCODE_SLASH;
case '@':
return KeyCode.KEYCODE_AT;
case 'HeadsetHook':
return KeyCode.KEYCODE_HEADSETHOOK;
case 'CameraFocus':
return KeyCode.KEYCODE_FOCUS;
case '+':
return KeyCode.KEYCODE_PLUS;
case 'ContextMenu':
return KeyCode.KEYCODE_MENU;
case 'Notification':
return KeyCode.KEYCODE_NOTIFICATION;
case 'BrowserSearch':
return KeyCode.KEYCODE_SEARCH;
case 'MediaPlayPause':
return KeyCode.KEYCODE_MEDIA_PLAY_PAUSE;
case 'MediaStop':
return KeyCode.KEYCODE_MEDIA_STOP;
case 'MediaTrackNext':
return KeyCode.KEYCODE_MEDIA_NEXT;
case 'MediaTrackPrevious':
return KeyCode.KEYCODE_MEDIA_PREVIOUS;
case 'MediaRewind':
return KeyCode.KEYCODE_MEDIA_REWIND;
case 'MediaFastForward':
return KeyCode.KEYCODE_MEDIA_FAST_FORWARD;
case 'MicrophoneVolumeMute':
return KeyCode.KEYCODE_MUTE;
case 'PageUp':
return KeyCode.KEYCODE_PAGE_UP;
case 'PageDown':
return KeyCode.KEYCODE_PAGE_DOWN;
case 'ModeChange':
return KeyCode.KEYCODE_SWITCH_CHARSET;
case 'Select':
return KeyCode.KEYCODE_BUTTON_SELECT;
case 'Escape':
return KeyCode.KEYCODE_ESCAPE;
case 'Delete':
return KeyCode.KEYCODE_FORWARD_DEL;
case 'CapsLock':
return KeyCode.KEYCODE_CAPS_LOCK;
case 'ScrollLock':
return KeyCode.KEYCODE_SCROLL_LOCK;
case 'Fn':
return KeyCode.KEYCODE_FUNCTION;
case 'PrintScreen':
return KeyCode.KEYCODE_SYSRQ;
case 'Pause':
return KeyCode.KEYCODE_BREAK;
case 'Home':
return KeyCode.KEYCODE_MOVE_HOME;
case 'End':
return KeyCode.KEYCODE_MOVE_END;
case 'Insert':
return KeyCode.KEYCODE_INSERT;
case 'BrowserForward':
return KeyCode.KEYCODE_FORWARD;
case 'MediaPlay':
return KeyCode.KEYCODE_MEDIA_PLAY;
case 'MediaPause':
return KeyCode.KEYCODE_MEDIA_PAUSE;
case 'Close':
return KeyCode.KEYCODE_MEDIA_CLOSE;
case 'Eject':
return KeyCode.KEYCODE_MEDIA_EJECT;
case 'MediaRecord':
return KeyCode.KEYCODE_MEDIA_RECORD;
case 'NumLock':
return KeyCode.KEYCODE_NUM_LOCK;
case 'AudioVolumeMute':
return KeyCode.KEYCODE_VOLUME_MUTE;
case 'Info':
return KeyCode.KEYCODE_INFO;
case 'ChannelUp':
return KeyCode.KEYCODE_CHANNEL_UP;
case 'ChannelDown':
return KeyCode.KEYCODE_CHANNEL_DOWN;
case 'ZoomIn':
return KeyCode.KEYCODE_ZOOM_IN;
case 'ZoomOut':
return KeyCode.KEYCODE_ZOOM_OUT;
case 'TV':
return KeyCode.KEYCODE_TV;
case 'Guide':
return KeyCode.KEYCODE_GUIDE;
case 'DVR':
return KeyCode.KEYCODE_DVR;
case 'BrowserFavorites':
return KeyCode.KEYCODE_BOOKMARK;
case 'ClosedCaptionToggle':
return KeyCode.KEYCODE_CAPTIONS;
case 'Settings':
return KeyCode.KEYCODE_SETTINGS;
case 'TVPower':
return KeyCode.KEYCODE_TV_POWER;
case 'TVInput':
return KeyCode.KEYCODE_TV_INPUT;
case 'STBPower':
return KeyCode.KEYCODE_STB_POWER;
case 'STBInput':
return KeyCode.KEYCODE_STB_INPUT;
case 'AVRPower':
return KeyCode.KEYCODE_AVR_POWER;
case 'AVRInput':
return KeyCode.KEYCODE_AVR_INPUT;
case 'ColorF0Red':
return KeyCode.KEYCODE_PROG_RED;
case 'ColorF1Green':
return KeyCode.KEYCODE_PROG_GREEN;
case 'ColorF2Yellow':
return KeyCode.KEYCODE_PROG_YELLOW;
case 'ColorF3Blue':
return KeyCode.KEYCODE_PROG_BLUE;
case 'AppSwitch':
return KeyCode.KEYCODE_APP_SWITCH;
case 'GroupNext':
return KeyCode.KEYCODE_LANGUAGE_SWITCH;
case 'MannerMode':
return KeyCode.KEYCODE_MANNER_MODE;
case 'TV3DMode':
return KeyCode.KEYCODE_3D_MODE;
case 'LaunchContacts':
return KeyCode.KEYCODE_CONTACTS;
case 'LaunchCalendar':
return KeyCode.KEYCODE_CALENDAR;
case 'LaunchMusicPlayer':
return KeyCode.KEYCODE_MUSIC;
case 'LaunchCalculator':
return KeyCode.KEYCODE_CALCULATOR;
case 'BrightnessDown':
return KeyCode.KEYCODE_BRIGHTNESS_DOWN;
case 'BrightnessUp':
return KeyCode.KEYCODE_BRIGHTNESS_UP;
case 'MediaAudioTrack':
return KeyCode.KEYCODE_MEDIA_AUDIO_TRACK;
case 'Standby':
return KeyCode.KEYCODE_SLEEP;
case 'WakeUp':
return KeyCode.KEYCODE_WAKEUP;
case 'Pairing':
return KeyCode.KEYCODE_PAIRING;
case 'MediaTopMenu':
return KeyCode.KEYCODE_MEDIA_TOP_MENU;
case 'MediaLast':
return KeyCode.KEYCODE_LAST_CHANNEL;
case 'TVDataService':
return KeyCode.KEYCODE_TV_DATA_SERVICE;
case 'VoiceDial':
return KeyCode.KEYCODE_VOICE_ASSIST;
case 'TVRadioService':
return KeyCode.KEYCODE_TV_RADIO_SERVICE;
case 'Teletext':
return KeyCode.KEYCODE_TV_TELETEXT;
case 'TVNumberEntry':
return KeyCode.KEYCODE_TV_NUMBER_ENTRY;
case 'TVTerrestrialAnalog':
return KeyCode.KEYCODE_TV_TERRESTRIAL_ANALOG;
case 'TVTerrestrialDigital':
return KeyCode.KEYCODE_TV_TERRESTRIAL_DIGITAL;
case 'TVSatellite':
return KeyCode.KEYCODE_TV_SATELLITE;
case 'TVSatelliteBS':
return KeyCode.KEYCODE_TV_SATELLITE_BS;
case 'TVSatelliteCS':
return KeyCode.KEYCODE_TV_SATELLITE_CS;
case 'TVSatelliteToggle':
return KeyCode.KEYCODE_TV_SATELLITE_SERVICE;
case 'TVNetwork':
return KeyCode.KEYCODE_TV_NETWORK;
case 'TVAntennaCable':
return KeyCode.KEYCODE_TV_ANTENNA_CABLE;
case 'TVInputHDMI1':
return KeyCode.KEYCODE_TV_INPUT_HDMI_1;
case 'TVInputHDMI2':
return KeyCode.KEYCODE_TV_INPUT_HDMI_2;
case 'TVInputHDMI3':
return KeyCode.KEYCODE_TV_INPUT_HDMI_3;
case 'TVInputHDMI4':
return KeyCode.KEYCODE_TV_INPUT_HDMI_4;
case 'TVInputComposite1':
return KeyCode.KEYCODE_TV_INPUT_COMPOSITE_1;
case 'TVInputComposite2':
return KeyCode.KEYCODE_TV_INPUT_COMPOSITE_2;
case 'TVInputComponent1':
return KeyCode.KEYCODE_TV_INPUT_COMPONENT_1;
case 'TVInputComponent2':
return KeyCode.KEYCODE_TV_INPUT_COMPONENT_2;
case 'TVInputVGA1':
return KeyCode.KEYCODE_TV_INPUT_VGA_1;
case 'TVAudioDescription':
return KeyCode.KEYCODE_TV_AUDIO_DESCRIPTION;
case 'TVAudioDescriptionMixUp':
return KeyCode.KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP;
case 'TVAudioDescriptionMixDown':
return KeyCode.KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN;
case 'ZoomToggle':
return KeyCode.KEYCODE_TV_ZOOM_MODE;
case 'TVContentsMenu':
return KeyCode.KEYCODE_TV_CONTENTS_MENU;
case 'TVMediaContext':
return KeyCode.KEYCODE_TV_MEDIA_CONTEXT_MENU;
case 'TVTimer':
return KeyCode.KEYCODE_TV_TIMER_PROGRAMMING;
case 'Help':
return KeyCode.KEYCODE_HELP;
case 'NavigatePrevious':
return KeyCode.KEYCODE_NAVIGATE_PREVIOUS;
case 'NavigateNext':
return KeyCode.KEYCODE_NAVIGATE_NEXT;
case 'NavigateIn':
return KeyCode.KEYCODE_NAVIGATE_IN;
case 'NavigateOut':
return KeyCode.KEYCODE_NAVIGATE_OUT;
}
if (data.key.startsWith('F')) {
const numberAsString = data.key.slice(1);
const number = Number.parseInt(numberAsString);
if (!Number.isNaN(number)) {
if (number >= 1 && number <= 12) {
return KeyCode.KEYCODE_F1 + number - 1;
} else {
/*
* Extra F keys.
*/
return undefined;
}
}
}
const keyCode = data.key.toLowerCase().charCodeAt(0);
const zeroCode = '0'.charCodeAt(0);
const nineCode = '9'.charCodeAt(0);
if (keyCode >= zeroCode && keyCode <= nineCode) {
return KeyCode.KEYCODE_0 + keyCode - zeroCode;
}
const aCode = 'a'.charCodeAt(0);
const zCode = 'z'.charCodeAt(0);
if (keyCode >= aCode && keyCode <= zCode) {
return KeyCode.KEYCODE_A + keyCode - aCode;
}
/* Unhandled:
* KEYCODE_NUM = 78;
* KEYCODE_PICTSYMBOLS = 94;
* KEYCODE_BUTTON_A = 96;
* KEYCODE_BUTTON_B = 97;
* KEYCODE_BUTTON_C = 98;
* KEYCODE_BUTTON_X = 99;
* KEYCODE_BUTTON_Y = 100;
* KEYCODE_BUTTON_Z = 101;
* KEYCODE_BUTTON_L1 = 102;
* KEYCODE_BUTTON_R1 = 103;
* KEYCODE_BUTTON_L2 = 104;
* KEYCODE_BUTTON_R2 = 105;
* KEYCODE_BUTTON_THUMBL = 106;
* KEYCODE_BUTTON_THUMBR = 107;
* KEYCODE_BUTTON_START = 108;
* KEYCODE_BUTTON_SELECT = 109;
* KEYCODE_BUTTON_MODE = 110;
* KEYCODE_WINDOW = 171;
* KEYCODE_BUTTON_1 = 188;
* KEYCODE_BUTTON_2 = 189;
* KEYCODE_BUTTON_3 = 190;
* KEYCODE_BUTTON_4 = 191;
* KEYCODE_BUTTON_5 = 192;
* KEYCODE_BUTTON_6 = 193;
* KEYCODE_BUTTON_7 = 194;
* KEYCODE_BUTTON_8 = 195;
* KEYCODE_BUTTON_9 = 196;
* KEYCODE_BUTTON_10 = 197;
* KEYCODE_BUTTON_11 = 198;
* KEYCODE_BUTTON_12 = 199;
* KEYCODE_BUTTON_13 = 200;
* KEYCODE_BUTTON_14 = 201;
* KEYCODE_BUTTON_15 = 202;
* KEYCODE_BUTTON_16 = 203;
* KEYCODE_ZENKAKU_HANKAKU = 211;
* KEYCODE_EISU = 212;
* KEYCODE_MUHENKAN = 213;
* KEYCODE_HENKAN = 214;
* KEYCODE_KATAKANA_HIRAGANA = 215;
* KEYCODE_YEN = 216;
* KEYCODE_RO = 217;
* KEYCODE_KANA = 218;
* KEYCODE_ASSIST = 219;
* KEYCODE_11 = 227;
* KEYCODE_12 = 228;
* KEYCODE_DPAD_UP_LEFT = 268;
* KEYCODE_DPAD_DOWN_LEFT = 269;
* KEYCODE_DPAD_UP_RIGHT = 270;
* KEYCODE_DPAD_DOWN_RIGHT = 271;
* KEYCODE_SENTINEL = 65535;
* KEYCODE_ROTARY_CONTROLLER = 65536;
* KEYCODE_MEDIA = 65537;
* KEYCODE_NAVIGATION = 65538;
* KEYCODE_RADIO = 65539;
* KEYCODE_TEL = 65540;
* KEYCODE_PRIMARY_BUTTON = 65541;
* KEYCODE_SECONDARY_BUTTON = 65542;
* KEYCODE_TERTIARY_BUTTON = 65543;
* KEYCODE_TURN_CARD = 65544;
*/
return undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment