Solved Key mapping on console

Hi.
Who knows how to map following keypresses to escape sequences on FreeBSD console?

Code:
Meta-Up:      \033[1;3A
Meta-Down: \033[1;3B
Meta-Left: \033[1;3D
Meta-Right: \033[1;3C
Ctrl-Up: \033[1;5A
Ctrl-Down: \033[1;5B
Ctrl-Left: \033[1;5D
Ctrl-Right: \033[1;5C

I've tried to use kbdcontrol without any success.
 
VT/SC do not support arrow keys + strg/shift sequences. This has been an invention from Gnome. They only work in Xorg in FreeBSD. I spend a lot of time making editors/jed copy/paste functionality work on VT, but gave up as even tweaking the keymaps accordingly does not work. Those combos are always registered as arrow key only.
 
VT/SC do not support arrow keys + strg/shift sequences. This has been an invention from Gnome. They only work in Xorg in FreeBSD. I spend a lot of time making editors/jed copy/paste functionality work on VT, but gave up as even tweaking the keymaps accordingly does not work. Those combos are always registered as arrow key only.
I get the same with editors/micro. Then I will stop messing with the termcap, teken code, console tuning, etc. Thank you K.Jacker.
 
I managed to fix the key mapping problem this way
  • You need to patch the sources:
Diff:
diff -ruN src.orig/share/vt/keymaps/ru.kbd src/share/vt/keymaps/ru.kbd
--- src.orig/share/vt/keymaps/ru.kbd    2019-08-29 03:01:37.000000000 +0300
+++ src/share/vt/keymaps/ru.kbd 2019-09-09 13:15:46.575636000 +0300
@@ -98,12 +98,12 @@
   092   nscr   pscr   debug  debug  nop    nop    nop    nop     O
   093   ralt   ralt   ralt   ralt   ralt   ralt   ralt   ralt    O
   094   fkey49 fkey49 fkey49 fkey49 fkey49 fkey49 fkey49 fkey49  O
-  095   fkey50 fkey50 fkey50 fkey50 fkey50 fkey50 fkey50 fkey50  O
+  095   fkey50 fkey73 fkey69 fkey77 fkey65 fkey81 fkey85 fkey50  O
   096   fkey51 fkey51 fkey51 fkey51 fkey51 fkey51 fkey51 fkey51  O
-  097   fkey53 fkey53 fkey53 fkey53 fkey53 fkey53 fkey53 fkey53  O
-  098   fkey55 fkey55 fkey55 fkey55 fkey55 fkey55 fkey55 fkey55  O
+  097   fkey53 fkey76 fkey72 fkey80 fkey68 fkey84 fkey88 fkey53  O
+  098   fkey55 fkey75 fkey71 fkey79 fkey67 fkey83 fkey87 fkey55  O
   099   fkey57 fkey57 fkey57 fkey57 fkey57 fkey57 fkey57 fkey57  O
-  100   fkey58 fkey58 fkey58 fkey58 fkey58 fkey58 fkey58 fkey58  O
+  100   fkey58 fkey74 fkey70 fkey78 fkey66 fkey82 fkey86 fkey58  O
   101   fkey59 fkey59 fkey59 fkey59 fkey59 fkey59 fkey59 fkey59  O
   102   fkey60 paste  fkey60 fkey60 fkey60 fkey60 fkey60 fkey60  O
   103   fkey61 fkey61 fkey61 fkey61 fkey61 fkey61 boot   fkey61  O
diff -ruN src.orig/sys/dev/vt/vt_core.c src/sys/dev/vt/vt_core.c
--- src.orig/sys/dev/vt/vt_core.c       2019-08-29 03:01:18.000000000 +0300
+++ src/sys/dev/vt/vt_core.c    2019-09-09 12:28:07.310546000 +0300
@@ -860,6 +860,27 @@
                        return (0);
                }

+               int fkey = c - (FKEY | F(1));
+               /* checking if function key strings are set (see kbdcontrol(1) manual)*/
+               if (fkey >=0 && fkey < 97) {
+                       size_t len;
+                       fkey += F_FN;
+                       u_char *raw_str = genkbd_get_fkeystr(kbd, fkey, &len);
+                       if (raw_str) {
+                               /*
+                                 since our strings here are of type u_char *,
+                                 normal strncpy() would not work here.
+                               */
+                               u_char fkey_str[MAXFK];
+                               for (int i = 0 ; i < len; i++) {
+                                       fkey_str[i] = raw_str[i];
+                               }
+                               fkey_str[len] = '\0';
+                               terminal_handle_fkey(vw->vw_terminal, fkey_str);
+                               return (0);
+                       }
+               }
+
                switch (c) {
                case NEXT:
                        /* Switch to next VT. */
diff -ruN src.orig/sys/kern/subr_terminal.c src/sys/kern/subr_terminal.c
--- src.orig/sys/kern/subr_terminal.c   2019-08-29 03:01:15.000000000 +0300
+++ src/sys/kern/subr_terminal.c        2019-09-09 12:28:07.312371000 +0300
@@ -348,6 +348,23 @@
}

void
+terminal_handle_fkey(struct terminal *tm, u_char *fkey_str) {
+       struct tty *tp;
+
+       tp = tm->tm_tty;
+       if (tp == NULL)
+               return;
+
+       if (fkey_str == NULL)
+               return;
+
+       tty_lock(tp);
+       ttydisc_rint_simple(tp, fkey_str, strlen(fkey_str));
+       ttydisc_rint_done(tp);
+       tty_unlock(tp);
+}
+
+void
terminal_input_special(struct terminal *tm, unsigned int k)
{
        struct tty *tp;
diff -ruN src.orig/sys/sys/terminal.h src/sys/sys/terminal.h
--- src.orig/sys/sys/terminal.h 2019-08-29 03:01:16.000000000 +0300
+++ src/sys/sys/terminal.h      2019-09-09 12:28:07.314047000 +0300
@@ -220,6 +220,7 @@
void   terminal_mute(struct terminal *tm, int yes);
void   terminal_input_char(struct terminal *tm, term_char_t c);
void   terminal_input_raw(struct terminal *tm, char c);
+void   terminal_handle_fkey(struct terminal *tm, u_char *fkey_str);
void   terminal_input_special(struct terminal *tm, unsigned int k);

void   termcn_cnregister(struct terminal *tm);
diff -ruN src.orig/usr.sbin/kbdcontrol/kbdcontrol.c src/usr.sbin/kbdcontrol/kbdcontrol.c
--- src.orig/usr.sbin/kbdcontrol/kbdcontrol.c   2019-08-29 03:02:35.000000000 +0300
+++ src/usr.sbin/kbdcontrol/kbdcontrol.c        2019-09-09 12:35:23.250130000 +0300
@@ -97,12 +97,12 @@
/* 53-56 */    "\033[D", "\033[E", "\033[C", "+"     ,
/* 57-60 */    "\033[F", "\033[B", "\033[G", "\033[L",
/* 61-64 */     "\177",   "\033[J", "\033[~", "\033[}",
-/* 65-68 */    ""      , ""      , ""      , ""      ,
-/* 69-72 */    ""      , ""      , ""      , ""      ,
-/* 73-76 */    ""      , ""      , ""      , ""      ,
-/* 77-80 */    ""      , ""      , ""      , ""      ,
-/* 81-84 */    ""      , ""      , ""      , ""      ,
-/* 85-88 */    ""      , ""      , ""      , ""      ,
+/* 65-68 */    "\033[1;3A", "\033[1;3B", "\033[1;3C", "\033[1;3D",
+/* 69-72 */    "\033[1;5A", "\033[1;5B", "\033[1;5C", "\033[1;5D",
+/* 73-76 */    "\033[1;2A", "\033[1;2B", "\033[1;2C", "\033[1;2D",
+/* 77-80 */    "\033[1;6A", "\033[1;6B", "\033[1;6C", "\033[1;6D",
+/* 81-84 */    "\033[1;4A", "\033[1;4B", "\033[1;4C", "\033[1;4D",
+/* 85-88 */    "\033[1;7A", "\033[1;7B", "\033[1;7C", "\033[1;7D",
/* 89-92 */    ""      , ""      , ""      , ""      ,
/* 93-96 */    ""      , ""      , ""      , ""      ,
        };

  • I patch ru.kbd file, so add that keyboard layout to /etc/rc.conf: doas sysrc keymap="ru"
  • Edit file /usr/src/libexec/rc/rc.d/syscons: sed -E -i '' -e '/errmsg=/s/kbdcontrol/& -F/' /usr/src/libexec/rc/rc.d/syscons
  • And rebuild the system
  • In order to enable PgUp and PgDown to work in editors/micro on console, I've added following key bindings to ~/.config/micro/bindings.json:
Code:
{
        "\u001b[I": "CursorPageUp",
        "\u001b[G": "CursorPageDown"
}
 
Last edited:
For x11/rxvt-unicode to work properly, I've added following code to ~/.Xresources:

Bash:
URxvt.keysym.M-Up:      \033[1;3A
URxvt.keysym.M-Down:    \033[1;3B
URxvt.keysym.M-Left:    \033[1;3D
URxvt.keysym.M-Right:   \033[1;3C
URxvt.keysym.C-Up:      \033[1;5A
URxvt.keysym.C-Down:    \033[1;5B
URxvt.keysym.C-Left:    \033[1;5D
URxvt.keysym.C-Right:   \033[1;5C
URxvt.keysym.S-Up:      \033[1;2A
URxvt.keysym.S-Down:    \033[1;2B
URxvt.keysym.S-Left:    \033[1;2D
URxvt.keysym.S-Right:   \033[1;2C
URxvt.keysym.C-S-Up:    \033[1;6A
URxvt.keysym.C-S-Down:  \033[1;6B
URxvt.keysym.C-S-Left:  \033[1;6D
URxvt.keysym.C-S-Right: \033[1;6C
URxvt.keysym.S-M-Up:    \033[1;4A
URxvt.keysym.S-M-Down:  \033[1;4B
URxvt.keysym.S-M-Left:  \033[1;4D
URxvt.keysym.S-M-Right: \033[1;4C
URxvt.keysym.C-M-Up:    \033[1;7A
URxvt.keysym.C-M-Down:  \033[1;7B
URxvt.keysym.C-M-Left:  \033[1;7D
URxvt.keysym.C-M-Right: \033[1;7C
 
VT/SC do not support arrow keys + strg/shift sequences. This has been an invention from Gnome. They only work in Xorg in FreeBSD. I spend a lot of time making editors/jed copy/paste functionality work on VT, but gave up as even tweaking the keymaps accordingly does not work. Those combos are always registered as arrow key only.
Do you know if there was a good reason to not implement these
sequences in vt? I had a hard time to get an idea how all these
keyboard terminal subsystems work. I have the impression, that this
knowlege from the past is being buried by current developments. When
did Gnome "invent" this key combination? Emacs is much older than Gnome.
 
Back
Top