Di Posting Oleh : Simple Learning
Kategori : cpp detecting keypress tutorial
Write a simple cpp program which detects a key pressed and its ASCII value with out pressing enter key and using cin>> on press ESC key program should exit. When user press a key its ASCII value should be displayed instantly. You can use any Cpp compiler CodeBlocks Recommended.
Concept used
Value
Up Key 72
Down Key 80
Right Key 77
Left Key 75
Cpp code: Compiler used CodeBlocks Cpp Compiler
Its a simple program which helps the beginner to control input keys.
Concept used
- Char and int data types
- while infinite loop
- conio.h header file getch() function
- Its a simple program which reads a character of char data type
- A header file conio.h is included to use getch() function
- An infinite while loop breaks on pressing ESC key
- In while loop when program runs getch() function calls which wait to get character when we enter a character it store is ASCII value in int variable then displays the pressed key and its ASCII value on screen and then wait to another input a character.
Value
Up Key 72
Down Key 80
Right Key 77
Left Key 75
Cpp code: Compiler used CodeBlocks Cpp Compiler
- #include<iostream>
- #include<conio.h>
- using namespace std;
- int main()
- {
- char key_press;
- int ascii_value;
- cout<<"\n\t\t\tPress Any Key To Check Its ASCI Value\n\n\t\t\tPress ESC to EXIT\n\n\n";
- while(1)
- {
- key_press=getch();
- ascii_value=key_press;
- if(ascii_value==27) // For ESC
- break;
- cout<<"\t\t\tKEY Pressed-> \" "<<key_press<<" \" Ascii Value = "<<ascii_value<<"\n\n";
- }
- return 0;
- }
Its a simple program which helps the beginner to control input keys.
0 Response to "Cpp tutorial detecting a keypress and ASCII code"
Post a Comment