Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish between Escape and Escape Sequence

My end goal is to distinguish between my pressing Esc (ASCII 27) on my keyboard, and me pressing the key on my keyboard (which translates to a sequence of 27 91 67). I am using termios to put my terminal into non-Canonical mode.

I think I understand there are two options:

  • Wait some arbitrary amount of time to see if something comes in (seems hacky)
  • Check STDIN to see if it is empty

I'm trying to do the latter. To that end, I'm trying to use select to see if stdin is empty or not.

The Problem

select always seems to return 0 (timeout expires). That seems odd for two reasons:

  1. I figured that if I didn't type anything after hitting Esc, then it would return -1 since it doesn't see anything left in stdin to read
  2. I figured that if I typed , then I would get a 1 returned since it sees that right after 27 there is a 91 and a 67 to read

Neither of those things happens, so I'm afraid I just don't understand select or standard in/out like I thought I did.

The Question(s)

Why doesn't select return anything except 0 in my example? Is it possible to check if stdin is empty? How do other libraries handle this?

Minimal, Complete, and Verifiable Example

I am running this on both MacOS High Sierra and Ubuntu 16 with equal results.

Source:

#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

int main() {
        // put terminal into non-canonical mode
        struct termios old;
        struct termios new;
        int fd = 0;  // stdin
        tcgetattr(fd, &old);
        memcpy(&new, &old, sizeof(old));
        new.c_lflag &= ~(ICANON | ECHO);
        tcsetattr(fd, TCSANOW, &new);

        // loop: get keypress and display (exit via 'x')
        char key;
        printf("Enter a key to see the ASCII value; press x to exit.\n");
        while (1) {
                key = getchar();

                // check if ESC
                if (key == 27) {
                        fd_set set;
                        struct timeval timeout;
                        FD_ZERO(&set);
                        FD_SET(STDIN_FILENO, &set);
                        timeout.tv_sec = 0;
                        timeout.tv_usec = 0;
                        int selret = select(1, &set, NULL, NULL, &timeout);
                        printf("selret=%i\n", selret);
                        if (selret == 1) {
                                // input available
                                printf("possible sequence\n");
                        } else if (selret == -1) {
                                // error
                                printf("err=%s\n", strerror(errno));
                        } else {
                                // just esc key
                                printf("esc key standalone\n");
                        }
                }

                printf("%i\n", (int)key);
                if (key == 'x') { break; }
        }

        // set terminal back to canonical
        tcsetattr(fd, TCSANOW, &old);
        return 0;
}

Output

gns-mac1:sandbox gns$ ./seltest 
Enter a key to see the ASCII value; press x to exit.
selret=0
esc key standalone
27
selret=0
esc key standalone
27
91
67
120
like image 633
Greg Schmit Avatar asked Dec 31 '17 03:12

Greg Schmit


People also ask

What is the meaning of '\ n escape sequence?

For example, \n is an escape sequence that denotes a newline character.

What are two escape sequences examples?

Escape sequences allow you to send nongraphic control characters to a display device. For example, the ESC character (\033) is often used as the first character of a control command for a terminal or printer. Some escape sequences are device-specific.

What does an escape sequence start with?

In C and many derivative programming languages, a string escape sequence is a series of two or more characters, starting with a backslash \ .

Which symbol is used for escape sequence?

An escape sequence contains a backslash (\) symbol followed by one of the escape sequence characters or an octal or hexadecimal number. A hexadecimal escape sequence contains an x followed by one or more hexadecimal digits (0-9, A-F, a-f). An octal escape sequence uses up to three octal digits (0-7).


1 Answers

I think the problem is that you're using getchar() — a function from the standard I/O library — where you need to use file descriptor I/O (read()).

Simple example

Here's a straight-forward adaptation of your code (tested on a MacBook Pro running macOS High Sierra 10.13.2), that produces the answer you and I want.

#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

enum { ESC_KEY = 27 };
enum { EOF_KEY = 4  };

int main(void)
{
    // put terminal into non-canonical mode
    struct termios old;
    struct termios new;
    int fd = 0;      // stdin
    tcgetattr(fd, &old);
    //memcpy(&new, &old, sizeof(old));
    new = old;
    new.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(fd, TCSANOW, &new);

    // loop: get keypress and display (exit via 'x')
    //int key;
    printf("Enter a key to see the ASCII value; press x to exit.\n");
    while (1)
    {
        char key;
        if (read(STDIN_FILENO, &key, 1) != 1)
        {
            fprintf(stderr, "read error or EOF\n");
            break;
        }
        if (key == EOF_KEY)
        {
            fprintf(stderr, "%d (control-D or EOF)\n", key);
            break;
        }

        // check if ESC
        if (key == 27)
        {
            fd_set set;
            struct timeval timeout;
            FD_ZERO(&set);
            FD_SET(STDIN_FILENO, &set);
            timeout.tv_sec = 0;
            timeout.tv_usec = 0;
            int selret = select(1, &set, NULL, NULL, &timeout);
            printf("selret=%i\n", selret);
            if (selret == 1)
                printf("Got ESC: possible sequence\n");
            else if (selret == -1)
                printf("error %d: %s\n", errno, strerror(errno));
            else
                printf("esc key standalone\n");
        }
        else 
            printf("%i\n", (int)key);
        if (key == 'x')
            break;
    }

    // set terminal back to canonical
    tcsetattr(fd, TCSANOW, &old);
    return 0;
}

Sample output (program esc29):

$ ./esc29   # 27 isn't a 2-digit prime
Enter a key to see the ASCII value; press x to exit.
115
100
97
115
100
selret=1
Got ESC: possible sequence
91
68
selret=1
Got ESC: possible sequence
91
67
selret=0
esc key standalone
selret=0
esc key standalone
selret=0
esc key standalone
100
100
4 (control-D or EOF)
$

I pressed the left/right arrow keys and got 'possible sequence' reported; I pressed the ESC on the touch strip and got 'ESC key standalone'. Other characters seem plausible, the code was rigged to break when control-D is pressed.

Complex example

This code reads up to 4 characters at a time, and processes those that are received. There are two nested loops, so I use a goto end_loops; (twice!) to break out of the both loops from the inner loop. I also use the atexit() function to do most of what can be done to ensure that the terminal attributes are reset to the sane state even if the program doesn't exit via the main() program. (We can debate whether the code should also use the at_quick_exit() function — it's a feature of C11 but not of POSIX.)

If the code reads multiple characters, it scans through them, looking for ESC (escape). If it finds one and there is any data left, then it reports the escape sequence (presumably a function key sequence). If it doesn't find any more characters, it uses select() as before to decide whether there are more characters in an ESC sequence or if this is a standalone ESC. In practice, the computer is hugely faster than a mere human, so it either reads a single character or the complete sequence. I use an array of length 4 since I think that's longer than the longest key sequence generated from the keyboard; I'd be happy to increase it to 8 (or any other larger number). The only downside to this is that the buffer must be available where characters need to be read in the unlikely event that several characters are read (e.g. because the program was computing while input was accumulating). There's also a chance that the ESC from a function key or arrow key will be the last character that fits in the buffer — in which case, extra reading is necessary. Good luck in demonstrating that with this program as written — you're not a fast enough typist. You'd need to add sleep code somewhere to allow characters to accumulate before it reads them.

So, this mainly shows a couple of extra techniques, but it could be useful as an alternative way of thinking about the processing.

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>

enum { ESC_KEY = 27 };
enum { EOF_KEY = 4  };

/* These two need to be set in main() but accessed from reset_tty() */
static int fd = STDIN_FILENO;
static struct termios old;

// set terminal back to canonical
static void reset_tty(void)
{
    tcsetattr(fd, TCSANOW, &old);
}

int main(void)
{
    struct termios new;
    tcgetattr(fd, &old);
    new = old;
    new.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(fd, TCSANOW, &new);
    atexit(reset_tty);      // Ensure the terminal is reset whenever possible

    printf("Enter a key to see the ASCII value; press x to exit.\n");
    char keys[4];
    int nbytes;
    while ((nbytes = read(fd, keys, sizeof(keys))) > 0)
    {
        for (int i = 0; i < nbytes; i++)
        {
            char key = keys[i];
            if (key == EOF_KEY)
            {
                fprintf(stderr, "%d (control-D or EOF)\n", key);
                goto end_loops;
            }
            else if (key == ESC_KEY && nbytes > i + 1)
            {
                printf("Got ESC sequence:");
                for (int j = i; j < nbytes; j++)
                    printf("%4d", keys[j]);
                putchar('\n');
                break;
            }
            else if (key == ESC_KEY)
            {
                fd_set set;
                struct timeval timeout;
                FD_ZERO(&set);
                FD_SET(fd, &set);
                timeout.tv_sec = 0;
                timeout.tv_usec = 0;
                int selret = select(1, &set, NULL, NULL, &timeout);
                printf("selret=%i\n", selret);
                if (selret == 1)
                    printf("Got ESC: possible sequence\n");
                else if (selret == -1)
                    printf("error %d: %s\n", errno, strerror(errno));
                else
                    printf("esc key standalone\n");
            }
            else 
                printf("%i\n", (int)key);
            if (key == 'x')
                goto end_loops;
        }
    }

end_loops:
    return 0;
}

Sample output (program esc67):

$ ./esc67
Enter a key to see the ASCII value; press x to exit.
65
90
97
122
selret=0
esc key standalone
Got ESC sequence:  27  91  65
Got ESC sequence:  27  91  66
Got ESC sequence:  27  91  67
Got ESC sequence:  27  91  68
Got ESC sequence:  27  79  80
selret=0
esc key standalone
97
Got ESC sequence:  27  91  67
97
Got ESC sequence:  27  91  67
120
$
like image 62
Jonathan Leffler Avatar answered Sep 28 '22 18:09

Jonathan Leffler