对于处于同样困境的人来说:
我找不到一个纯Python工作的解决方案。在
我求助于一个非常简单的“C”程序(大部分是表):#include
#include
#include
#include
#include
#include
void INThandler() {
exit(0);
}
char ttab[] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', /* Backspace */
'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\n', /* Enter key */
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* <... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
&#39;-&#39;,
0, /* Left Arrow */
0,
0, /* Right Arrow */
&#39;&#43;&#39;,
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
char ntab[] &#61; {
0, 27, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;0&#39;, &#39;-&#39;, &#39;&#61;&#39;, &#39;\b&#39;, /* Backspace */
&#39;\t&#39;, &#39;q&#39;, &#39;w&#39;, &#39;e&#39;, &#39;r&#39;, &#39;t&#39;, &#39;y&#39;, &#39;u&#39;, &#39;i&#39;, &#39;o&#39;, &#39;p&#39;, &#39;[&#39;, &#39;]&#39;,&#39;\n&#39;, /* Enter key */
0, &#39;a&#39;, &#39;s&#39;, &#39;d&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;;&#39;,&#39;\&#39;&#39;, &#39;&#96;&#39;, 0, /* Left shift */
&#39;\\&#39;, &#39;z&#39;, &#39;x&#39;, &#39;c&#39;, &#39;v&#39;, &#39;b&#39;, &#39;n&#39;, &#39;m&#39;, &#39;,&#39;, &#39;.&#39;, &#39;/&#39;, 0, /* Right shift */
&#39;*&#39;,
0, /* Alt */
&#39; &#39;, /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* <... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
&#39;-&#39;,
0, /* Left Arrow */
0,
0, /* Right Arrow */
&#39;&#43;&#39;,
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
char stab[] &#61; {
0, 27, &#39;!&#39;, &#39;&#64;&#39;, &#39;#&#39;, &#39;$&#39;, &#39;%&#39;, &#39;^&#39;, &#39;&&#39;, &#39;*&#39;, &#39;(&#39;, &#39;)&#39;, &#39;_&#39;, &#39;&#43;&#39;, 0, /* Backspace */
0, &#39;Q&#39;, &#39;W&#39;, &#39;E&#39;, &#39;R&#39;, &#39;T&#39;, &#39;Y&#39;, &#39;U&#39;, &#39;I&#39;, &#39;O&#39;, &#39;P&#39;, &#39;{&#39;, &#39;}&#39;, 0, /* Enter key */
0, &#39;A&#39;, &#39;S&#39;, &#39;D&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;:&#39;, &#39;"&#39;, 0,&#39;\n&#39;, /* Left shift */
0, &#39;Z&#39;, &#39;X&#39;, &#39;C&#39;, &#39;V&#39;, &#39;B&#39;, &#39;N&#39;, &#39;M&#39;, &#39;&#39;, &#39;?&#39;, 0, /* Right shift */
&#39;*&#39;,
0, /* Alt */
&#39; &#39;, /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* <... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
&#39;-&#39;,
0, /* Left Arrow */
0,
0, /* Right Arrow */
&#39;&#43;&#39;,
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
int main() {
char devname[] &#61; "/dev/input/event0";
int device &#61; open(devname, O_RDONLY);
struct input_event ev;
int shift &#61; 0;
char line[4096], *p &#61; line;
signal(SIGINT, INThandler);
fputs("starting\n", stdout);
//fputs("starting\n", stderr);
while (1) {
read(device, &ev, sizeof(ev));
if (ev.type &#61;&#61; 1) {
if (ev.code &#61;&#61; 42)
shift &#61; ev.value;
else if (ev.value) {
//printf("Key: %i State: %i\n", ev.code, ev.value);
char *t &#61; shift? stab: ntab;
char ch &#61; t[ev.code];
//printf("Key: %02d State: %d [%c]\n", ev.code, ev.value, ch);
if (ch &#61;&#61; &#39;\n&#39;) {
*p &#61; &#39;\0&#39;;
fputs(line, stdout); fputc(&#39;\n&#39;, stdout); fflush(stdout);
//fputs(line, stderr); fputc(&#39;\n&#39;, stderr); fflush(stderr);
p &#61; line;
} else
*p&#43;&#43; &#61; ch;
}
}
}
}
。。。从Python驱动&#xff1a;
^{pr2}$
我相信Python驱动程序可以简化。这是我(工作)申请表中的逐字记录。在
嗨&#xff01;在