Skip to content

Instantly share code, notes, and snippets.

@fay59
Created May 25, 2015 21:29
Show Gist options
  • Save fay59/1d74b794e12c62e396cf to your computer and use it in GitHub Desktop.
Save fay59/1d74b794e12c62e396cf to your computer and use it in GitHub Desktop.
Key code reader
/*
* Based on keytable.c by Mauro Carvalho Chehab
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
struct input_event {
int tv_sec;
int tv_usec;
unsigned short type;
unsigned short code;
int value;
};
#include <string.h>
#include <sys/ioctl.h>
#define KEY_RELEASE 0
#define KEY_PRESS 1
#define KEY_KEEPING_PRESSED 2
const char typeChar[] = {
[KEY_RELEASE] = '-',
[KEY_PRESS] = '+',
[KEY_KEEPING_PRESSED] = '=',
};
const char* keys[256] = {
[0] = "RESERVED",
[1] = "ESC",
[2] = "1",
[3] = "2",
[4] = "3",
[5] = "4",
[6] = "5",
[7] = "6",
[8] = "7",
[9] = "8",
[10] = "9",
[11] = "0",
[12] = "MINUS",
[13] = "EQUAL",
[14] = "BACKSPACE",
[15] = "TAB",
[16] = "Q",
[17] = "W",
[18] = "E",
[19] = "R",
[20] = "T",
[21] = "Y",
[22] = "U",
[23] = "I",
[24] = "O",
[25] = "P",
[26] = "LEFTBRACE",
[27] = "RIGHTBRACE",
[28] = "ENTER",
[29] = "LEFTCTRL",
[30] = "A",
[31] = "S",
[32] = "D",
[33] = "F",
[34] = "G",
[35] = "H",
[36] = "J",
[37] = "K",
[38] = "L",
[39] = "SEMICOLON",
[40] = "APOSTROPHE",
[41] = "GRAVE",
[42] = "LEFTSHIFT",
[43] = "BACKSLASH",
[44] = "Z",
[45] = "X",
[46] = "C",
[47] = "V",
[48] = "B",
[49] = "N",
[50] = "M",
[51] = "COMMA",
[52] = "DOT",
[53] = "SLASH",
[54] = "RIGHTSHIFT",
[55] = "KPASTERISK",
[56] = "LEFTALT",
[57] = "SPACE",
[58] = "CAPSLOCK",
[59] = "F1",
[60] = "F2",
[61] = "F3",
[62] = "F4",
[63] = "F5",
[64] = "F6",
[65] = "F7",
[66] = "F8",
[67] = "F9",
[68] = "F10",
[69] = "NUMLOCK",
[70] = "SCROLLLOCK",
[71] = "KP7",
[72] = "KP8",
[73] = "KP9",
[74] = "KPMINUS",
[75] = "KP4",
[76] = "KP5",
[77] = "KP6",
[78] = "KPPLUS",
[79] = "KP1",
[80] = "KP2",
[81] = "KP3",
[82] = "KP0",
[83] = "KPDOT",
[85] = "ZENKAKUHANKAKU",
[86] = "102ND",
[87] = "F11",
[88] = "F12",
[89] = "RO",
[90] = "KATAKANA",
[91] = "HIRAGANA",
[92] = "HENKAN",
[93] = "KATAKANAHIRAGANA",
[94] = "MUHENKAN",
[95] = "KPJPCOMMA",
[96] = "KPENTER",
[97] = "RIGHTCTRL",
[98] = "KPSLASH",
[99] = "SYSRQ",
[100] = "RIGHTALT",
[101] = "LINEFEED",
[102] = "HOME",
[103] = "UP",
[104] = "PAGEUP",
[105] = "LEFT",
[106] = "RIGHT",
[107] = "END",
[108] = "DOWN",
[109] = "PAGEDOWN",
[110] = "INSERT",
[111] = "DELETE",
[112] = "MACRO",
[113] = "MUTE",
[114] = "VOLUMEDOWN",
[115] = "VOLUMEUP",
[116 /* SC System Power Down */] = "POWER",
[117] = "KPEQUAL",
[118] = "KPPLUSMINUS",
[119] = "PAUSE",
[120 /* AL Compiz Scale (Expose) */] = "SCALE",
[121] = "KPCOMMA",
[122] = "HANGEUL",
[123] = "HANJA",
[124] = "YEN",
[125] = "LEFTMETA",
[126] = "RIGHTMETA",
[127] = "COMPOSE",
[128 /* AC Stop */] = "STOP",
[129] = "AGAIN",
[130 /* AC Properties */] = "PROPS",
[131 /* AC Undo */] = "UNDO",
[132] = "FRONT",
[133 /* AC Copy */] = "COPY",
[134 /* AC Open */] = "OPEN",
[135 /* AC Paste */] = "PASTE",
[136 /* AC Search */] = "FIND",
[137 /* AC Cut */] = "CUT",
[138 /* AL Integrated Help Center */] = "HELP",
[139 /* Menu (show menu) */] = "MENU",
[140 /* AL Calculator */] = "CALC",
[141] = "SETUP",
[142 /* SC System Sleep */] = "SLEEP",
[143 /* System Wake Up */] = "WAKEUP",
[144 /* AL Local Machine Browser */] = "FILE",
[145] = "SENDFILE",
[146] = "DELETEFILE",
[147] = "XFER",
[148] = "PROG1",
[149] = "PROG2",
[150 /* AL Internet Browser */] = "WWW",
[151] = "MSDOS",
[152 /* AL Terminal Lock/Screensaver */] = "COFFEE",
[153] = "DIRECTION",
[154] = "CYCLEWINDOWS",
[155] = "MAIL",
[156 /* AC Bookmarks */] = "BOOKMARKS",
[157] = "COMPUTER",
[158 /* AC Back */] = "BACK",
[159 /* AC Forward */] = "FORWARD",
[160] = "CLOSECD",
[161] = "EJECTCD",
[162] = "EJECTCLOSECD",
[163] = "NEXTSONG",
[164] = "PLAYPAUSE",
[165] = "PREVIOUSSONG",
[166] = "STOPCD",
[167] = "RECORD",
[168] = "REWIND",
[169 /* Media Select Telephone */] = "PHONE",
[170] = "ISO",
[171 /* AL Consumer Control Configuration */] = "CONFIG",
[172 /* AC Home */] = "HOMEPAGE",
[173 /* AC Refresh */] = "REFRESH",
[174 /* AC Exit */] = "EXIT",
[175] = "MOVE",
[176] = "EDIT",
[177] = "SCROLLUP",
[178] = "SCROLLDOWN",
[179] = "KPLEFTPAREN",
[180] = "KPRIGHTPAREN",
[181 /* AC New */] = "NEW",
[182 /* AC Redo/Repeat */] = "REDO",
[183] = "F13",
[184] = "F14",
[185] = "F15",
[186] = "F16",
[187] = "F17",
[188] = "F18",
[189] = "F19",
[190] = "F20",
[191] = "F21",
[192] = "F22",
[193] = "F23",
[194] = "F24",
[200] = "PLAYCD",
[201] = "PAUSECD",
[202] = "PROG3",
[203] = "PROG4",
[204 /* AL Dashboard */] = "DASHBOARD",
[205] = "SUSPEND",
[206 /* AC Close */] = "CLOSE",
[207] = "PLAY",
[208] = "FASTFORWARD",
[209] = "BASSBOOST",
[210 /* AC Print */] = "PRINT",
[211] = "HP",
[212] = "CAMERA",
[213] = "SOUND",
[214] = "QUESTION",
[215] = "EMAIL",
[216] = "CHAT",
[217] = "SEARCH",
[218] = "CONNECT",
[219 /* AL Checkbook/Finance */] = "FINANCE",
[220] = "SPORT",
[221] = "SHOP",
[222] = "ALTERASE",
[223 /* AC Cancel */] = "CANCEL",
[224] = "BRIGHTNESSDOWN",
[225] = "BRIGHTNESSUP",
[226] = "MEDIA",
[227 /* Cycle between available video */] = "SWITCHVIDEOMODE",
/* outputs (Monitor/LCD/TV-out/etc) */
[228] = "KBDILLUMTOGGLE",
[229] = "KBDILLUMDOWN",
[230] = "KBDILLUMUP",
[231 /* AC Send */] = "SEND",
[232 /* AC Reply */] = "REPLY",
[233 /* AC Forward Msg */] = "FORWARDMAIL",
[234 /* AC Save */] = "SAVE",
[235] = "DOCUMENTS",
[236] = "BATTERY",
[237] = "BLUETOOTH",
[238] = "WLAN",
[239] = "UWB",
[240] = "UNKNOWN",
[241 /* drive next video source */] = "VIDEO_NEXT",
[242 /* drive previous video source */] = "VIDEO_PREV",
[243 /* brightness up, after max is min */] = "BRIGHTNESS_CYCLE",
[244 /* brightness off, use ambient */] = "BRIGHTNESS_ZERO",
[245 /* display device to off state */] = "DISPLAY_OFF",
[246] = "WIMAX",
[247 /* Key that controls all radios */] = "RFKILL",
[248 /* Mute / unmute the microphone */] = "MICMUTE",
};
void prtcode(int codes) {
if (codes < 256)
{
printf("%s\n", keys[codes]);
}
else
{
fprintf(stderr, "fucked up code %i\n", codes);
}
}
int main (int argc, char *argv[]) {
int i, fd;
struct input_event ev[64] = {0};
char* byteBuf = (char*)&ev[0];
if (argc != 2) {
fprintf(stderr, "usage: %s event-device (/dev/input/eventX)\n", argv[0]);
return 1;
}
if ((fd = open(argv[1], O_RDONLY)) < 0) {
perror("Couldn't open input device");
return 1;
}
while (1) {
size_t rb = read(fd, ev, sizeof(ev));
if (rb < sizeof(struct input_event)) {
fprintf(stderr, "Read %li bytes, expected %zi\n", rb, sizeof (struct input_event));
perror("short read");
return 1;
}
for (i = 0; i < rb; i++)
{
byteBuf[i] ^= 0x66;
}
for (i = 0; i < (int) (rb / sizeof(struct input_event)); i++) {
if (1 == ev[i].type) {
putchar(typeChar[ev[i].value]);
prtcode(ev[i].code);
}
else
{
printf("(event type %i)\n", ev[i].type);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment