1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#ifndef GETCH_H #define GETCH_H #include <stdio.h> #include <termios.h> #include <stdlib.h> #include <unistd.h>
typedef enum KEYBOARD { KEY_UP = 183, KEY_DOWN = 184, KEY_RIGHT = 185, KEY_LEFT = 186, KEY_BACKSPACE = 127, KEY_ENTER = 10, KEY_0 = 48, KEY_1 = 49, KEY_2 = 50, KEY_3 = 51, KEY_4 = 52, KEY_5 = 53, KEY_6 = 54, KEY_7 = 55, KEY_8 = 56, KEY_9 = 57, KEY_A = 65, KEY_B = 66, KEY_C = 67, KEY_D = 68, KEY_E = 69, KEY_F = 70, KEY_G = 71, KEY_H = 72, KEY_I = 73, KEY_J = 74, KEY_K = 75, KEY_L = 76, KEY_M = 77, KEY_N = 78, KEY_O = 79, KEY_P = 80, KEY_Q = 81, KEY_R = 82, KEY_S = 83, KEY_T = 84, KEY_U = 85, KEY_V = 86, KEY_W = 87, KEY_X = 88, KEY_Y = 89, KEY_Z = 90, KEY_a = 97, KEY_b = 98, KEY_c = 99, KEY_d = 100, KEY_e = 101, KEY_f = 102, KEY_g = 103, KEY_h = 104, KEY_i = 105, KEY_j = 106, KEY_k = 107, KEY_l = 108, KEY_m = 109, KEY_n = 110, KEY_o = 111, KEY_p = 112, KEY_q = 113, KEY_r = 114, KEY_s = 115, KEY_t = 116, KEY_u = 117, KEY_v = 118, KEY_w = 119, KEY_x = 120, KEY_y = 121, KEY_z = 122 }KEYBOARD;
static int get_keyboard(void) { int ret = 0; struct termios old; ret=tcgetattr(STDIN_FILENO,&old); if(0 > ret) { perror("tcgetattr"); return -1; } struct termios new = old; new.c_lflag &= ~(ICANON|ECHO); ret= tcsetattr(STDIN_FILENO,TCSANOW,&new); if(0 > ret) { perror("tcsetattr"); return -2; } int key_value = 0; do { key_value += getchar(); }while(stdin->_IO_read_end - stdin->_IO_read_ptr); ret = tcsetattr(STDIN_FILENO,TCSANOW,&old); if(0 > ret) { perror("tcsetattr"); return -3; } return key_value; } #endif
|