RE: Ncurses keypad is not properly working in FreeBSD

"Brad Hines" <[email protected]> Mon, 23 Jan 2023 23:07:19 -0800
Newsgroups gmane.comp.lib.ncurses.general,gmane.comp.lib.ncurses.bugs
Organization Planet A Energy
Message-ID <[email protected]>
This is a multipart message in MIME format.

------=_NextPart_000_0F28_01D92F7F.7245CF30
Content-Type: text/plain;
	charset="utf-8"
Content-Transfer-Encoding: quoted-printable

I=E2=80=99m not sure if this is related to your question or not, but =
perhaps it will help.

=20

I had some issues with certain keycodes, and I recall that =
there=E2=80=99s something funny about xterm.  It may be that xterm is =
the source of your problems.  A quick and dirty thing to try would be to =
try a different terminal type.

=20

=20

I made a note in my code that I found this online that had some info, =
might be of help to you:

=20

https://stackoverflow.com/questions/16975367/ncurses-reading-numpad-keys-=
and-escaping

=20

That is solving a slightly different set of keys than I was worried =
about.  I am using CDK which is built on top of ncurses, but I ended up =
extending the already-defined key set with my own customer keys to =
define ALT-A to ALT-Z and then ALT-a to ALT-z.

=20

=20

=20

=20

=20

From: [email protected] =
<[email protected]> On Behalf Of =
Archimedes Gaviola
Sent: Monday, January 23, 2023 8:09 PM
To: [email protected]; [email protected]
Subject: Ncurses keypad is not properly working in FreeBSD

=20

Hi,

=20

I have a C program that accepts and displays numeric characters in 0-9 =
only and with alphabet keys, function keys are expected not to respond =
to any code from the keyboard using keypad(). I simulated a plain =
Ncurses C program without a window and another one with a window using =
the newwin() function. The program without window works getting no =
response from alphabet keys as well as function keys, however the =
Enter/return key (ch =3D=3D KEY_ENTER) is not working instead I use (ch =
=3D=3D '\n') to get it to work. The backspace key (ch =3D=3D =
KEY_BACKSPACE) is working. For the program with the window, I expect it =
to behave similar to the plain program but the function keys such F5 to =
F12 are showing codes on the screen as well as Insert, Delete, Page Up =
and Page Down keys are also showing codes. Only F1, F2, F3 and F4 are =
behaving expectedly. The same thing happens to the Enter/return and =
backspace keys I use (ch =3D=3D '\n') and  (ch =3D=3D '\b') to get them =
working.

=20

I need your help in this case if there's something I missed in the =
Ncurses initialization parameters or maybe a sort of environment =
variable configuration problem with my FreeBSD OS. My FreeBSD system is =
using xterm.

=20

pangi@siquijor:~ # echo $TERM
xterm

=20

FreeBSD version: FreeBSD 14.0-CURRENT (arm64)

=20

Ncurses version:

#define NCURSES_VERSION_MAJOR 6
#define NCURSES_VERSION_MINOR 2
#define NCURSES_VERSION_PATCH 20210220

=20

1. Plain C program (without window)

=20

#include <ncurses.h>

int main(void)
{
        initscr();
        keypad(stdscr, TRUE);
        noecho();

        int i;

        for (i=3D1; i<=3D14; i++)
        {
                int ch =3D getch();
                if ((ch =3D=3D '1') || (ch =3D=3D '2') || (ch =3D=3D =
'3') \
                     || (ch =3D=3D '4') || (ch =3D=3D '5') || (ch =3D=3D =
'6') \
                     || (ch =3D=3D '7') || (ch =3D=3D '8') || (ch =3D=3D =
'9') \
                     || (ch =3D=3D '0'))
                {
                        addch(ch);
                        refresh();
                } else {
                        --i;
                }

                //if (ch =3D=3D KEY_ENTER) (not working)
                if (ch =3D=3D '\n')
                {
                        printw("Return key is pressed.\n");
                }

                if (ch =3D=3D KEY_BACKSPACE)
                {
                        printw("Backspace key is pressed.\n");
                }

        }
        endwin();
        return 0;
}

=20

2. Plain C derived program (with window)

=20

#include <ncurses.h>

int main(void)
{
        WINDOW *alpha;

        initscr();
        //keypad(stdscr, TRUE); (the same outcome with keypad(alpha, =
TRUE) below)
        keypad(alpha, TRUE);
        noecho();
        start_color();
        init_pair(1,COLOR_WHITE,COLOR_BLUE);

        alpha =3D newwin(0,0,0,0);
        if( alpha =3D=3D NULL)
        {
                endwin();
                puts("Problem creating window");
                return(1);
        }

        wbkgd(alpha,COLOR_PAIR(1));
        wrefresh(alpha);

        int i;

        for (i=3D1; i<=3D14; i++)
        {
                int ch =3D wgetch(alpha);
                if ((ch =3D=3D '1') || (ch =3D=3D '2') || (ch =3D=3D =
'3') \
                     || (ch =3D=3D '4') || (ch =3D=3D '5') || (ch =3D=3D =
'6') \
                     || (ch =3D=3D '7') || (ch =3D=3D '8') || (ch =3D=3D =
'9') \
                     || (ch =3D=3D '0'))
                {
                        waddch(alpha,ch);
                        wrefresh(alpha);
                } else {
                        --i;
                }

                //if (ch =3D=3D KEY_ENTER) (not working)
                if (ch =3D=3D '\n')
                {
                        wprintw(alpha,"Return key is pressed.\n");
                }

                //if (ch =3D=3D KEY_BACKSPACE) (not working)
                if (ch =3D=3D '\b')
                {
                        wprintw(alpha,"Backspace key is pressed.\n");
                }

        }
        endwin();
        return 0;
}

=20

Thanks and best regards,

Archimedes

=20


------=_NextPart_000_0F28_01D92F7F.7245CF30
Content-Type: text/html;
	charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
xmlns=3D"http://www.w3.org/TR/REC-html40"><head><meta =
http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8"><meta =
name=3DGenerator content=3D"Microsoft Word 15 (filtered =
medium)"><style><!--
/* Font Definitions */
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:#0563C1;
	text-decoration:underline;}
span.EmailStyle18
	{mso-style-type:personal-reply;
	font-family:"Calibri",sans-serif;
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri",sans-serif;}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]--></head><body lang=3DEN-US =
link=3D"#0563C1" vlink=3D"#954F72" style=3D'word-wrap:break-word'><div =
class=3DWordSection1><p class=3DMsoNormal>I=E2=80=99m not sure if this =
is related to your question or not, but perhaps it will =
help.<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal>I had some issues with certain keycodes, and I recall =
that there=E2=80=99s something funny about xterm. =C2=A0It may be that =
xterm is the source of your problems.=C2=A0 A quick and dirty thing to =
try would be to try a different terminal type.<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>I made a =
note in my code that I found this online that had some info, might be of =
help to you:<o:p></o:p></p><p class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal><a =
href=3D"https://stackoverflow.com/questions/16975367/ncurses-reading-nump=
ad-keys-and-escaping">https://stackoverflow.com/questions/16975367/ncurse=
s-reading-numpad-keys-and-escaping</a><o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p class=3DMsoNormal>That is =
solving a slightly different set of keys than I was worried about.=C2=A0 =
I am using CDK which is built on top of ncurses, but I ended up =
extending the already-defined key set with my own customer keys to =
define ALT-A to ALT-Z and then ALT-a to ALT-z.<o:p></o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><div =
style=3D'border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in =
0in 0in'><p class=3DMsoNormal><b>From:</b> =
[email protected] =
&lt;[email protected]&gt; <b>On =
Behalf Of </b>Archimedes Gaviola<br><b>Sent:</b> Monday, January 23, =
2023 8:09 PM<br><b>To:</b> [email protected]; =
[email protected]<br><b>Subject:</b> Ncurses keypad is not properly =
working in FreeBSD<o:p></o:p></p></div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p><div><div><p =
class=3DMsoNormal>Hi,<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p class=3DMsoNormal>I =
have a C program that accepts and displays numeric characters in 0-9 =
only and with alphabet keys, function keys are expected not to respond =
to any code from the keyboard using keypad(). I simulated a plain =
Ncurses C program without a window and another one with a window using =
the newwin() function. The program without window works getting no =
response from alphabet keys as well as function keys, however the =
Enter/return key (ch =3D=3D KEY_ENTER) is not working instead I use (ch =
=3D=3D '\n') to get it to work. The backspace key (ch =3D=3D =
KEY_BACKSPACE) is working. For the program with the window, I expect it =
to behave similar to the plain program but the function keys such F5 to =
F12 are showing codes on the screen as well as Insert, Delete, Page Up =
and Page Down keys are also showing codes. Only F1, F2, F3 and F4 are =
behaving expectedly. The same thing happens to the Enter/return and =
backspace keys I use (ch =3D=3D '\n') and&nbsp; (ch =3D=3D '\b') to get =
them working.<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p class=3DMsoNormal>I =
need your help in this case if there's something I missed in the Ncurses =
initialization parameters or maybe a sort of environment variable =
configuration problem with my FreeBSD OS. My FreeBSD system is using =
xterm.<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p =
class=3DMsoNormal>pangi@siquijor:~ # echo =
$TERM<br>xterm<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p =
class=3DMsoNormal>FreeBSD version: FreeBSD 14.0-CURRENT =
(arm64)<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p =
class=3DMsoNormal>Ncurses version:<o:p></o:p></p></div><div><p =
class=3DMsoNormal>#define NCURSES_VERSION_MAJOR 6<br>#define =
NCURSES_VERSION_MINOR 2<br>#define NCURSES_VERSION_PATCH =
20210220<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p =
class=3DMsoNormal>1. Plain C program (without =
window)<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p =
class=3DMsoNormal>#include &lt;ncurses.h&gt;<br><br>int =
main(void)<br>{<br>&nbsp; &nbsp; &nbsp; &nbsp; initscr();<br>&nbsp; =
&nbsp; &nbsp; &nbsp; keypad(stdscr, TRUE);<br>&nbsp; &nbsp; &nbsp; =
&nbsp; noecho();<br><br>&nbsp; &nbsp; &nbsp; &nbsp; int i;<br><br>&nbsp; =
&nbsp; &nbsp; &nbsp; for (i=3D1; i&lt;=3D14; i++)<br>&nbsp; &nbsp; =
&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; int ch =3D getch();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; if ((ch =3D=3D '1') || (ch =3D=3D '2') || (ch =3D=3D '3') =
\<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;|| (ch =3D=3D '4') || (ch =3D=3D '5') || (ch =3D=3D '6') =
\<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;|| (ch =3D=3D '7') || (ch =3D=3D '8') || (ch =3D=3D '9') =
\<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;|| (ch =3D=3D '0'))<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addch(ch);<br>&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
refresh();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } =
else {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; --i;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; //if (ch =3D=3D KEY_ENTER) (not working)<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ch =3D=3D '\n')<br>&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
printw(&quot;Return key is pressed.\n&quot;);<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; if (ch =3D=3D KEY_BACKSPACE)<br>&nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
printw(&quot;Backspace key is pressed.\n&quot;);<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; &nbsp; &nbsp; =
}<br>&nbsp; &nbsp; &nbsp; &nbsp; endwin();<br>&nbsp; &nbsp; &nbsp; =
&nbsp; return 0;<br>}<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p =
class=3DMsoNormal>2. Plain C derived program (with =
window)<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p =
class=3DMsoNormal>#include &lt;ncurses.h&gt;<br><br>int =
main(void)<br>{<br>&nbsp; &nbsp; &nbsp; &nbsp; WINDOW =
*alpha;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; initscr();<br>&nbsp; &nbsp; =
&nbsp; &nbsp; //keypad(stdscr, TRUE); (the same outcome with =
keypad(alpha, TRUE) below)<br>&nbsp; &nbsp; &nbsp; &nbsp; keypad(alpha, =
TRUE);<br>&nbsp; &nbsp; &nbsp; &nbsp; noecho();<br>&nbsp; &nbsp; &nbsp; =
&nbsp; start_color();<br>&nbsp; &nbsp; &nbsp; &nbsp; =
init_pair(1,COLOR_WHITE,COLOR_BLUE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; =
alpha =3D newwin(0,0,0,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; if( alpha =
=3D=3D NULL)<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endwin();<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; puts(&quot;Problem creating =
window&quot;);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; return(1);<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; =
&nbsp; &nbsp; wbkgd(alpha,COLOR_PAIR(1));<br>&nbsp; &nbsp; &nbsp; &nbsp; =
wrefresh(alpha);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; int i;<br><br>&nbsp; =
&nbsp; &nbsp; &nbsp; for (i=3D1; i&lt;=3D14; i++)<br>&nbsp; &nbsp; =
&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; int ch =3D wgetch(alpha);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; if ((ch =3D=3D '1') || (ch =3D=3D '2') || (ch =
=3D=3D '3') \<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp;|| (ch =3D=3D '4') || (ch =3D=3D '5') || (ch =3D=3D =
'6') \<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;|| (ch =3D=3D '7') || (ch =3D=3D '8') || (ch =3D=3D '9') =
\<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp;|| (ch =3D=3D '0'))<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; waddch(alpha,ch);<br>&nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; wrefresh(alpha);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; } else {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --i;<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; //if (ch =3D=3D KEY_ENTER) (not =
working)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if =
(ch =3D=3D '\n')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; wprintw(alpha,&quot;Return key is =
pressed.\n&quot;);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; }<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
//if (ch =3D=3D KEY_BACKSPACE) (not working)<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ch =3D=3D '\b')<br>&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
wprintw(alpha,&quot;Backspace key is pressed.\n&quot;);<br>&nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; &nbsp; =
&nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; endwin();<br>&nbsp; &nbsp; =
&nbsp; &nbsp; return 0;<br>}<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div><div><p =
class=3DMsoNormal>Thanks and best regards,<o:p></o:p></p></div><div><p =
class=3DMsoNormal>Archimedes<o:p></o:p></p></div><div><p =
class=3DMsoNormal><o:p>&nbsp;</o:p></p></div></div></div></body></html>
------=_NextPart_000_0F28_01D92F7F.7245CF30--