-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Key
, test for printable character on keydown when selection
fixes #50
- Loading branch information
Showing
6 changed files
with
142 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Keycodes from './keycodes'; | ||
|
||
/** | ||
* An abstraction around a KeyEvent | ||
* that key listeners in the editor can use | ||
* to determine what sort of key was pressed | ||
*/ | ||
const Key = class Key { | ||
constructor(event) { | ||
this.keyCode = event.keyCode; | ||
this.event = event; | ||
} | ||
|
||
static fromEvent(event) { | ||
return new Key(event); | ||
} | ||
|
||
isEscape() { | ||
return this.keyCode === Keycodes.ESC; | ||
} | ||
|
||
isDelete() { | ||
return this.keyCode === Keycodes.BACKSPACE || | ||
this.keyCode === Keycodes.DELETE; | ||
} | ||
|
||
isSpace() { | ||
return this.keyCode === Keycodes.SPACE; | ||
} | ||
|
||
isEnter() { | ||
return this.keyCode === Keycodes.ENTER; | ||
} | ||
|
||
get ctrlKey() { | ||
return this.event.ctrlKey; | ||
} | ||
|
||
get metaKey() { | ||
return this.event.metaKey; | ||
} | ||
|
||
/** | ||
* See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode#Printable_keys_in_standard_position | ||
* and http://stackoverflow.com/a/12467610/137784 | ||
*/ | ||
isPrintable() { | ||
if (this.ctrlKey || this.metaKey) { | ||
return false; | ||
} | ||
|
||
const {keyCode:code} = this; | ||
|
||
return ( | ||
(code >= Keycodes['0'] && code <= Keycodes['9']) || // number keys | ||
this.isSpace() || | ||
this.isEnter() || | ||
(code >= Keycodes.A && code <= Keycodes.Z) || // letter keys | ||
(code >= Keycodes.NUMPAD_0 && code <= Keycodes.NUMPAD_9) || // numpad keys | ||
(code >= Keycodes[';'] && code <= Keycodes['`']) || // punctuation | ||
(code >= Keycodes['['] && code <= Keycodes['"']) || | ||
// FIXME the IME action seems to get lost when we issue an `editor.deleteSelection` | ||
// before it (in Chrome) | ||
code === Keycodes.IME | ||
); | ||
} | ||
}; | ||
|
||
export default Key; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
export default { | ||
LEFT_ARROW: 37, | ||
BACKSPACE : 8, | ||
SPACE: 32, | ||
ENTER : 13, | ||
ESC : 27, | ||
DELETE : 46, | ||
M : 77 | ||
'0': 48, | ||
'9': 57, | ||
A: 65, | ||
Z: 90, | ||
'NUMPAD_0': 186, | ||
'NUMPAD_9': 111, | ||
';': 186, | ||
'`': 192, | ||
'[': 219, | ||
'"': 222, | ||
|
||
// Input Method Editor uses multiple keystrokes to display characters. | ||
// Example on mac: press option-i then i. This fires 2 key events in Chrome | ||
// with keyCode 229 and displays ˆ and then î. | ||
// See http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html#fixed-virtual-key-codes | ||
IME: 229 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters