@@ -432,53 +432,57 @@ define(function (require, exports, module) {
432432 /**
433433 * @private
434434 * Looks for keycodes that have os-inconsistent keys and fixes them.
435- * @param {number } The keycode from the keyboard event.
436- * @param {string } The current best guess at what the key is.
437435 * @return {string } If the key is OS-inconsistent, the correct key; otherwise, the original key.
438436 **/
439- function _mapKeycodeToKey ( keycode , key ) {
440- // If keycode represents one of the digit keys (0-9), then return the corresponding digit
441- // by subtracting KeyEvent.DOM_VK_0 from keycode. ie. [48-57] --> [0-9]
442- if ( keycode >= KeyEvent . DOM_VK_0 && keycode <= KeyEvent . DOM_VK_9 ) {
443- return String ( keycode - KeyEvent . DOM_VK_0 ) ;
444- // Do the same with the numpad numbers
445- // by subtracting KeyEvent.DOM_VK_NUMPAD0 from keycode. ie. [96-105] --> [0-9]
446- } else if ( keycode >= KeyEvent . DOM_VK_NUMPAD0 && keycode <= KeyEvent . DOM_VK_NUMPAD9 ) {
447- return String ( keycode - KeyEvent . DOM_VK_NUMPAD0 ) ;
437+ function _mapKeycodeToKey ( event ) {
438+ // key code mapping https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values
439+ const code = event . code ;
440+ let codes = {
441+ "Tab" : "Tab" ,
442+ "Space" : "Space" ,
443+ "Backspace" : "Backspace" ,
444+ "Insert" : "Insert" ,
445+ "Delete" : "Delete" ,
446+ "ArrowUp" : "Up" ,
447+ "ArrowDown" : "Down" ,
448+ "ArrowLeft" : "Left" ,
449+ "ArrowRight" : "Right" ,
450+ "Semicolon" : ";" ,
451+ "Equal" : "=" ,
452+ "Add" : "+" , // Eg. Numpad Add button NumpadAdd
453+ "Comma" : "," ,
454+ "Minus" : "-" ,
455+ "Period" : "." ,
456+ "Decimal" : "." , // NumpadDecimal
457+ "Slash" : "/" ,
458+ "Divide" : "/" , //NumpadDivide
459+ "Quote" : "'" ,
460+ "Backquote" : "`" ,
461+ "BracketLeft" : "[" ,
462+ "BracketRight" : "]" ,
463+ "Backslash" : "\\"
464+ } ;
465+ let strippedCode ;
466+ // event.code should be there in all browsers post 2014. But some older browsers don't, so the check `code &&`
467+ if ( code && code . startsWith ( "Key" ) ) {
468+ strippedCode = code . replace ( "Key" , "" ) ;
469+ return codes [ strippedCode ] || strippedCode ;
448470 }
449-
450-
451- switch ( keycode ) {
452- case KeyEvent . DOM_VK_SEMICOLON :
453- return ";" ;
454- case KeyEvent . DOM_VK_EQUALS :
455- return "=" ;
456- case KeyEvent . DOM_VK_COMMA :
457- return "," ;
458- case KeyEvent . DOM_VK_SUBTRACT :
459- case KeyEvent . DOM_VK_DASH :
460- return "-" ;
461- case KeyEvent . DOM_VK_ADD :
462- return "+" ;
463- case KeyEvent . DOM_VK_DECIMAL :
464- case KeyEvent . DOM_VK_PERIOD :
465- return "." ;
466- case KeyEvent . DOM_VK_DIVIDE :
467- case KeyEvent . DOM_VK_SLASH :
468- return "/" ;
469- case KeyEvent . DOM_VK_BACK_QUOTE :
470- return "`" ;
471- case KeyEvent . DOM_VK_OPEN_BRACKET :
472- return "[" ;
473- case KeyEvent . DOM_VK_BACK_SLASH :
474- return "\\" ;
475- case KeyEvent . DOM_VK_CLOSE_BRACKET :
476- return "]" ;
477- case KeyEvent . DOM_VK_QUOTE :
478- return "'" ;
479- default :
480- return key ;
471+ if ( code && code . startsWith ( "Digit" ) ) {
472+ strippedCode = code . replace ( "Digit" , "" ) ;
473+ return codes [ strippedCode ] || strippedCode ;
481474 }
475+ if ( code && code . startsWith ( "Numpad" ) ) {
476+ // this can either be a simple numpad digit like 'Numpad0'(we should return 0)
477+ // or 'NumpadAdd'(for which we should return the mapped + button)
478+ strippedCode = code . replace ( "Numpad" , "" ) ;
479+ return codes [ strippedCode ] || strippedCode ;
480+ }
481+ if ( codes [ code ] ) {
482+ return codes [ code ] ;
483+ }
484+ // This is pretty much all the keys in a keyboard we usually encounter. If still no match, return keycode
485+ return String . fromCharCode ( event . keyCode ) ;
482486 }
483487
484488 /**
@@ -489,47 +493,7 @@ define(function (require, exports, module) {
489493 hasCtrl = ( brackets . platform !== "mac" ) ? ( event . ctrlKey ) : ( event . metaKey ) ,
490494 hasAlt = ( event . altKey ) ,
491495 hasShift = ( event . shiftKey ) ,
492- key = String . fromCharCode ( event . keyCode ) ;
493-
494- //From the W3C, if we can get the KeyboardEvent.key then look here
495- //As that will let us use keys like then function keys "F5" for commands. The
496- //full set of values we can use is here
497- // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
498- let ident = event . key ;
499- if ( ident ) {
500- if ( ident . charAt ( 0 ) === "U" && ident . charAt ( 1 ) === "+" ) {
501- //This is a unicode code point like "U+002A", get the 002A and use that
502- key = String . fromCharCode ( parseInt ( ident . substring ( 2 ) , 16 ) ) ;
503- } else {
504- //This is some non-character key, just use the raw identifier
505- key = ident ;
506- }
507- }
508-
509- // Translate some keys to their common names
510- if ( key === "\t" ) {
511- key = "Tab" ;
512- } else if ( key === " " ) {
513- key = "Space" ;
514- } else if ( key === "\b" ) {
515- key = "Backspace" ;
516- } else if ( key === "Help" ) {
517- key = "Insert" ;
518- } else if ( event . keyCode === KeyEvent . DOM_VK_DELETE ) {
519- key = "Delete" ;
520- } else if ( key === "ArrowUp" ) {
521- key = "Up" ;
522- } else if ( key === "ArrowDown" ) {
523- key = "Down" ;
524- } else if ( key === "ArrowLeft" ) {
525- key = "Left" ;
526- } else if ( key === "ArrowRight" ) {
527- key = "Right" ;
528- }
529- else {
530- key = _mapKeycodeToKey ( event . keyCode , key ) ;
531- }
532-
496+ key = _mapKeycodeToKey ( event ) ;
533497 return normalizeKeyDescriptorString ( _buildKeyDescriptor ( hasMacCtrl , hasCtrl , hasAlt , hasShift , key ) ) ;
534498 }
535499
0 commit comments