Advertisement
Guest User

Untitled

a guest
Jul 26th, 2015
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5.  
  6. bool match(char c, std::string line) {
  7.     for(size_t i = 0; i < line.length(); ++i)
  8.         if(c == line[i])
  9.             return true;
  10.     return false;
  11. }
  12.  
  13. int main(int argc, char **argv) {
  14.     std::string process;
  15.     const char
  16.         CNTRL   =   '^',
  17.             ALT =   '~',
  18.         CMD =   '@',
  19.         BCKSL   =   '\\';
  20.     std::map <char, bool>   flag;
  21.     for(auto &each : {CNTRL, ALT, CMD, BCKSL})
  22.         flag[each] = false;
  23.     std::string
  24.         all_opts    = std::string() + CNTRL + ALT + CMD,
  25.         all_sp      = all_opts + BCKSL;
  26.     std::map <char, int>    key_code;
  27.     key_code['n']   = 52;
  28.     key_code['h']   = 123;
  29.     key_code['j']   = 125;
  30.     key_code['k']   = 126;
  31.     key_code['l']   = 124;
  32.     switch(argc) {
  33.         case 1:
  34.             return 1;
  35.             break;
  36.         case 2:
  37.             process = "Terminal";
  38.             break;
  39.         case 3:
  40.             process = argv[1];
  41.             break;
  42.  
  43.     }
  44.     bool first  = 1;
  45.  
  46.     for(int i = argc - 1; i < argc; ++i)
  47.         for(int j = 0; j < strlen(argv[i]); ++j) {
  48.             if(!(!flag[BCKSL] && match(argv[i][j], all_sp))) {
  49.                 printf("osascript -e 'tell application \"%s\" to activate' -e 'tell application \"System Events\" to tell process \"%s\" to ", process.c_str(), process.c_str());
  50.                 if(flag[BCKSL] && !match(argv[i][j], all_opts)) {
  51.                     if(match(argv[i][j], "nhjkl"))
  52.                         printf("key code %d", key_code[argv[i][j]]);
  53.                     else
  54.                         printf("keystroke \"\\%c\"", argv[i][j]);
  55.                     flag[BCKSL] = 0;
  56.                 } else
  57.                     printf("keystroke \"%c\"", argv[i][j]);
  58.                 if(
  59.                         flag[CNTRL]
  60.                         || flag[CMD]
  61.                         || flag[ALT]
  62.                 ) {
  63.                     printf(" using { ");
  64.                     if(flag[CNTRL]) {
  65.                         flag[CNTRL] = 0;
  66.                         printf("control down");
  67.                         first = 0;
  68.                     } if(flag[CMD]) {
  69.                         flag[CMD] = 0;
  70.                         if(!first)
  71.                             printf(", ");
  72.                         else
  73.                             first = 0;
  74.                         printf("command down");
  75.                     } if(flag[ALT]) {
  76.                         flag[ALT] = 0;
  77.                         if(!first)
  78.                             printf(", ");
  79.                         else
  80.                             first = 0;
  81.                         printf("option down");
  82.                     }
  83.                     first = 1;
  84.                     printf(" }");
  85.                 }
  86.             }
  87.             if(!flag[BCKSL] && match(argv[i][j], all_sp))
  88.                 flag[argv[i][j]] = 1;
  89.             else
  90.                 flag[BCKSL] = 0;
  91.             if(!(flag[BCKSL] || flag[CNTRL] || flag[CMD] || flag[ALT]))
  92.                 printf("'\n");
  93.         }
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement