Detect keypress and print ASCII value C++

Here we will write a program to Detect keypress and print ASCII value C++.

We will use a infinite while loop until user press ESC(escape button on keyboard), user will enter a key and the program will provide the ASCII value of the input.

WAP to Detect keypress and print ASCII value C++:

PROGRAM:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char key_press;
int ascii_value;
cout<<"\nPress Any Key To Check Its ASCII Value \nPress ESC to EXIT\n";
while(1)
{
key_press=getch();
ascii_value=key_press;
if(ascii_value==27) // For ESC
break;
cout<<"KEY Pressed-> " "<<key_press<<" " Ascii Value = "<<ascii_value<<"\n";
}
return 0;
}

OUTPUT:

Press Any Key To Check Its ASCI Value
Press ESC to EXIT
KEY Pressed-> "a" ASCII value = 97
KEY Pressed-> "b" ASCII value = 98
KEY Pressed-> "A" ASCII value = 65
KEY Pressed-> "*" ASCII value = 42

 

Leave a Comment