问题描述
我首先在 Windows 上用 Ansi C 编写了一个小程序,现在我在带有内置 GCC 的 Ubuntu 上编译它.
I've written a tiny program in Ansi C on Windows first, and I compiled it on Ubuntu with the built-in GCC now.
程序很简单:
- 使用
scanf()
从控制台读取该行. - 分析字符串并计算.
但是发生了一些奇怪的事情.当我尝试移动光标时,它会打印四个字符:
But something weird happens. When I try to move the cursor, it prints four characters:
- 按 打印
^[[A
" - 按 打印
^[[B
" - 按 打印
^[[C
" - 按 打印
^[[D
"
- pressing prints "
^[[A
" - pressing prints "
^[[B
" - pressing prints "
^[[C
" - pressing prints "
^[[D
"
如何避免这种情况?
How can this be avoided?
为什么要打印这 4 个字符而不是移动光标?
Why does it print these 4 characters instead of moving the cursor?
推荐答案
因为那是键盘实际发送到 PC 的内容(更准确地说,终端打印的内容与它实际从键盘接收的内容).例如 bash
获取这些值,解密它们并理解您想要四处移动,因此它将移动光标(在左/右的情况下)或使用其历史来获取以前的命令(向上/下).所以你不能指望你的程序神奇地支持箭头键.
Because that's what the keyboard actually sends to the PC (more precisely, what the terminal prints for what it actually receives from the keyboard). bash
for example gets those values, deciphers them and understands that you want to move around, so it will either move the cursor (in case of left/right) or use its history to fetch previous commands (up/down). So you can't expect your program to magically support arrow keys.
然而,从终端的标准输入读取已经支持左/右箭头键(我相信,但我现在不在 Linux 中进行测试和确定).所以我的猜测是还有另一个问题在干扰.一种可能的原因可能是您的一个修改键被卡住了?也许是 ALT、CTRL 或 SUPER?
However, reading from standard input from the terminal already supports left/right arrow keys (I believe, but I'm not in Linux right now to test and make sure). So my guess is that there is another issue interfering. One possible cause could be that one of your modifier keys is stuck? Perhaps ALT, CTRL or SUPER?
这篇关于为什么终端显示“^[[A"?"^[[B""^[[C"“^[[[D"]在 Ubuntu 中按箭头键时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!