Files
atom/src/stdlib/key-binder.coffee
Nathan Sobo a1e0039890 Centralize key binding logic in KeyBinder and jQuery.fn.bindKey extension.
This commit removes window.bindKey in favor of binding keys on dom elements. It also refactors pattern parsing in the test helper to use KeyBinder.parseKeyPattern.
2011-12-30 13:19:41 -06:00

41 lines
1.0 KiB
CoffeeScript

module.exports =
class KeyBinder
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
keyEventMatchesPattern: (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) ->
[modifiers..., key] = pattern.split '+'
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