Use event.originalEvent.keyIdentifier instead of event.which.

Events match patterns if event.keyStroke == key pattern.
This commit is contained in:
Corey Johnson
2012-02-15 10:03:50 -08:00
parent 919cef41eb
commit a41676e84e
8 changed files with 84 additions and 116 deletions

View File

@@ -4,13 +4,6 @@ Specificity = require 'specificity'
module.exports =
class BindingSet
namedKeys:
backspace: 8, tab: 9, clear: 12, enter: 13, 'return': 13,
esc: 27, escape: 27, space: 32, left: 37, up: 38, right: 39,
down: 40, del: 46, 'delete': 46, home: 36, end: 35, pageup: 33,
pagedown: 34, ',': 188, '.': 190, '/': 191, '`': 192, '-': 189,
'=': 187, ';': 186, '\'': 222, '[': 219, ']': 221, '\\': 220
selector: null
commandForEvent: null
@@ -28,29 +21,5 @@ class BindingSet
null
eventMatchesPattern: (event, pattern) ->
pattern = @parseKeyPattern pattern
pattern.ctrlKey == event.ctrlKey and
pattern.altKey == event.altKey and
pattern.shiftKey == event.shiftKey and
pattern.metaKey == event.metaKey and
pattern.which == event.which
parseKeyPattern: (pattern) ->
pattern = pattern.replace(/<|>/g, "")
[modifiers..., key] = pattern.split '-'
modifiers.push 'shift' if key == key.toUpperCase() and key.toUpperCase() != key.toLowerCase()
if @namedKeys[key]
charCode = @namedKeys[key]
key = null
else
charCode = key.toUpperCase().charCodeAt 0
ctrlKey: 'ctrl' in modifiers
altKey: 'alt' in modifiers
shiftKey: 'shift' in modifiers
metaKey: 'meta' in modifiers
which: charCode
key: key
pattern = pattern.replace(/^<|>$/g, '')
event.keystroke == pattern

View File

@@ -42,60 +42,35 @@ class GlobalKeymap
$(keyEvent.target).trigger(commandEvent)
keystrokeStringForEvent: (event) ->
if event.type is 'keypress'
char = String.fromCharCode event.which
if /^U\+/i.test event.originalEvent.keyIdentifier
hexCharCode = event.originalEvent.keyIdentifier.replace(/^U\+/i, '')
charCode = parseInt(hexCharCode, 16)
key = @keyFromCharCode(charCode)
else
char = @keyCodeToChar[event.which]
key = event.originalEvent.keyIdentifier.toLowerCase()
modifiers = ''
if event.altKey and char isnt 'alt'
if event.altKey and key isnt 'alt'
modifiers += 'alt-'
if event.ctrlKey and char isnt 'ctrl'
if event.ctrlKey and key isnt 'ctrl'
modifiers += 'ctrl-'
if event.metaKey and char isnt 'meta'
if event.metaKey and key isnt 'meta'
modifiers += 'meta-'
if event.shiftKey
if event.type is 'keypress'
"#{modifiers}#{char}"
else if shiftChar = @shiftKeyCodeToChar[event.which]
"#{modifiers}#{shiftChar}"
else if char is 'shift'
"#{modifiers}shift"
else if (char)
"#{modifiers}shift-#{char}"
else
null
else if char
"#{modifiers}#{char}"
isNamedKey = key.length > 1
modifiers += 'shift-' if isNamedKey
else
null
key = key.toLowerCase()
keyCodeToChar:
8: 'backspace', 9: 'tab', 13: 'enter', 16: 'shift', 17: 'ctrl',
18: 'alt', 19: 'pause', 20: 'capslock', 27: 'esc', 32: 'space',
33: 'pageup', 34: 'pagedown', 35: 'end', 36: 'home', 37: 'left',
38: 'up', 39: 'right', 40: 'down', 45: 'insert', 46: 'del',
48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5', 54: '6',
55: '7', 56: '8', 57: '9', 65: 'a', 66: 'b', 67: 'c', 68: 'd',
69: 'e', 70: 'f', 71: 'g', 72: 'h', 73: 'i', 74: 'j', 75: 'k',
76: 'l', 77: 'm', 78: 'n', 79: 'o', 80: 'p', 81: 'q', 82: 'r',
83: 's', 84: 't', 85: 'u', 86: 'v', 87: 'w', 88: 'x', 89: 'y',
90: 'z', 91: 'meta', 93: 'meta', 96: '0', 97: '1', 98: '2',
99: '3', 100: '4', 101: '5', 102: '6', 103: '7', 104: '8',
105: '9', 106: '*', 107: '+', 109: '-', 110: '.', 111: '/',
112: 'f1', 113: 'f2', 114: 'f3', 115: 'f4', 116: 'f5', 117: 'f6',
118: 'f7', 119: 'f8', 120: 'f9', 121: 'f10', 122: 'f11',
123: 'f12', 144: 'numlock', 145: 'scroll', 186: ';', 187: '=',
188: ',', 189: '-', 190: '.', 191: '/', 192: '`', 219: '[',
220: '\\', 221: ']', 222: '\''
shiftKeyCodeToChar:
48: ')', 49: '!', 50: '@', 51: '#', 52: '$', 53: '%', 54: '^',
55: '&', 56: '*', 57: '(', 65: 'A', 66: 'B', 67: 'C', 68: 'D',
69: 'E', 70: 'F', 71: 'G', 72: 'H', 73: 'I', 74: 'J', 75: 'K',
76: 'L', 77: 'M', 78: 'N', 79: 'O', 80: 'P', 81: 'Q', 82: 'R',
83: 'S', 84: 'T', 85: 'U', 86: 'V', 87: 'W', 88: 'X', 89: 'Y',
90: 'Z', 186: ':', 187: '+', 188: '<', 189: '_', 190: '>',
191: '?', 192: '~', 219: '{', 220: '|', 221: '}', 222: '"'
"#{modifiers}#{key}"
keyFromCharCode: (charCode) ->
switch charCode
when 8 then 'backspace'
when 9 then 'tab'
when 13 then 'enter'
when 27 then 'escape'
when 32 then 'space'
when 127 then 'delete'
else String.fromCharCode(charCode)

View File

@@ -15,7 +15,7 @@ class VimMode
@opStack = []
@activateCommandMode()
atom.bindKeys '.editor', '<esc>': 'activate-command-mode'
atom.bindKeys '.editor', 'escape': 'activate-command-mode'
@editor.on 'activate-command-mode', => @activateCommandMode()
@setupCommandMode()
@@ -39,7 +39,7 @@ class VimMode
'w': 'move-to-next-word'
'b': 'move-to-previous-word'
'}': 'move-to-next-paragraph'
'esc': 'reset-command-mode'
'escape': 'reset-command-mode'
'left': 'move-left'
'right': 'move-right'