Hi Antonio,

At 2026-07-04T22:04:38+0000, Antonio Niño Díaz wrote:
> I've done a few more tests. If I assign single characters to
> ku/kd/kr/kl, like "ku=A", it works as expected (when I press A I get a
> KEY_UP event). It's just multi-character strings that fail. However, I
> have noticed that if I make it "ku=AA" it will hang slightly after I
> press the first A (and then I just receive an A key press event
> instead of KEY_UP because it hasn't seen a second A). So I think that
> there may be some issue with some wait loop in ncurses...

Your example established that you're using keypad mode.  Have you read
the getch(3) man page?  What you are observing sounds exactly like what
is documented there.

   Keypad Mode
     Call keypad(3NCURSES) on a window to configure keypad mode when
     reading input from it.  In keypad mode, curses treats key strokes
     not from the alphabetic section of the keyboard (those
     corresponding to the ECMA‐6 character set —— see ascii(7) ——
     optionally modified by either the control or shift keys) as
     function keys.  (In curses, the term “function key” includes but is
     not limited to keycaps engraved with “F1”, “PF1”, and so on.)  If a
     window is in keypad mode, wgetch translates these key strokes to a
     numeric code corresponding to the KEY_ symbols listed in subsection
     “Key Codes” below.  If the window is not in keypad mode, the input
     queue populates with the characters of the function key’s escape
     sequence, which the application must collect individually with
     multiple wgetch calls.

     •   The curses.h header file declares many function keys whose
         names begin with KEY_; these object‐like macros have integer
         values outside the range of eight‐bit character codes.

     •   In ncurses, user‐defined function keys are configured with
         define_key(3NCURSES); they have no names, but are also expected
         to have integer values outside the range of eight‐bit character
         codes.

     A variable intended to hold a function key code must thus be of
     type short or larger.

     Most terminals one encounters follow the ECMA‐48 standard insofar
     as their function keys produce character sequences prefixed with
     the escape character ESC.  This fact implies that curses cannot
     distinguish a user’s press of the escape key (assuming it sends
     ESC) from the beginning of a function key’s character sequence
     without waiting to see if, and how soon, further input arrives.

     •   If the escape sequence matches a string capability defining a
         function key for the terminal type (such as key_home (khome) or
         key_up (kuu1)), wgetch returns the function key code
         corresponding to the unique sequence defined by the terminal.

     •   If the escape sequence matches no function keys defined for the
         terminal type, call wgetch repeatedly to obtain the codes of
         the individual characters of the sequence, in the order they
         occurred in the input.

     •   If wgetch cannot decide the validity of the input as a function
         key because it has not read enough characters to disambiguate
         it, the function waits until it has this information or the
         escape delay elapses.  Configure the escape delay with the
         global variable ESCDELAY, an extension (see section
         “EXTENSIONS” below), or the environment variable of the same
         name (see section “ENVIRONMENT” of ncurses(3NCURSES)), also an
         extension.

     Consequently, a user of a curses application that employs keypad
     mode may experience a pause or “hang” after pressing the escape key
     while curses collects sufficient characters to disambiguate the
     input.  If the window is in “no time‐out” mode, the escape delay is
     effectively infinite; see notimeout(3NCURSES).  In the event of
     such a pause, further typing “awakens” curses.

Regards,
Branden

Attachment: signature.asc
Description: PGP signature

Reply via email to