Write a program that check and classify buttons pressed on the keyboard.
After pressing any button, the program
should display one of labels: - lowercase letter
- capital letter
- digit
- ENTER
- ESC
- Left Arrow
- Right arrow
- F1 function key
- Another (unrecognised) key
HINT: to retrieve the keyboard key code, use function getch() from the library <conio.h>.
#include <conio.h>
#include <iostream>
#define KEY_LEFT  75
#define KEY_RIGHT 77
#define DIGIT     48-57
using namespace std;
int main()
{
    int c, ex;
    while(1)
    {
        c = getch();
        if (c>=48 && c<=57)
        {
            cout << endl << "Digit :" << (char) c << endl;
        }
        else if (c>=65 && c<=90)
        {
          cout << endl << "Capital letter :" << (char) c << endl;
        }
         else if (c>=97 && c<=122)
        {
          cout << endl << "lowercase letter :" << (char) c << endl;
        }
            else if (c>=27 && c<=27)
        {
          cout << endl << "ESC :" << (char) c << endl;
        }
        else
        {
            switch(ex = getch())
            {
                case KEY_LEFT   /* M */:
                    cout << endl << "Left Arrow" << endl;  // key left
                    break;
                case KEY_RIGHT: /* P */
                    cout << endl << "Right Arrow" << endl;  // key right
                    break;
                default:
                    cout << endl << (char) ex << endl;  // not arrow
                    break;
            }
        }
    }
    return 0;
}
Comments