2

In the following code INS=\x1b\x5b\x32\x7e the hex value corresponds to Insert key.
Is it any list of hex values corresponding to key-press exists?

3
  • How did you get that code ? Commented Feb 16, 2016 at 10:25
  • @123 This is from "Advanced Bash Scripting" guide. Example 5-3. The code is successfully understands key-presses. That is what I'm wondering, where does this values come from? Commented Feb 16, 2016 at 11:09
  • You can look at my answer here for a way to get the key codes in hex. Commented Feb 16, 2016 at 11:39

1 Answer 1

1

Bash use ANSI-C quoting, so first notice that \x is not part of the key code, but used to tell that the following characters are an hexadecimal 1/2 digits code.

Let's look at \x1b\x5b\x32\x7e as example:

  • \x1b is the ASCII escape code (ESC)

  • \x5b refers to the character [. Used right after the escape code (so ESC[), it form with it a Control Sequence Introducer (CSI). A CSI tells to the terminal to interpret the next parameters as part of a sequence.

    This is used in the special cases of (not comprehensive):
    INS, DEL, , End, PgUp, PgDn and F6 to F12.

    Why we need to use a CSI on those special keys? Because those keys use a 4 to 6 digits hexadecimal code - so it can't be interpreted with a single \x00 parameter, it must be a sequence.

  • \x32 and \x7e together are the control sequence, which basically is translated to 2~ which refer to the INS command. In the ANSI standard for ASCII terminals, the keys listed before are identified by a combination of a decimal code and the ~ (\x7e) character. In a CSI sequence, the action is defined by the last character - in this case ~ which correspond to the "special keys action".

Let's get an other example to prove this: 17~ means F6 , and is translated to \x31\x37\x7e using the ASCII table:

HEX  Chr
---  ---
31   1
37   7
7E   ~

So, that looks good - we have \x1b\x5b\x31\x37\x7e when adding the CSI, which is the valid code in bash for F6 - see this gist that list most of the special keys code.

But how to know that F6 should be translated to 17~? By looking at the ANSI code table, you will find the following:

176 7E ~ DECKEYS Sent by special function keys

[1~=FIND, [2~=INSERT, [3~=REMOVE, [4~=SELECT, [5~=PREV, [6~=NEXT [11~=F1… [17~=F6…[34~=F20 ([23~=ESC,[24~=BS,[25~=LF,[28~=HELP,[29~=DO)

This isn't perfect, but from this you can establish the following table (this is theoretical, probably need some validation):

+-----+------+-------------+-----------+------------+-------------------+
| Key | Code |     Hex     |    Key    |    Code    |        Hex        |
+-----+------+-------------+-----------+------------+-------------------+
| F6  | [17~ | 5B 31 37 7E | END       | [4~ OR [OF | 5B 34 7E OR 4F 46 |
| F7  | [18~ | 5B 31 38 7E | Home (⇱)  | [1~ OR [OH | 5B 31 7E OR 4F 48 |
| F8  | [19~ | 5B 31 39 7E | Page down | [6~        | 5B 36 7E          |
| F9  | [20~ | 5B 32 30 7E | Page up   | [5~        | 5B 35 7E          |
| F10 | [21~ | 5B 32 31 7E | Up        | [!         | 5B 41             |
| F11 | [23~ | 5B 32 33 7E | Down      | ["         | 5B 42             |
| F12 | [24~ | 5B 32 34 7E | Right     | [#         | 5B 43             |
| INS | [2~  | 5B 32 7E    | Left      | [$         | 5B 44             |
| DEL | [3~  | 5B 33 7E    |           |            |                   |
+-----+------+-------------+-----------+------------+-------------------+

Which should be enough for most of your uses.

Please note that this answer is an attempt to explain the ANSI quoting for keys in bash, it might not be accurate because of the lack of documentation about this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.