C语言 为什么在Ubuntu中按方向键时终端显示“^[[A”“^[[B”“^[[C”“^[[D”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21384040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:41:26  来源:igfitidea点击:

Why does the terminal show "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu?

cubuntu

提问by Kevin Dong

I've written a tiny program in Ansi C on Windows first, and I compiled it on Ubuntu with the built-in GCC now.

我首先在 Windows 上用 Ansi C 编写了一个小程序,现在我在带有内置 GCC 的 Ubuntu 上编译它。

The program is simple:

程序很简单:

  • read the line from console with scanf().
  • Analyze the string and calculate.
  • 使用scanf().从控制台读取该行。
  • 分析字符串并计算。

But something weird happens. When I try to move the cursor, it prints four characters:

但奇怪的事情发生了。当我尝试移动光标时,它会打印四个字符:

  • pressing Upprints "^[[A"
  • pressing Dnprints "^[[B"
  • pressing Rtprints "^[[C"
  • pressing Ltprints "^[[D"
  • Up印刷品“ ^[[A
  • Dn印刷品“ ^[[B
  • Rt印刷品“ ^[[C
  • Lt印刷品“ ^[[D

  • How can this be avoided?

  • Why does it print these 4 characters instead of moving the cursor?

  • 如何避免这种情况?

  • 为什么它打印这 4 个字符而不是移动光标?

采纳答案by Shahbaz

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). bashfor 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.

因为那是键盘实际发送到 PC 的内容(更准确地说,终端打印的内容是它实际从键盘接收的内容)。bash例如获取这些值,解密它们并了解您想要四处移动,因此它将移动光标(在向左/向右的情况下)或使用其历史记录来获取以前的命令(向上/向下)。所以你不能指望你的程序神奇地支持箭头键。

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?

但是,从终端的标准输入读取已经支持左/右箭头键(我相信,但我现在不在 Linux 中进行测试和确定)。所以我的猜测是还有另一个问题在干扰。一种可能的原因可能是您的一个修饰键卡住了?也许是 ALT、CTRL 或 SUPER?

回答by KhaledMohamedP

For those who are coming from the osx (mac) try changing the shells to bash

对于那些来自 osx (mac) 的人,请尝试将 shell 更改为 bash

Terminal -> Preferences -> Shells open with -> [select] Command (complete path)

Terminal -> Preferences -> Shells open with -> [select] Command (complete path)

then paste

然后粘贴

/bin/bash

/bin/bash

回答by Sumith08

This might be because the user account is created in shell. You can change it to bash by two ways.

这可能是因为用户帐户是在 shell 中创建的。您可以通过两种方式将其更改为 bash。

Permament solution is -

永久解决方案是 -

sudo chsh -s /bin/bash ${username}

To get this solution working you will have to logout and login

要使此解决方案起作用,您必须注销并登录

Temporary solution is everytime when you login into the ubuntu server type bashand hit return.

临时解决方案是每次登录 ubuntu 服务器类型bash并按回车键。

回答by Milan Parmar

i think simple way is we can just do kill %%because this sometimes happen because of background processes.

我认为简单的方法是我们可以这样做, kill %%因为这有时是由于后台进程而发生的。