-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(web): refactor and harmonize constants 🏗️ #12072
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,36 @@ | ||
// TODO: Move to separate folder: 'codes' | ||
// We should start splitting off code needed by keyboards even without a KeyboardProcessor active. | ||
/* | ||
* Keyman is copyright (C) SIL International. MIT License. | ||
* | ||
* Keyboard key codes and modifier bitmasks. | ||
*/ | ||
|
||
// see also: common/web/types/src/kmx/kmx.ts | ||
import { ModifierKeyConstants, USVirtualKeyCodes } from '@keymanapp/common-types'; | ||
|
||
const Codes = { | ||
// Define Keyman Developer modifier bit-flags (exposed for use by other modules) | ||
// Compare against /common/include/kmx_file.h. CTRL+F "#define LCTRLFLAG" to find the secton. | ||
modifierCodes: { | ||
"LCTRL":0x0001, // LCTRLFLAG | ||
"RCTRL":0x0002, // RCTRLFLAG | ||
"LALT":0x0004, // LALTFLAG | ||
"RALT":0x0008, // RALTFLAG | ||
"SHIFT":0x0010, // K_SHIFTFLAG | ||
"CTRL":0x0020, // K_CTRLFLAG | ||
"ALT":0x0040, // K_ALTFLAG | ||
...ModifierKeyConstants, | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should include this here -- we should maintain this as a legacy interface only. Mixing the names in a published interface doesn't buy us anything -- we have to maintain the old names anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
// Debug-mode keyboards compiled before Keyman 18.0 referenced the `ModifierKeyConstants` | ||
// constants via the names established below. We must continue to support them, as they're | ||
// essentially part of the keyboard API now. | ||
"LCTRL": ModifierKeyConstants.LCTRLFLAG, | ||
"RCTRL": ModifierKeyConstants.RCTRLFLAG, | ||
"LALT": ModifierKeyConstants.LALTFLAG, | ||
"RALT": ModifierKeyConstants.RALTFLAG, | ||
"SHIFT": ModifierKeyConstants.K_SHIFTFLAG, | ||
"CTRL": ModifierKeyConstants.K_CTRLFLAG, | ||
"ALT": ModifierKeyConstants.K_ALTFLAG, | ||
// TENTATIVE: Represents command keys, which some OSes use for shortcuts we don't | ||
// want to block. No rule will ever target a modifier set with this bit set to 1. | ||
"META":0x0080, // K_METAFLAG | ||
"CAPS":0x0100, // CAPITALFLAG | ||
"NO_CAPS":0x0200, // NOTCAPITALFLAG | ||
"NUM_LOCK":0x0400, // NUMLOCKFLAG | ||
"NO_NUM_LOCK":0x0800, // NOTNUMLOCKFLAG | ||
"SCROLL_LOCK":0x1000, // SCROLLFLAG | ||
"NO_SCROLL_LOCK":0x2000, // NOTSCROLLFLAG | ||
"VIRTUAL_KEY":0x4000, // ISVIRTUALKEY | ||
"VIRTUAL_CHAR_KEY":0x8000 // VIRTUALCHARKEY // Unused by KMW, but reserved for use by other Keyman engines. | ||
|
||
"META": ModifierKeyConstants.K_METAFLAG, | ||
"CAPS": ModifierKeyConstants.CAPITALFLAG, | ||
"NO_CAPS": ModifierKeyConstants.NOTCAPITALFLAG, | ||
"NUM_LOCK": ModifierKeyConstants.NUMLOCKFLAG, | ||
"NO_NUM_LOCK": ModifierKeyConstants.NOTNUMLOCKFLAG, | ||
"SCROLL_LOCK": ModifierKeyConstants.SCROLLFLAG, | ||
"NO_SCROLL_LOCK": ModifierKeyConstants.NOTSCROLLFLAG, | ||
"VIRTUAL_KEY": ModifierKeyConstants.ISVIRTUALKEY, | ||
"VIRTUAL_CHAR_KEY": ModifierKeyConstants.VIRTUALCHARKEY // Unused by KMW, but reserved for use by other Keyman engines. | ||
// Note: keys_mod_other = 0x10000, used by KMX+ for the | ||
// other modifier flag in layers, > 16 bit so not available here. | ||
// See keys_mod_other in keyman_core_ldml.ts | ||
|
@@ -50,35 +55,7 @@ const Codes = { | |
|
||
// Define standard keycode numbers (exposed for use by other modules) | ||
keyCodes: { | ||
"K_BKSP":8,"K_TAB":9,"K_ENTER":13, | ||
"K_SHIFT":16,"K_CONTROL":17,"K_ALT":18,"K_PAUSE":19,"K_CAPS":20, | ||
"K_ESC":27,"K_SPACE":32,"K_PGUP":33, | ||
"K_PGDN":34,"K_END":35,"K_HOME":36,"K_LEFT":37,"K_UP":38, | ||
"K_RIGHT":39,"K_DOWN":40,"K_SEL":41,"K_PRINT":42,"K_EXEC":43, | ||
"K_INS":45,"K_DEL":46,"K_HELP":47,"K_0":48, | ||
"K_1":49,"K_2":50,"K_3":51,"K_4":52,"K_5":53,"K_6":54,"K_7":55, | ||
"K_8":56,"K_9":57,"K_A":65,"K_B":66,"K_C":67,"K_D":68,"K_E":69, | ||
"K_F":70,"K_G":71,"K_H":72,"K_I":73,"K_J":74,"K_K":75,"K_L":76, | ||
"K_M":77,"K_N":78,"K_O":79,"K_P":80,"K_Q":81,"K_R":82,"K_S":83, | ||
"K_T":84,"K_U":85,"K_V":86,"K_W":87,"K_X":88,"K_Y":89,"K_Z":90, | ||
"K_NP0":96,"K_NP1":97,"K_NP2":98, | ||
"K_NP3":99,"K_NP4":100,"K_NP5":101,"K_NP6":102, | ||
"K_NP7":103,"K_NP8":104,"K_NP9":105,"K_NPSTAR":106, | ||
"K_NPPLUS":107,"K_SEPARATOR":108,"K_NPMINUS":109,"K_NPDOT":110, | ||
"K_NPSLASH":111,"K_F1":112,"K_F2":113,"K_F3":114,"K_F4":115, | ||
"K_F5":116,"K_F6":117,"K_F7":118,"K_F8":119,"K_F9":120, | ||
"K_F10":121,"K_F11":122,"K_F12":123,"K_NUMLOCK":144,"K_SCROLL":145, | ||
"K_LSHIFT":160,"K_RSHIFT":161,"K_LCONTROL":162,"K_RCONTROL":163, | ||
"K_LALT":164,"K_RALT":165, | ||
"K_COLON":186,"K_EQUAL":187,"K_COMMA":188,"K_HYPHEN":189, | ||
"K_PERIOD":190,"K_SLASH":191,"K_BKQUOTE":192, | ||
"K_LBRKT":219,"K_BKSLASH":220,"K_RBRKT":221, | ||
"K_QUOTE":222,"K_oE2":226,"K_OE2":226, | ||
"K_LOPT":50001,"K_ROPT":50002, | ||
"K_NUMERALS":50003,"K_SYMBOLS":50004,"K_CURRENCIES":50005, | ||
"K_UPPER":50006,"K_LOWER":50007,"K_ALPHA":50008, | ||
"K_SHIFTED":50009,"K_ALTGR":50010, | ||
"K_TABBACK":50011,"K_TABFWD":50012 | ||
...USVirtualKeyCodes, | ||
} as {[name: string]: number}, | ||
|
||
codesUS: [ | ||
|
@@ -117,34 +94,34 @@ const Codes = { | |
getModifierState(layerId: string): number { | ||
var modifier=0; | ||
if(layerId.indexOf('shift') >= 0) { | ||
modifier |= Codes.modifierCodes['SHIFT']; | ||
modifier |= ModifierKeyConstants.K_SHIFTFLAG; | ||
} | ||
|
||
// The chiral checks must not be directly exclusive due each other to visual OSK feedback. | ||
var ctrlMatched=false; | ||
if(layerId.indexOf('leftctrl') >= 0) { | ||
modifier |= Codes.modifierCodes['LCTRL']; | ||
modifier |= ModifierKeyConstants.LCTRLFLAG; | ||
ctrlMatched=true; | ||
} | ||
if(layerId.indexOf('rightctrl') >= 0) { | ||
modifier |= Codes.modifierCodes['RCTRL']; | ||
modifier |= ModifierKeyConstants.RCTRLFLAG; | ||
ctrlMatched=true; | ||
} | ||
if(layerId.indexOf('ctrl') >= 0 && !ctrlMatched) { | ||
modifier |= Codes.modifierCodes['CTRL']; | ||
modifier |= ModifierKeyConstants.K_CTRLFLAG; | ||
} | ||
|
||
var altMatched=false; | ||
if(layerId.indexOf('leftalt') >= 0) { | ||
modifier |= Codes.modifierCodes['LALT']; | ||
modifier |= ModifierKeyConstants.LALTFLAG; | ||
altMatched=true; | ||
} | ||
if(layerId.indexOf('rightalt') >= 0) { | ||
modifier |= Codes.modifierCodes['RALT']; | ||
modifier |= ModifierKeyConstants.RALTFLAG; | ||
altMatched=true; | ||
} | ||
if(layerId.indexOf('alt') >= 0 && !altMatched) { | ||
modifier |= Codes.modifierCodes['ALT']; | ||
modifier |= ModifierKeyConstants.K_ALTFLAG; | ||
} | ||
|
||
return modifier; | ||
|
@@ -160,9 +137,9 @@ const Codes = { | |
var modifier=0; | ||
|
||
if(layerId.indexOf('caps') >= 0) { | ||
modifier |= Codes.modifierCodes['CAPS']; | ||
modifier |= ModifierKeyConstants.CAPITALFLAG; | ||
} else { | ||
modifier |= Codes.modifierCodes['NO_CAPS']; | ||
modifier |= ModifierKeyConstants.NOTCAPITALFLAG; | ||
} | ||
|
||
return modifier; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optionally, we could also emit the same values under their new name:
so that at some point in the distant future, we could drop the old names entirely... maybe.