Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Object to hold the state of pressed keys
- var keysPressed = {};
- // List of keys to capture
- /* {{ var#keysToCapture={{ project root }}/scripts/json/gamevars/keys.json }} */
- function listKeys(keys){
- list = ``;
- Object.keys(keys).forEach(key => {
- list += (len(list)>0?`, `:``)+keys[key];
- });
- return list;
- }
- console.log("Valid Keystrokes: "+listKeys(keysToCapture));
- // Event listener for keydown to update the keysPressed object
- document.addEventListener('keydown', function(event) {
- // console.log ( `key down: ${event.code}` );
- var code = keyEncode ( event.code );
- var keyIndex = keysToCapture.indexOf(code);
- console.log ( `keystroke: "${code}" (down: ${keyIndex})` );
- if ( keysToCapture.indexOf(code) === -1 ) {
- return;
- }
- keysPressed[event.code] = true;
- for ( option = 0; option < triggerKeys.length; option++ ) {
- if ( len(triggerKeys[option].key) === 1 ) {
- triggerKeys[option].key = `Key`+triggerKeys[option].key;
- }
- if ( triggerKeys[option].key === event.code ) {
- page = triggerKeys[option].dest;
- console.log ( `new page: ${page}` );
- canvasRenderPage ( );
- break;
- }
- }
- });
- // Event listener for keyup to update the keysPressed object
- document.addEventListener('keyup', function(event) {
- // console.log ( `key up: ${event.code}` );
- var code = keyEncode ( event.code );
- var keyIndex = keysToCapture.indexOf(code);
- console.log ( `keystroke: "${code}" (up: ${keyIndex})` );
- if ( keysToCapture.indexOf(code) === -1 ) {
- return;
- }
- keysPressed[event.code] = false;
- });
- // Set up the interval to check key presses every 100 milliseconds
- setInterval(backbuffer, 100);
- function keyEncode ( code ) {
- code = addSpacesBeforeCapitalsAndNumbers(code);
- code = code.split(" ");
- if ( code[0] === `F` ) {
- code = code.join(``);
- }else if(code[1] !== undefined){
- code = code[0] + " (" + code[1] + ")";
- }else{
- code = code[0];
- }
- code = ucwords ( code );
- return code;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement