/** * Keyboard helper * @module lib/keys.js; */ const keys = {}; const key_numbers = {}; const letters = "zxcvbnmasdfghjklqwertyuiop"; const numbers = "1234567890"; let callback = function () {}; letters .toUpperCase() .split("") .map(function (k, i) { keys[k.charCodeAt(0)] = i; }); numbers.split("").map(function (k, i) { keys[k.charCodeAt(0)] = i + letters.length; key_numbers[k.charCodeAt(0)] = true; }); window.addEventListener("keydown", keydown, true); function keydown(e) { if (e.altKey || e.ctrlKey || e.metaKey) { e.stopPropagation(); return; } if ( document.activeElement instanceof HTMLInputElement && e.keyCode in key_numbers ) { e.stopPropagation(); return; } if (!(e.keyCode in keys)) return; var index = keys[e.keyCode]; if (e.shiftKey) index += letters.length; callback(index); } function listen(fn) { callback = fn; } export default { listen };