5

i wrote a piece of code to detect arrow keys using _getch(); and i wish to detect the esc key as well but i dont actually know what are the numbers that i should use so any help is appreciated.

#include <conio.h>
#include <stdio.h>
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77

int c = 0;
_getch();
switch ((c = _getch())) {
case KEY_UP:
    printf("Up\n");
    break;
case KEY_DOWN:
    printf("Down\n");
    break;
case KEY_LEFT:
    printf("Left\n");
    break;
case KEY_RIGHT:
    printf("Right\n");
    break;
default:
    printf("Null\n");
    break;

each of the arrow keys is a combination of two characters of ascii code 224 and the defined ones (notice the first _getch();) but i dont know that for the escape key, i tried searching but didnt find them, a full list of those would be so helpful.

Thanks.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
xixhxix
  • 153
  • 1
  • 1
  • 12

4 Answers4

8

Up,Down,Left,Right is known as extended key and to detect them you have to read Two Char first one is Null and the second is the ASCII code but ESC is not extended key so you can detect it with only one char.

I hope that code will help you:

#include <stdio.h>
#include <stdlib.h>

#define esc 27

int main()
{
    char ch;
    do
    {
        ch = getch();
        switch(ch)
        {
            case esc:
                // your logic goes here
                break;
        }
    }
    while(exitflag != 1);
}
Ahmed Lotfy
  • 1,065
  • 2
  • 15
  • 33
6

ESC is 27 in ASCII.

So, you want #define ESC 27 and check the value returned by _getch.

Here's an ASCII table

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • so there is an ascii code for the esc key but not for the arrows? – xixhxix Dec 10 '15 at 19:21
  • @xixhxix, The escape key was used for generating the [escape character](https://en.wikipedia.org/wiki/Escape_character#ASCII_escape_character). The code is for the character rather than the key. – chris Dec 10 '15 at 19:29
  • @xixhxix : ASCII originates from the days of Teletype terminals where your opportunities for non-printing cursor positioning were limited to space, tab, carriage-return, line-feed, form-feed and vertical-tab (basically you could only go forward or to the beginning of the current line) - arrow keys had no purpose. The escape character was used to issue "escape sequences" - sequences of characters preceded by the escape character. On a video terminal (or "glass teletype") escape sequences were used to position the cursor amongst other functions. These are still supported in terminal emulators – Clifford Dec 10 '15 at 20:09
3

I think you are confusing the Esc key with the general term "escape sequence", in which a particular value is used to indicate that values which follow do not have their usual meaning - and often the ESC value 27 is used to do this, but not in this case.

When you press the Arrow (and other non-printing) keys, _getch() returns two values from consective calls. The first value is 0 or 224 which acts as an "escape" character to indicate that the next call will inform of the actual key pressed.

The code you posted does not detect the Arrow keys, it detects the keys 'H', 'P', 'K' and 'M', although I suspect that the first _getch() was intended to read the escape value.

When I run the following program and press the keys 1, Up-arrow, Esc

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int k;
    do {
        k = _getch();
        if (k == 0 || k == 224) {   // Escape sequence?
            printf("Special key: ");
            k = _getch();           // read again
        }
        printf("%d\n", k);          // print numeric key value
    } while (k != 27);              // end when Esc pressed
    
    return 0;
}

I get the following output:

49
Special key: 72
27

Conclusion: I recommend you detect 0 and then have a nested switch statement to figure out which non-printing key was pressed.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
2

A general solution to this problem would be to just print out the value c is set to. That way any key you press you can get the value for.

default:
   printf("Key pressed has value = %d\n",c);
   break;
arduic
  • 659
  • 3
  • 12