TSE for Linux: I thought I found the fix for the keyboard problem

22 views
Skip to first unread message

S.E. Mitchell

unread,
Sep 9, 2023, 5:42:00 AM9/9/23
to TSEPro Support
Under Linux, certain keys are not recognized - for instance, there is
not a way to tell the difference between <tab> and <ctrl i>, <ctrl
enter> and <ctrl j>, <enter> and <ctrl m>, <ctrl n> and <ctrl />, and
so on.

Well, I thought I found the magic sauce!
https://invisible-island.net/xterm/modified-keys.html

However, I cannot get it to work.
As noted in the doc referenced, on program startup I issue the sequence:
fputs("\033[>4;2m", stdout);

But I still can not distinguish between the keys listed.

Oh well :(
--
Sammy Mitchell

knud van eeden

unread,
Sep 9, 2023, 7:15:38 AM9/9/23
to sem...@googlegroups.com

Key Recognition in Linux

GPT-4
User
Under Linux, certain keys are not recognized - for instance, there is not a way to tell the difference between <tab> and <ctrl i>, <ctrl enter> and <ctrl j>, <enter> and <ctrl m>, <ctrl n> and <ctrl />, and so on. How to create a computer program to check the difference?
ChatGPT

Recognizing the difference between certain key combinations like <tab> and <ctrl+i> in a Linux terminal can be tricky. This is because, historically, these key combinations are often mapped to the same ASCII control codes.

If you want to distinguish between these keys in a program, you can use libraries that provide lower-level access to the input, like ncurses for C/C++ or similar libraries in other languages. These libraries can sometimes discern the difference between key combinations by capturing the raw keycodes.

Here is a simple example using C and ncurses:

c
#include <ncurses.h> int main() { initscr(); raw(); // Disable line buffering keypad(stdscr, TRUE); // Enable special keys noecho(); // Don't echo the characters printw("Press any key to see its code or 'q' to quit\n"); int ch; while(1) { ch = getch(); if(ch == 'q') break; // Clear line and print keycode move(1, 0); clrtoeol(); printw("Keycode: %d", ch); refresh(); } endwin(); return 0; }

bash
gcc your_file.c -lncurses

Run the compiled program in the terminal and press keys to see if it can distinguish them.

Note: This may not work for all key combinations due to terminal limitations and configurations.


with friendly greetings

Knud van Eeden

knud van eeden

unread,
Sep 9, 2023, 7:24:04 AM9/9/23
to sem...@googlegroups.com
> Under Linux, certain keys are not recognized - for instance, there is
> not a way to tell the difference between <tab> and <ctrl i>, <ctrl
> enter> and <ctrl j>, <enter> and <ctrl m>, <ctrl n> and <ctrl />, and
> so on.

> How to create a computer program to check the difference?

ChatGPT

Recognizing the difference between certain key combinations like <tab>
and <ctrl+i> in a Linux terminal can be tricky. This is because,
historically, these key combinations are often mapped to the same ASCII
control codes.

If you want to distinguish between these keys in a program, you can use
libraries that provide lower-level access to the input, like ncurses
for C/C++ or similar libraries in other languages. These libraries can
sometimes discern the difference between key combinations by capturing
the raw keycodes.

Here is a simple example using C and ncurses:

--- cut here: begin --------------------------------------------------

#include <ncurses.h>

int main() {
    initscr();
    raw(); // Disable line buffering
    keypad(stdscr, TRUE); // Enable special keys
    noecho(); // Don't echo the characters

    printw("Press any key to see its code or 'q' to quit\n");

    int ch;
    while(1) {
        ch = getch();
        if(ch == 'q') break;

        // Clear line and print keycode
        move(1, 0);
        clrtoeol();
        printw("Keycode: %d", ch);
        refresh();
    }

    endwin();
    return 0;
}

--- cut here: end ----------------------------------------------------

===

To compile:

--- cut here: begin --------------------------------------------------

gcc your_file.c -lncurses

--- cut here: end ----------------------------------------------------

knud van eeden

unread,
Sep 9, 2023, 7:31:06 AM9/9/23
to sem...@googlegroups.com
> Under Linux, certain keys are not recognized - for instance, there is
> not a way to tell the difference between <tab> and <ctrl i>, <ctrl
> enter> and <ctrl j>, <enter> and <ctrl m>, <ctrl n> and <ctrl />, and
> so on.

> I am using the Watcom C compiler.

> How to create a computer program to check the difference?

===

ChatGPT

If you are using the Watcom C compiler on Linux and you want to
distinguish between certain key sequences, it gets tricky due to
terminal limitations. Many terminals map sequences like <tab> and
<ctrl+i> to the same ASCII control codes. Libraries like ncurses that I
mentioned earlier often use the same terminal capabilities, so they
inherit these limitations.

However, if you are working on a specific application and need to
distinguish these keys, there is a workaround you could consider: you
could set the terminal into "raw" mode and attempt to read the escape
sequences directly. You would not be able to use standard C libraries
like <stdio.h> for this, as they buffer input.

Here is an example using low-level UNIX system calls. I do not have a
Watcom-specific example, but you should be able to adapt this logic:

--- cut here: begin --------------------------------------------------

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

void set_raw_mode() {
    struct termios term;
    tcgetattr(STDIN_FILENO, &term);
    term.c_lflag &= ~(ICANON | ECHO); // Disable canonical mode and echo
    tcsetattr(STDIN_FILENO, TCSANOW, &term);
}

int main() {
    set_raw_mode();

    char c;
    while (read(STDIN_FILENO, &c, 1) == 1) {
        if (c == 3) { // Ctrl-C to exit
            break;
        }
        printf("Got char: %d\n", c);
    }

    // Restore terminal settings here if needed

    return 0;
}

--- cut here: end ----------------------------------------------------

===

Compile the code using gcc or adapt it for Watcom.

Please note that:

This is a very simplified example and does not cover all the nuances of
terminal programming.

The key codes you will see may still not distinguish between the
sequences you are interested in, due to the aforementioned terminal
limitations.

You should usually restore the terminal settings upon exit, which this
example does not cover for simplicity.

If Watcom C provides specific methods for this or if you are running
this in a specialized environment (outside a typical terminal), you
might have more options.

knud van eeden

unread,
Sep 9, 2023, 8:05:13 AM9/9/23
to sem...@googlegroups.com, S.E. Mitchell
> Sammy wrote:
> Under Linux, certain keys are not recognized - for instance, there is
> not a way to tell the difference between <tab> and <ctrl i>, <ctrl
> enter> and <ctrl j>, <enter> and <ctrl m>, <ctrl n> and <ctrl />, and
> so on.

Possible resolutions:

1. Because ncurses library seems to have the source code for getting the raw code, it might be worth
   checking if this assumed little bit of source code can be extracted and added directly into 
   your Watcom compiler source code and use it directly.

2. Maybe one of the solutions ChatGPT, Claude2, Bard, Perplexity might help. Though this is not guaranteed.

3. I asked already in the ReddIt user group if they could get ncurses to run with Watcom. This because ncurses pops
   up in many solutions so being able to use that might move things fast forward.

4. You could also yourself ask further questions e.g. on ReddIt regarding implementing solutions in Watcom for TSE for Linux as
   you are able to do this more specifically.


The Open Watcom community is relatively small, with moderate activity on Reddit.

 The primary discussions are done on the "contributors" and "users.c_cpp" groups on the Open Watcom newsgroups

a third option is the user forums on sourceforge.

5. Other solutions.

S.E. Mitchell

unread,
Sep 9, 2023, 8:14:02 AM9/9/23
to knud van eeden, sem...@googlegroups.com
Been there, done that :)

--
Sammy Mitchell

S.E. Mitchell

unread,
Sep 9, 2023, 8:21:54 AM9/9/23
to knud van eeden, sem...@googlegroups.com
Amplification:

Currently using raw mode, none of the keys I listed in the original
message can be distinguished.
I've also tried it with ncurses, and with the same result.

Did you try any of these examples?
I compiled them, ran them, and saw no difference.
--
Sammy Mitchell

S.E. Mitchell

unread,
Sep 9, 2023, 9:45:38 AM9/9/23
to knud van eeden, sem...@googlegroups.com
And for reference, here is the code I'm using to test with:

/* compile with: gcc -o keypress keypress.c */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

struct termios orig_termios;

void disableRawMode() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);

fputs("\033>", stdout); // restore alt keypad mode
fputs("\033[>4;0m", stdout); // restore CIS u style key reporting
fflush(stdout);
}

void enableRawMode() {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(disableRawMode);

struct termios raw = orig_termios;

raw.c_cflag &= ~(CSIZE | PARENB);
raw.c_cflag |= CS8;
raw.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
ICRNL | IXON);
raw.c_oflag &= ~OPOST;
raw.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}

int main() {
enableRawMode();

fputs("\033=", stdout); // alternate keypad mode
fputs("\033[>4;2m", stdout); // request CSI u style key reporting
fflush(stdout);

char c;
while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
if (iscntrl(c)) {
printf("%d\r\n", c);
} else {
printf("%d ('%c')\r\n", c, c);
}
}

return 0;
}

Can someone else try this, to see if they notice any difference between
<tab> and <ctrl i>, <ctrl enter> and <ctrl j>, <enter> and <ctrl m>,
<ctrl n> and <ctrl />?

Guy Rouillier

unread,
Sep 9, 2023, 11:07:11 AM9/9/23
to Semware TSE Pro
I'm not an expert on this by any means.  I found this StackExchange article on the topic:


It links to several resources with a deeper dive on the topic.  In brief, it's a mess, and the terminal program you are using has a say in key mappings.  Given the large number of Linux terminal programs, I don't see a general solution.  On a more positive note, apparently most terminal programs agree on a small number of these characters (such as ctrl-i and tab being equivalent.)  According to this page, GUI programs have an easier task, since they rely on key press messages, which are more standardized.

--
Guy Rouillier
--
 
---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.

S.E. Mitchell

unread,
Sep 9, 2023, 11:31:38 AM9/9/23
to sem...@googlegroups.com
Thanks for the reference!
Based on that, I found
https://unix.stackexchange.com/questions/631241/mapping-otherwise-conflicting-or-unmappable-keys-in-terminal-vim
and based on that, I added:

XTerm*Translations: #override\
Ctrl ~Meta Shift <Key>x :string("\033[88;5u")

To my ~/Xresources file.

I shutdown WSL, then restarted it, but no love :(

I guess it is a lost cause.
--
Sammy Mitchell
> To view this discussion on the web visit https://groups.google.com/d/msgid/semware/em2d732ea4-3889-4f80-9f8c-22332a2f4a8b%40d2a397b8.com.

knud van eeden

unread,
Feb 28, 2024, 7:17:52 AMFeb 28
to sem...@googlegroups.com, Tse-linux List
Hello Sammy,

I checked just now my question at Reddit about

"How to run the ncurses C library using the Watcom C compiler on Linux?"

and there was actually an answer:

From: erysdren
6 months ago

"PDCurses is compatible with Watcom C"

See PDCurses at:

with friendly greetings
Knud van Eeden
--

---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.

Claus Futtrup

unread,
Feb 28, 2024, 2:06:44 PMFeb 28
to sem...@googlegroups.com
Hi Knud

Interesting!

Cheers,
Claus
Reply all
Reply to author
Forward
0 new messages