Guest User

Untitled

a guest
Jul 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var exec = require('child_process').exec,
  2. config = require('./config.js'),
  3. lastTime = {},
  4. windowID = 'unfilled',
  5. throttledCommands = config.throttledCommands,
  6. regexThrottle = new RegExp('^(' + throttledCommands.join('|') + ')$', 'i'),
  7. regexFilter = new RegExp('^(' + config.filteredCommands.join('|') + ')$', 'i');
  8.  
  9. for (var i = 0; i < throttledCommands.length; i++) {
  10.     lastTime[throttledCommands[i]] = new Date().getTime();
  11. }
  12.  
  13. function setWindowID() {
  14.     if (config.os === 'other' & windowID === 'unfilled') {
  15.         exec('xdotool search --onlyvisible --name ' + config.programName, function(error, stdout) {
  16.             windowID = stdout.trim();
  17.              console.log(key, windowID);
  18.         });
  19.     }
  20. }
  21.  
  22. var defaultKeyMap = config.keymap || {
  23.     'up':'Up','left':'Left','down':'Down','right':'Right',
  24.     'a':'a','b':'b',
  25.     'x':'x','y':'y',
  26.     'start':'s','select':'e'
  27. };
  28.  
  29. function sendKey(command) {
  30.     //if doesn't match the filtered words
  31.     if (!command.match(regexFilter)) {
  32.         var allowKey = true,
  33.         key = defaultKeyMap[command] || command;
  34.         //throttle certain commands (not individually though)
  35.         if (key.match(regexThrottle)) {
  36.             var newTime = new Date().getTime();
  37.             if (newTime - lastTime[key] < config.timeToWait) {
  38.                 allowKey = false;
  39.             } else {
  40.                 lastTime = newTime;
  41.             }
  42.         }
  43.         if (allowKey) {
  44.             //if xdotool is installed
  45.             if (config.os === 'other') {
  46.                 //Send to preset window under non-windows systems
  47.                 exec('xdotool key --window ' + windowID + ' --delay ' + config.delay + ' ' + key);
  48.             } else {
  49.                 //use python on windows
  50.                 // "VisualBoyAdvance"
  51.                 // "DeSmuME 0.9.10 x64"
  52.                
  53.                 console.log('key.py' + ' ' + config.programName + ' ' + key);
  54.                 exec('key.py' + ' ' + config.programName + ' ' + key);
  55.                
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. //Only actually does something when not running under windows.
  62. setWindowID();
  63.  
  64. exports.sendKey = sendKey;
Add Comment
Please, Sign In to add comment