显示自定义控制台中按下的键

显示自定义控制台中按下的键

我有一个自定义客户端,它通过 websocket 连接到远程 Ubuntu 并在那里运行 bash。但 Del、箭头等键不起作用。我如何输出服务器端收到的内容?在 Linux 中显示按下的键工作。

更新:我首先问了有关 SSH 的问题,但后来意识到我可能没有使用 SSH。

更新2:我尝试过什么。

# showkey
Couldn't get a file descriptor referring to the console

# evtest
No device specified, trying to scan all of /dev/input/event*
USAGE:
 Capture mode:
   evtest [--grab] /dev/input/eventX
     --grab  grab the device for exclusive access

 Query mode: (check exit code)
   evtest --query /dev/input/eventX <type> <value>

<type> is one of: EV_KEY, EV_SW, EV_LED, EV_SND
<value> can either be a numerical value, or the textual name of the
key/switch/LED/sound being queried (e.g. SW_DOCK).


# ls /dev/input
ls: cannot access /dev/input: No such file or directory


# stty -a
speed 38400 baud; rows 300; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc ixany imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke

基本上,我需要这样的东西:

termbox 键盘演示

更新3:控制台客户端和服务器之间的通信是通过 Go 中的 websockets 完成的。来源 -客户服务器

答案1

结果显示stty -a很有希望,但是问题和其他答案都没有指出通常的方法来查找通过特殊键(如DelLeft-cursor等)发送的内容。

不清楚原帖中提到的“Del”指的是哪个。我的键盘上有DeleteDel键(在编辑键盘和数字键盘上)。两者都可以发送以(ASCII 转义字符)开头的一系列数据字节ESC。也就是说,在常规终端中。

然而,问题提到网络套接字,听起来好像是在网络浏览器中运行。屏幕截图显示了某种屏幕键盘,其中包含DEL编辑小键盘。

如果实现完成,那么当紧迫该键,如果你先按controlV

^[[3~

^[回显的 ASCII 转义字符 ( control[)。您需要controlV 下一步字符以防止 shell 解释它或将其丢弃。

(字面意思为下一个)设置lnext是任何特米奥斯您可能会遇到这种情况,但奇怪的是,POSIX 标准中没有提及,除非将其保留用于扩展(请参阅提及VLNEXT)。但是,由于这是针对 Linux 标记的,因此您可以参考 Linux 文档:

  • termios(3)

    termios 函数描述了用于控制异步通信端口的通用终端接口。

  • stty(1)

    打印或者改变终端特性。

您可以在输出中看到它stty -a,即本例中的第四行输出:

$ stty -a
speed 38400 baud; rows 40; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke

答案2

我认为你需要退一步思考一下你正在尝试做什么。你不清楚如何通过 WebSockets 连接到服务器。我假设你正在连接到代理连接到 telnet 端口的 WebSocket 服务器,对吗(因为 WebSockets 只能连接到 WebSockets)?你可能想看看Websockify项目包含一个示例 VT100 兼容的 telnet 客户端。您可以以此为基础开展您的项目。

相关内容