#include #include #include std::string getInputDevPath() { const char *pdevsName = "/proc/bus/input/devices"; std::string devPath = ""; char buf[8196]; FILE *fp; if ((fp = fopen(pdevsName, "r")) == NULL) { printf("[ERR] Cannot open '%s'\n", pdevsName); return ""; } int rd = fread(buf, sizeof(*buf), sizeof(buf) / sizeof(*buf), fp); fclose(fp); if (rd < 6) { printf("[ERR] Wrong size was read from devs file\n"); return ""; } buf[rd] = 0; char *pHandlers, *pEV = buf; do { pHandlers = strstr(pEV, "Handlers="); pEV = strstr(pHandlers, "EV="); } while (pHandlers && pEV && 0 != strncmp(pEV + 3, "1200", 4)); if (pHandlers && pEV) { char *pevent = strstr(pHandlers, "event"); if (pevent) { devPath += "/dev/input/event"; devPath.push_back(pevent[5]); } else { printf("[ERR] Abnormal keyboard event device\n"); } } else { printf("[ERR] Keyboard event device not found\n"); } return devPath; } int main() { std::string r = "keyboard: " + getInputDevPath(); printf("%s\n", r.c_str()); }