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?
-
How did you get that code ?123– 1232016-02-16 10:25:10 +00:00Commented 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?japan– japan2016-02-16 11:09:35 +00:00Commented Feb 16, 2016 at 11:09
-
You can look at my answer here for a way to get the key codes in hex.123– 1232016-02-16 11:39:48 +00:00Commented Feb 16, 2016 at 11:39
1 Answer
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:
\x1bis the ASCII escape code (ESC)\x5brefers to the character[. Used right after the escape code (soESC[), 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\x00parameter, it must be a sequence.\x32and\x7etogether are the control sequence, which basically is translated to2~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:
1767E~DECKEYSSent 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.