#include #include static const std::string CMD_GET_KBD_DEV_NUM = "grep -E 'Handlers|EV=' /proc/bus/input/devices | grep -B1 'EV=1200..' | grep -Eo 'event[0-9]+' | grep -Eo '[0-9]+' | tr -d '\n'"; static const std::string CMD_COUNT_KBD_DEV = "grep -E 'Handlers|EV=' /proc/bus/input/devices | grep -B1 'EV=1200..' | grep -Eo 'event[0-9]+' | wc -l"; std::string execCmd(const char *cmd) { FILE *pipe = popen(cmd, "r"); char buf[2048]; std::string r = ""; while (!feof(pipe)) if (fgets(buf, 2048, pipe) != NULL) r += buf; pclose(pipe); return r; } std::string getKbdDevPath() { std::string kbdCnt = ""; std::string devNum = ""; kbdCnt += execCmd(CMD_COUNT_KBD_DEV.c_str()); if (kbdCnt[0] == '0') { // Not found keyboard return ""; } else if (kbdCnt[0] == '1') { devNum += execCmd(CMD_GET_KBD_DEV_NUM.c_str()); if (devNum[0] == '\0') { // Not found keyboard return ""; } // found keyboard return "/dev/input/event" + devNum; } printf("Multiple keyboards exist\n"); return ""; } int main() { std::string devPath = getKbdDevPath(); if (devPath[0] == '\0') { printf("Not found keyboard\n"); } else { printf("keyboard: %s\n", devPath.c_str()); } }