我如何找出转义序列我的终端需要发送? [英] How do I find out what escape sequence my terminal needs to send?

查看:235
本文介绍了我如何找出转义序列我的终端需要发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用在OSX终端应用程序连接到Ubuntu(12.04,我认为)。我有终端在它的xerm-256的默认设置。在这种模式下,终端发送\\ 033OQ的序列时F2为pressed。然而有转变为-F2或控制-F2映射。也没有我需要这些键,从时间的不同程序操作的特点,以时间顺序为左移位设置,右移,控制左,控制右等。就拿屏风。按住Shift键F2和控制-F2用于垂直和水平分割画面。在TMUX箭头键用来与移动和控制调整窗格。

我的问题是,我不知道终端应当送什么顺序。我如何找出这些序列应该是什么?我知道我可以preSS控制-V,然后一键搞定正在接收的code,但这不是我所需要的。我并不需要看到正在发送的序列。我需要知道我要送什么顺序。

例如我的左箭头发送^ [OD。我控制左发送^ [5D]。但是我的上下键都没有区别。我的下发^ [OB和控制,倒也发送^ [OB。同样的问题与我的键。它是具有或不具有移相同。

所有的按键是否有名单的地方,并有codeS,特别是与改性剂如换档,控制,ALT /选项?

我怎样才能找到预期的顺序是什么,所以我可以给需要什么?


解决方案

的XTerm

既然你做广告,你正在使用的的xterm 的(通过您的长期价值),你很可能要安排发送的xterm 的会送的序列这些键。从 ctlseqs 文件的的xterm 的描述了底部这些特殊改性的键序列式​​功能键部分:


  

...的xterm识别哪些是控制序列的最后一个字符前附加的参数,功能键修改器。


  
  

2移
    3 Alt键
    4 Shift + Alt键
    5控制
    6控制+ Shift键
    ⋮


示例

<大骨节病> F5 发送 ^ [[15〜,所以<大骨节病>移 - <大骨节病> F5 应发送 ^ [[15; 2〜(即添加 2 前的最后一个字符)

箭头键和前四个功能键是有点不同。他们经常使用基于SS3序列(从 ^ [0 );这些需要被改变为基于CSI-当量(从 ^ [[),因为SS3序列不能有参数。另外,对于按键的正常序列通常不会有一个数字参数,所以一个占位符也被添加 1 参数(?):

<大骨节病>最多发送 ^ [[A ^ [OA ,所以<大骨节病>移 + <大骨节病>最多应该送 ^ [[1; 2A (即切换到CSI,并添加 1; 2 最后一个字符前)

<大骨节病> F1 发送 ^ [OP ,所以<大骨节病>移 + <大骨节病> F1 应该送 ^ [[1; 2P (即切换到CSI添加 1; 2 最后一个字符前)

您可能还喜欢看各种终端仿真程序的源代码code看到whay他们这样做。例如, TMUX 的相关位是在其的 的xterm-keys.c 文件

配置

由于您的终端仿真器是不是已经将所有要支持序列,你必须配置它这样做。自带的OS X,的终端的内置终端模拟器,有几个键preconfigured,但你可能要增加他们的最自己。您可以通过调用在终端得到的密钥列表> preferences ... 菜单项,选择顶级设置标签,选择适当的配置文件,并切换到它的键盘标签。从那里,你可以添加和删除键定义。例如,你可以定义 F2 发送字符串外壳:(原文如此),然后键入<大骨节病> ESC 后跟 [1; 2Q 到字符串文本框(在<大骨节病> ESC 将显示为 \\ 033 ,这是正常的)。

注:更改定义<大骨节病>选项 - <大骨节病>右键和<大骨节病>选项 - <大骨节病>左可能会影响某些程序(如贝壳和Emacs)与击键工作。默认配置发送ESC-f和ESC-B,这是Emacs风格的按键向后字和正向词,许多程序在默认情况下理解。这种配置向后在我看来,外壳(和其他程序)应配置为识别正确的修改方向键序列来代替。

您可能也想尝试不同的终端模拟器。 的iTerm 的2 有内置的支持,大多数修饰键(也许不是控制+ FN变种),所以会少些手动配置。

另外请注意,OS X具有一定的<大骨节病>控制中定义了一些全系统的快捷键 - <大骨节病> Fx的组合(见的系统preferences 键盘 键盘快捷键 键盘和放大器;文本输入)。你将不得不关闭一些快捷方式,以确保的终端的iTerm 的有机会看到您想要的组合键。

I am using the terminal application on osX to connect to ubuntu (12.04 i think). I have the terminal at it's default setting of xerm-256. In this mode the terminal sends the sequence of \033OQ when F2 is pressed. There is no mapping however for Shift-F2 or Control-F2. There is also no sequences set for Shift-Left, Shift-Right, Control-Left, Control-Right, etc. I need some of these keys to operate features from time to time in various programs. Take for example byobu. Shift-F2 and Control-F2 are used to split the screen vertically and horizontally. In tmux the arrow keys are used with shift and control for resizing panes.

My problem is that I don't know what sequence the terminal should send. How do I find out what these sequences should be? I know that I can press Control-v and then a key to get the code that is being received but this is not what I need. I don't need to see what sequence is being sent. I need to know what sequence I need to send.

For instance my left arrow sends ^[OD. My Control-Left sends ^[[5D]. However my up and down keys have no distinction. My down sends ^[OB and Control-down also sends ^[OB. Same problem with my up key. It is the same with or without shift.

Is there a list somewhere with all of the keys and there codes, specifically with modifiers such as shift, control, alt/option?

How can I find out what the expected sequence is so I can send what is needed?

解决方案

XTerm

Since you are advertising that you are using an xterm (via your TERM value), you will most likely want to arrange to send the sequences that xterm would send for these keys. The ctlseqs documentation from xterm describes these particular modified key sequences at the bottom of the PC-Style Function Keys section:

… xterm recognizes function key modifiers which are parameters appended before the final character of the control sequence.

    2 Shift
    3 Alt
    4 Shift + Alt
    5 Control
    6 Control + Shift
    ⋮

Examples

F5 sends ^[[15~, so Shift-F5 should send ^[[15;2~ (i.e. add ;2 before the final character)

The arrow keys and the first four function keys are a bit different. They often use SS3-based sequences (starting with ^[O); these will need to be changed to CSI-based equivalents (starting with ^[[) since SS3 sequences can not have parameters. Also, the normal sequences for the keys do not usually have a numeric parameter, so a placeholder(?) 1 parameter is also added:

Up sends ^[[A or ^[OA, so Shift+Up should send ^[[1;2A (i.e. switch to CSI, and add 1;2 before the final character)

F1 sends ^[OP, so Shift+F1 should send ^[[1;2P (i.e. switch to CSI, add 1;2 before the final character)

You might also like to look at the source code of various terminal emulators to see whay they do. For example, the relevant bits of tmux are in its xterm-keys.c file.

Configuration

Since your terminal emulator is not already sending all the sequences you want to support, you will have to configure it to do so. The built-in terminal emulator that comes with OS X, Terminal, has a few keys preconfigured, but you will probably have to add most of them yourself. You can get to the list of keys by invoking the Terminal > Preferences… menu item, selecting the top-level Settings tab, picking the appropriate profile, and switching to its Keyboard tab. From there you can add and remove key definitions. For example, you could define F2 with shift to send string to shell: (sic) and type ESC followed by [1;2Q into the string text box (the ESC will show up as \033, this is okay).

Note: Changing the definition of Option-Right and Option-Left might affect how some programs (e.g. shells and Emacs) work with those keystrokes. The default configuration sends ESC-f and ESC-b, which are Emacs-style keystrokes for backward-word and forward-word that many programs will understand by default. This configuration seems backwards to me; the shell (and other programs) should be configured to recognize proper modified arrow key sequences instead.

You might also want to try a different terminal emulator. iTerm 2 has built-in support for most of these modified keys (maybe not the Control+Fn variants), so there would be less to manually configure.

Also, note that OS X has some system-wide shortcuts defined for some of the Control-Fx combinations (see System Preferences, Keyboard, Keyboard Shortcuts, Keyboard & Text Input). You will have to disable some of these shortcuts to make sure that Terminal or iTerm has a chance to "see" your desired key combinations.

这篇关于我如何找出转义序列我的终端需要发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆