Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <map>
- #include <string>
- bool match(char c, std::string line) {
- for(size_t i = 0; i < line.length(); ++i)
- if(c == line[i])
- return true;
- return false;
- }
- int main(int argc, char **argv) {
- std::string process;
- const char
- CNTRL = '^',
- ALT = '~',
- CMD = '@',
- BCKSL = '\\';
- std::map <char, bool> flag;
- for(auto &each : {CNTRL, ALT, CMD, BCKSL})
- flag[each] = false;
- std::string
- all_opts = std::string() + CNTRL + ALT + CMD,
- all_sp = all_opts + BCKSL;
- std::map <char, int> key_code;
- key_code['n'] = 52;
- key_code['h'] = 123;
- key_code['j'] = 125;
- key_code['k'] = 126;
- key_code['l'] = 124;
- switch(argc) {
- case 1:
- return 1;
- break;
- case 2:
- process = "Terminal";
- break;
- case 3:
- process = argv[1];
- break;
- }
- bool first = 1;
- for(int i = argc - 1; i < argc; ++i)
- for(int j = 0; j < strlen(argv[i]); ++j) {
- if(!(!flag[BCKSL] && match(argv[i][j], all_sp))) {
- printf("osascript -e 'tell application \"%s\" to activate' -e 'tell application \"System Events\" to tell process \"%s\" to ", process.c_str(), process.c_str());
- if(flag[BCKSL] && !match(argv[i][j], all_opts)) {
- if(match(argv[i][j], "nhjkl"))
- printf("key code %d", key_code[argv[i][j]]);
- else
- printf("keystroke \"\\%c\"", argv[i][j]);
- flag[BCKSL] = 0;
- } else
- printf("keystroke \"%c\"", argv[i][j]);
- if(
- flag[CNTRL]
- || flag[CMD]
- || flag[ALT]
- ) {
- printf(" using { ");
- if(flag[CNTRL]) {
- flag[CNTRL] = 0;
- printf("control down");
- first = 0;
- } if(flag[CMD]) {
- flag[CMD] = 0;
- if(!first)
- printf(", ");
- else
- first = 0;
- printf("command down");
- } if(flag[ALT]) {
- flag[ALT] = 0;
- if(!first)
- printf(", ");
- else
- first = 0;
- printf("option down");
- }
- first = 1;
- printf(" }");
- }
- }
- if(!flag[BCKSL] && match(argv[i][j], all_sp))
- flag[argv[i][j]] = 1;
- else
- flag[BCKSL] = 0;
- if(!(flag[BCKSL] || flag[CNTRL] || flag[CMD] || flag[ALT]))
- printf("'\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement