17

I have a Fedora machine that I can SSH to. One of the programs I'd like to use occasionally uses the function keys. The problem is that I'm SSH'ing from an Android tablet (ASUS Transformer Infinity) with a physical keyboard, but no F1-F12 keys. So, until the terminal app I'm using (VX ConnectBot) decides to add them as a feature, I'm looking for a way to send them using the rest of the keyboard.

I can use all printable ASCII characters, Esc, Ctrl, Shift, Enter, and Tab.

1
  • I think you'd be better off asking on Android.SE. – derobert Nov 1 '12 at 21:34
27

Terminals only understand characters, not keys. So al function keys are encoded as sequences of characters, using control characters. Apart from a few common ones that have an associated control character (Tab is Ctrl+I, Enter is Ctrl+M, Esc is Ctrl+[), function keys send escape sequences, beginning with Ctrl+[ [ or Ctrl+[ O. You can use the tput command to see what escape sequence applications expect for each function key on your terminal. These sequences are stored in the terminfo database. For example, the shell snippet below shows the escape sequences corresponding to each function key.

$ for x in {1..12}; do echo -n "F$x "; tput kf$x | cat -A; echo; done
F1 ^[OP
F2 ^[OQ
F3 ^[OR
F4 ^[OS
F5 ^[[15~
F6 ^[[17~
F7 ^[[18~
F8 ^[[19~
F9 ^[[20~
F10 ^[[21~
F11 ^[[23~
F12 ^[[24~

Another way to see the escape sequence for a function key is to press Ctrl+V in a terminal application that doesn't rebind the Ctrl+V key (such as the shell). Ctrl+V inserts the next character (which will be the escape character) literally, and you'll be able to see the rest of the sequence, which consists of ordinary characters.

Since the sequences may be awkward to type, do investigate changing the key bindings in your application or using another terminal emulator. Also, note that you may have a time limit: some applications only recognize escape sequences if they come in fast enough, so that they can give a meaning to the Esc key alone.

2
  • How do I add shifted function keys to the list your command provides? – drevicko Jun 19 '19 at 4:29
  • 1
    @drevicko On xterm, Shift+F1 is kf13, Shift+F2 is kf14, etc. Then Ctrl+F1 is kf25, Ctrl+Shift+F1 is kf37, Alt+F1 is kf49. I don't know if this works on other terminals nor what you get if you have an actual F13 key. – Gilles 'SO- stop being evil' Jun 19 '19 at 6:48
4

Android Terminal Emulator by Jack Palevich uses key combinations with the volume keys to send a variety of keys, including function keys. There is also Hacker's Keyboard which has every key available that a real keyboard would have.

1
  • Thanks, the Hacker Keyboard should cover what I need for now. – Sizik Nov 2 '12 at 19:02
0

As Drake Clarris suggests, on Android, you can install alternate keyboards, such as Hacker's Keyboard that include F-keys.

However, if for whatever reason you can't install a new keyboard, there are other alternatives, as long as your terminal app allows you to press Esc and/or Alt.

You can often use Esc followed by Number, or Alt+Number to simulate F1-F10, using 0 for F10.

These shortcuts are very helpful when using a terminal emulator on a phone or other restricted device that lacks physical F-keys. They are also useful with a physical keyboard when your terminal program or another program running in the same session (eg byobu/screen/tmux) intercepts F-keys for its own purposes.

  • Example with Esc: I want to press F10 to quit mc, the Midnight Commander, but Gnome Terminal intercepts F10 and interprets it as its menu key. Instead, I press Esc, then 0. Gnome Terminal passes the inputs separately, mc interprets them as F10, and mc exits.

  • Example with Alt: I want to press F4 to filter htop, but byobu intercepts F4 and interprets it as "next window". Instead, I hold Alt and press 4. Byobu passes the key combination through, htop interprets it as F4, and enables filtering.

The Alt method may not work if an application interprets Alt+Number as a distinct key combination from F-Number. I have not encountered this problem in practice.

I don't know if there's a way to type F11 or F12 this way, as I've never needed to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.