Advertisement
jargon

Story05Js :: "canvasKeyListener.js"

Jul 7th, 2024 (edited)
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Object to hold the state of pressed keys
  3. var keysPressed = {};
  4.  
  5. // List of keys to capture
  6. /* {{ var#keysToCapture={{ project root }}/scripts/json/gamevars/keys.json }} */
  7.  
  8. function listKeys(keys){
  9.     list = ``;
  10.     Object.keys(keys).forEach(key => {
  11.         list += (len(list)>0?`, `:``)+keys[key];
  12.     });
  13.     return list;
  14. }
  15.  
  16. console.log("Valid Keystrokes: "+listKeys(keysToCapture));
  17.  
  18. // Event listener for keydown to update the keysPressed object
  19. document.addEventListener('keydown', function(event) {
  20.    
  21.     // console.log ( `key down: ${event.code}` );
  22.    
  23.     var code = keyEncode ( event.code );
  24.  
  25.     var keyIndex = keysToCapture.indexOf(code);
  26.    
  27.     console.log ( `keystroke: "${code}" (down: ${keyIndex})` );
  28.  
  29.     if ( keysToCapture.indexOf(code) === -1 ) {
  30.         return;
  31.     }
  32.    
  33.     keysPressed[event.code] = true;    
  34.        
  35.     for ( option = 0; option < triggerKeys.length; option++ ) {
  36.        
  37.         if ( len(triggerKeys[option].key) === 1 ) {
  38.             triggerKeys[option].key = `Key`+triggerKeys[option].key;
  39.         }
  40.        
  41.         if ( triggerKeys[option].key === event.code ) {
  42.            
  43.             page = triggerKeys[option].dest;
  44.            
  45.             console.log ( `new page: ${page}` );
  46.            
  47.             canvasRenderPage ( );
  48.  
  49.             break;
  50.         }
  51.     }
  52. });
  53.  
  54. // Event listener for keyup to update the keysPressed object
  55. document.addEventListener('keyup', function(event) {
  56.    
  57.     // console.log ( `key up: ${event.code}` );
  58.    
  59.     var code = keyEncode ( event.code );
  60.    
  61.     var keyIndex = keysToCapture.indexOf(code);
  62.     console.log ( `keystroke: "${code}" (up: ${keyIndex})` );
  63.    
  64.     if ( keysToCapture.indexOf(code) === -1 ) {
  65.         return;
  66.     }
  67.    
  68.     keysPressed[event.code] = false;
  69.    
  70. });
  71.  
  72. // Set up the interval to check key presses every 100 milliseconds
  73. setInterval(backbuffer, 100);
  74.  
  75. function keyEncode ( code ) {
  76.    
  77.     code = addSpacesBeforeCapitalsAndNumbers(code);
  78.    
  79.     code = code.split(" ");
  80.     if ( code[0] === `F` ) {
  81.         code = code.join(``);
  82.     }else if(code[1] !== undefined){
  83.         code = code[0] + " (" + code[1] + ")";
  84.     }else{
  85.         code = code[0];
  86.     }
  87.    
  88.     code = ucwords ( code );
  89.    
  90.     return code;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement