Atlas - main.c
Home / usr / key_detect / src Lines: 10 | Size: 2041 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/** 2 * Key detect (main.c) 3 * A simple program to detect keypresses. 4 * Github: https://www.github.com/0x4248/key_detect 5 * Licence: GNU General Public License v3.0 6 * By: 0x4248 7 */ 8 9#include <stdbool.h> 10#include <stdio.h> 11#include <stdlib.h> 12#include <termios.h> 13#include <unistd.h> 14 15int main(int argc, char *argv[]) { 16 bool binary_mode = false; 17 bool hex_mode = false; 18 19 /* Argument parsing */ 20 if (argc > 1) { 21 if (argv[1][0] == '-' && argv[1][1] == 'b') { 22 binary_mode = true; 23 } else if (argv[1][0] == '-' && argv[1][1] == 'h') { 24 printf("Usage: key_detect [-b]\n"); 25 printf(" -b\tBinary mode. Prints the binary representation of the " 26 "keypress.\n"); 27 printf(" -x\tHexadecimal mode. Prints the hexadecimal " 28 "representation of the keypress.\n"); 29 printf(" -h\tPrints this help message.\n"); 30 return EXIT_SUCCESS; 31 } else if (argv[1][0] == '-' && argv[1][1] == '-' && 32 argv[1][2] == 'h' && argv[1][3] == 'e' && 33 argv[1][4] == 'x' || 34 argv[1][0] == '-' && argv[1][1] == 'x') { 35 hex_mode = true; 36 } 37 } 38 39 struct termios oldt, newt; 40 int ch; 41 tcgetattr(STDIN_FILENO, &oldt); 42 newt = oldt; 43 newt.c_lflag &= ~(ICANON | ECHO); 44 tcsetattr(STDIN_FILENO, TCSANOW, &newt); 45 46 printf("Listening for keypresses.\nPress Ctrl+C to stop listening and " 47 "exit.\n"); 48 49 while (true) { 50 /* Main loop */ 51 ch = getchar(); 52 if (binary_mode) { 53 for (int i = 7; i >= 0; i--) { 54 printf("%d", (ch >> i) & 1); 55 } 56 printf("\n"); 57 } else if (hex_mode) { 58 printf("Code: 0x%x\tCharacter: %c\n", ch, ch); 59 fflush(stdout); 60 } else { 61 printf("Code: %d\tCharacter: %c\n", ch, ch); 62 fflush(stdout); 63 } 64 } 65 66 tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 67 68 return EXIT_SUCCESS; 69}[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.