Created
February 5, 2016 20:32
-
-
Save serapath/3406ea6afd7793b01a6c to your computer and use it in GitHub Desktop.
User needs to type a unique pattern of keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <html> | |
// <head> | |
// <meta charset="UTF-8"> | |
// </head> | |
// <body> | |
// <script src="asdf.js"></script> | |
// </body> | |
// </html> | |
var charOE = 'ö'.charCodeAt(0) | |
var charSEMICOLON = ';'.charCodeAt(0) | |
var USER_EVENT | |
var systemOE = createSystemEvent(charOE) | |
var systemSEMICOLON = createSystemEvent(charSEMICOLON) | |
document.body.addEventListener('keypress', function handler (ev) { | |
console.log(ev) | |
USER_EVENT = ev | |
console.log('USER EVENT CHAR: ', String.fromCharCode(USER_EVENT.keyCode)) | |
console.log('USER EVENT CODE: ', USER_EVENT.keyCode) | |
console.log('SYSTEM EVENT CHAR (ö): ', String.fromCharCode(systemOE.keyCode)) | |
console.log('SYSTEM EVENT CODE (ö): ', systemOE.keyCode) | |
console.log('SYSTEM EVENT CHAR (;): ', String.fromCharCode(systemSEMICOLON.keyCode)) | |
console.log('SYSTEM EVENT CODE (;): ', systemSEMICOLON.keyCode) | |
}) | |
function getCharCode (event) { | |
return { | |
charCode: String.fromCharCode(event.charCode), | |
keyCode: event.keyCode | |
} | |
} | |
function createSystemEvent(charCode) { | |
// Create KeyboardEvent instance | |
var event = document.createEvent('KeyboardEvents') | |
event.initKeyboardEvent( | |
/* type */ 'keypress', | |
/* bubbles */ true, | |
/* cancelable */ false, | |
/* view */ window, | |
/* keyIdentifier*/ '', | |
/* keyLocation */ 0, | |
/* ctrlKey */ false, | |
/* altKey */ false, | |
/* shiftKey */ false, | |
/* metaKey */ false, | |
/* altGraphKey */ false | |
) | |
// Define custom values | |
// This part requires the script to be run in the page's context | |
var getterCode = {get: function() {return charCode}} | |
var getterChar = {get: function() {return String.fromCharCode(charCode)}} | |
Object.defineProperties(event, { | |
charCode: getterCode, | |
which: getterChar, | |
keyCode: getterCode, // Not fully correct | |
key: getterChar, // Not fully correct | |
char: getterChar | |
}) | |
return event | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment