Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 4df69d1

Browse files
maxbrunsfeldNathan Sobo
authored andcommitted
Switch buildKeydownEvent to use new keyboard event APIs
Signed-off-by: Nathan Sobo <nathan@github.com>
1 parent dc61898 commit 4df69d1

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

src/helpers.coffee

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,37 @@ exports.keyupEvent = (key, options) ->
112112
return keyboardEvent(key, 'keyup', options)
113113

114114
keyboardEvent = (key, eventType, {ctrl, shift, alt, cmd, keyCode, target, location}={}) ->
115-
event = document.createEvent('KeyboardEvent')
115+
ctrlKey = ctrl ? false
116+
altKey = alt ? false
117+
shiftKey = shift ? false
118+
metaKey = cmd ? false
116119
bubbles = true
117120
cancelable = true
118-
view = null
121+
code = Object.keys(NonPrintableKeyNamesByCode).find (candidateCode) ->
122+
NonPrintableKeyNamesByCode[candidateCode] is key
123+
124+
unless code?
125+
for candidateCode, characters of KeyboardLayout.getCurrentKeymap()
126+
if characters.unmodified is key
127+
code = candidateCode
128+
else if characters.withShift is key
129+
code = candidateCode
130+
shiftKey = true
131+
else if characters.withAltGr is key
132+
code = candidateCode
133+
altKey = true
134+
else if characters.withShiftAltGr is key
135+
code = candidateCode
136+
altKey = true
137+
shiftKey = true
138+
139+
event = new KeyboardEvent(eventType, {
140+
code, ctrlKey, altKey, shiftKey, metaKey, bubbles, cancelable
141+
})
119142

120-
key = key.toUpperCase() if LowerCaseLetterRegex.test(key)
121-
if key.length is 1
122-
keyIdentifier = "U+#{key.charCodeAt(0).toString(16)}"
123-
else
124-
switch key
125-
when 'ctrl'
126-
keyIdentifier = 'Control'
127-
ctrl = true if eventType isnt 'keyup'
128-
when 'alt'
129-
keyIdentifier = 'Alt'
130-
alt = true if eventType isnt 'keyup'
131-
when 'shift'
132-
keyIdentifier = 'Shift'
133-
shift = true if eventType isnt 'keyup'
134-
when 'cmd'
135-
keyIdentifier = 'Meta'
136-
cmd = true if eventType isnt 'keyup'
137-
else
138-
keyIdentifier = key[0].toUpperCase() + key[1..]
139-
140-
location ?= KeyboardEvent.DOM_KEY_LOCATION_STANDARD
141-
event.initKeyboardEvent(eventType, bubbles, cancelable, view, keyIdentifier, location, ctrl, alt, shift, cmd)
142143
if target?
143144
Object.defineProperty(event, 'target', get: -> target)
144145
Object.defineProperty(event, 'path', get: -> [target])
145-
Object.defineProperty(event, 'keyCode', get: -> keyCode)
146-
Object.defineProperty(event, 'which', get: -> keyCode)
147146
event
148147

149148
# bindingKeystrokes and userKeystrokes are arrays of keystrokes

0 commit comments

Comments
 (0)