/* GetChars - This is a simple program to dump keycodes for keypresses Copyright (C) 2004 Frank Sorenson (frank@tuxrocks.com) 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; either version 2 of the License, or (at your option) any later version. 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. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Compiling: g++ GetChars.cpp -o GetChars */ #include #include #include #include #include int SetTerminalSettings(int Port, int Reset = 0) { static struct termios OldTermios; struct termios NewTermios; if (Reset == 1) { NewTermios = OldTermios; } else { if (tcgetattr(Port, &OldTermios) < 0) return -1; NewTermios = OldTermios; NewTermios.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); NewTermios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); NewTermios.c_cc[VMIN] = 1; NewTermios.c_cc[VTIME] = 0; } if (tcsetattr(Port, TCSAFLUSH, &NewTermios) < 0) return -1; return 0; } void CatchSig(int sig) { SetTerminalSettings(0, 1); exit(0); } int InputWaiting(int Port) { fd_set rsel; int MaxFDs = 32; int Result; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; FD_ZERO(&rsel); FD_SET(Port, &rsel); Result = select(MaxFDs, &rsel, NULL, NULL, &tv); if (!Result) return 0; if (FD_ISSET(Port, &rsel) == 0) return 0; return 1; } void SetupSignals() { if ((int) signal(SIGINT, CatchSig) < 0) { perror("signal"); exit(1); } if ((int) signal(SIGQUIT, CatchSig) < 0) { perror("signal"); exit(1); } if ((int) signal(SIGTERM, CatchSig) < 0) { perror("signal"); exit(1); } } int ReadChar(int Port) { int Result; unsigned char InputChar; Result = read(0, &InputChar, 1); if (Result != 1) return -1; return InputChar; } int main(int argc, char *argv[]) { int InputChar; SetupSignals(); SetTerminalSettings(0); while (1) { InputChar = ReadChar(0); if (InputChar < 0) break; if (InputChar == 0x03) break; /* Control-C */ printf("0x%02X", InputChar); if (InputChar == 0x1B) { while (InputWaiting(0)) { InputChar = ReadChar(0); if (InputChar < 0) break; printf(" 0x%02X", InputChar); } } printf("\n"); } }