我需要访问我正在编写的控制台应用程序(个性化编辑器)的修饰键状态。
是否有任何包/库/任何内容提供此访问权限?
我从某个地方拼凑了以下内容,但只有当你是 root 时它才有效,而且我真的不想在 root 级别搞乱。
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include <errno.h>
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~0000172 ; //~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
return ch;
}
enum MODKEYS
{
SHIFT_L = 1,
SHIFT_R = 2,
CTRL_L = 4,
CTRL_R = 8,
ALT_L = 16,
ALT_R = 32,
};
int chkmodifiers()
{
int mods=0,keyb,mask;
char key_map[KEY_MAX/8 + 1]; // Create a byte array the size of the number of keys
//event1 - got by inspecting /dev/input/...
FILE *kbd = fopen("/dev/input/event1", "r");
if (kbd == NULL)
{
printf("(chkmodifiers) ERROR: %s\n", strerror(errno)); //permission - got to be root!
return 0;
}
memset(key_map, 0, sizeof(key_map));
ioctl(fileno(kbd), EVIOCGKEY(sizeof(key_map)), key_map); // Fill the keymap with the current keyboard state
keyb = key_map[KEY_LEFTSHIFT/8];
mask = 1 << (KEY_LEFTSHIFT % 8);
if (keyb & mask) mods += SHIFT_L;
keyb = key_map[KEY_RIGHTSHIFT/8];
mask = 1 << (KEY_RIGHTSHIFT % 8);
if (keyb & mask) mods += SHIFT_R;
keyb = key_map[KEY_LEFTCTRL/8];
mask = 1 << (KEY_LEFTCTRL % 8);
if (keyb & mask) mods += CTRL_L;
keyb = key_map[KEY_RIGHTCTRL/8];
mask = 1 << (KEY_RIGHTCTRL % 8);
if (keyb & mask) mods += CTRL_R;
keyb = key_map[KEY_LEFTALT/8];
mask = 1 << (KEY_LEFTALT % 8);
if (keyb & mask) mods += ALT_L;
keyb = key_map[KEY_RIGHTALT/8];
mask = 1 << (KEY_RIGHTALT % 8);
if (keyb & mask) mods += ALT_R;
return mods;
}
int main()
{
puts("Press a key!");
char ch=0;
int n=0,m;
while (ch != 'q')
{
n = kbhit();
if (n != -1)
{
m = chkmodifiers();
ch = (char)n;
printf("You pressed '%c' [%d]\n", ch, n);
if ((m & SHIFT_L) == SHIFT_L) printf(" .. and ls\n");
if ((m & SHIFT_R) == SHIFT_R) printf(" .. and rs\n");
if ((m & CTRL_L) == CTRL_L) printf(" .. and lc\n");
if ((m & CTRL_R) == CTRL_R) printf(" .. and rc\n");
if ((m & ALT_L) == ALT_L) printf(" .. and la\n");
if ((m & ALT_R) == ALT_R) printf(" .. and ra\n");
}
}
return 0;
}
ベストアンサー1
也许看看libtermkey,一个终端键输入库,可识别特殊键(例如箭头和功能键),包括“修改”键,例如Ctrl-Left
.
另一种选择可能是增强魅力,一个最小的 ncurses 副本。