make hotkey configuration more timing tolerant
This commit is contained in:
parent
e1c2618dc8
commit
1d9132de3b
@ -7,42 +7,49 @@
|
|||||||
|
|
||||||
const id = Math.random().toString().slice(2);
|
const id = Math.random().toString().slice(2);
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
const modifierKeys = new Set(['Alt', 'AltGraph', 'Control', 'Fn', 'FnLock', 'Meta', 'Shift', 'Super', ]);
|
const MODIFIERS = new Set(['Alt', 'AltGraph', 'Control', 'Fn', 'FnLock', 'Meta', 'Shift', 'Super', ]);
|
||||||
|
|
||||||
|
|
||||||
let listening = false;
|
let listening = false;
|
||||||
|
let keysPressed = [];
|
||||||
|
|
||||||
|
function addModifiers(event) {
|
||||||
|
// add modifier key if it isn't already present
|
||||||
|
if (MODIFIERS.has(event.key) && keysPressed.indexOf(event.key) === -1) {
|
||||||
|
keysPressed.push(event.key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addMainKey(event) {
|
||||||
|
if (!MODIFIERS.has(event.key)) {
|
||||||
|
keysPressed.push(event.key);
|
||||||
|
|
||||||
|
value.keys = keysPressed.join('+');
|
||||||
|
dispatch('update', {value});
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
|
||||||
|
unlisten();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function listen() {
|
function listen() {
|
||||||
// don't re-listen if we already are
|
// don't re-listen if we already are
|
||||||
if (listening) return;
|
if (listening) return;
|
||||||
|
|
||||||
listening = true;
|
listening = true;
|
||||||
window.addEventListener('keyup', setKeybind, {once: true});
|
window.addEventListener('keydown', addModifiers);
|
||||||
|
window.addEventListener('keyup', addMainKey);
|
||||||
// setTimeout avoids reacting to the click event that we are currently processing
|
// setTimeout avoids reacting to the click event that we are currently processing
|
||||||
setTimeout(() => window.addEventListener('click', cancel, {once: true}), 0);
|
setTimeout(() => window.addEventListener('click', unlisten), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setKeybind(event) {
|
function unlisten() {
|
||||||
// separate events fire for modifier keys, even when they are combined with a regular key
|
|
||||||
if (modifierKeys.has(event.key)) return;
|
|
||||||
|
|
||||||
let keys = [];
|
|
||||||
if (event.ctrlKey) keys.push('Ctrl');
|
|
||||||
if (event.altKey) keys.push('Alt');
|
|
||||||
if (event.metaKey) keys.push('Meta');
|
|
||||||
if (event.shiftKey) keys.push('Shift');
|
|
||||||
// capitalize
|
|
||||||
keys.push(event.key);
|
|
||||||
|
|
||||||
value.keys = keys.join('+');
|
|
||||||
dispatch('update', {value});
|
|
||||||
listening = false;
|
listening = false;
|
||||||
window.removeEventListener('click', cancel, {once: true});
|
keysPressed = [];
|
||||||
event.preventDefault();
|
window.removeEventListener('keydown', addModifiers);
|
||||||
event.stopPropagation();
|
window.removeEventListener('keyup', addMainKey);
|
||||||
}
|
window.removeEventListener('click', unlisten);
|
||||||
|
|
||||||
function cancel() {
|
|
||||||
listening = false;
|
|
||||||
window.removeEventListener('keyup', setKeybind, {once: true});
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user