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>.
SAMPLE PROGRAM OUTPUT
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char key_press;
int ascii_value;
cout<<"\t\t\tPress Button on the keyboard\n\n\n";
while(1)
{
key_press=getch();
ascii_value=key_press;
cout<<"\t\t\tKEY Pressed-> is "<<key_press<<"<<key_press Ascii Value = "<<ascii_value<<endl;
if(ascii_value == 77)
{
cout<<"\t\t\tRight arrow key"<<endl;
}
else if(ascii_value == 75)
{
cout<<"\t\t\tLeft Arrow key"<<endl;
}
else if(ascii_value>=65 && ascii_value<=90)
{
cout<<"\t\t\tcapital letter"<<endl;
}
else if(ascii_value>=97 && ascii_value<=122)
{
cout<<"\t\t\tLowercase letter"<<endl;
}
else if(ascii_value>=48 && ascii_value<=57)
{
cout<<"\t\t\tDigit"<<endl;
}
else if(ascii_value==27) // For ESC
{
cout<<"\t\t\tESC key"<<endl;
}
else if(ascii_value==13)
{
cout<<"\t\t\tEnter key"<<endl;
}
else if(ascii_value == 359)
{
cout<<" F1 function key"<<endl;
}
else
{
cout<<"\t\t\tAnother (unrecognised) key"<<endl;
}
}
return 0;
}
Comments
Leave a comment