How can I make my program in C detect an input of the arrow keys? And how can I put it in conditional statements like if( up arrow key ...) Does the arrow keys have ascii values?
Ah in this , go search for the ASCII code of the up arrow key, then use the ASCII code to implement your program.
I does not search
I send you that page , it has some information.
is it 37 38 39 40 for the left up right down arrow keys?
As I found here : http://chexed.com/ComputerTips/asciicodes.php they are as follow : 24 ↑ Up arrow 25 ↓ Down arrow 26 → Right arro 27 ← Left arrow
Windows: conio.h http://www.programmingsimplified.com/c/conio.h Other: ncurses http://invisible-island.net/ncurses/ncurses-intro.html
For console applications anyway
what are ncurses?
#include<iostream> using namespace std; #include <stdio.h> #include <conio.h> #include <dos.h> #include <windows.h> void gotoxy(int xpos, int ypos) { COORD scrn; HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE); scrn.X = xpos; scrn.Y = ypos; SetConsoleCursorPosition(hOuput,scrn); } int main (void) { int row=0,col=0; cout<<"press any Arrow key or enter: "; while(1) { if(kbhit()) { char c = getch(); if(c==-32) { c = getch(); if(c==80) // Downward { cout<<"Downward"; } else if(c==75) // Left { cout<<"Left"; } else if(c==77) // Up { cout<<"Right"; } else if(c== 72) // Right { cout<<"Up"; } } else if(c==13) { cout<<"Enter"; } else if(c==32) { cout<<"Space"; } else { cout<<c; } row++; gotoxy(0+col,row+0); cout<<"press any Arrow key or enter: "; } } return 0; }
This is in c++.Just do a small change to transfer it into C and then run,you will get your answer.Or simply run as c++ project
Join our real-time social learning platform and learn together with your friends!