Linux下使用C++获取Caps Lock的状态
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 |
#include <stdio.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/kd.h> void print_caps_lock_state(int state) { printf("Caps Lock state: %s (%d)\n", state & K_CAPSLOCK == K_CAPSLOCK ? "on" : "off", state); } int main(void) { int fd = open("/dev/tty0", O_NOCTTY); if (fd == -1) { perror("open"); return -1; } int state = 0; // Get the keyboard state into the state variable. if (-1 == ioctl(fd, KDGKBLED, &state)) { perror("ioctl"); close(fd); return -1; } print_caps_lock_state(state); // Toggle the caps lock state state ^= K_CAPSLOCK; // Set the new state if (-1 == ioctl(fd, KDSKBLED, state)) { perror("ioctl set"); close(fd); return -1; } // Get the new state state = 0; if (-1 == ioctl(fd, KDGKBLED, &state)) { perror("ioctl"); close(fd); return -1; } print_caps_lock_state(state); close(fd); return 0; } |
除非注明,否则均为浮生笔记原创文章,转载必须以链接形式标明本文链接