-
Arrow keys!
I'm trying to make a program that reads when an arrow key is pressed using getch() but it doesnt work.I use the ASCII values of the arrow keys to test if an arrow key has been pressed in the switch.
Whats wrong and how can I make that a program notes when an arrow key has been pressed?
#include <stdio.h>
#include <stdlib.h>
main(void)
{
int x=0,y=0,c;
do{
system("cls");
printf("x=%d,y=%d",x,y);
c=getch();
switch(c){
case 24:
y+=1;
break;
case 25:
y-=1;
break;
case 26:
x+=1;
break;
case 27:
x-=1;
break;
}
}while(c!='q');
return 0;
}
-
-
KEYS
I have read both threads and still dont know how to make that a program reads an arrow key.
NOW HELP!!
-
-
>NOW HELP!!
You're not going to get very far in life if you think people will jump at your every order. Try asking nicely next time, you may receive a better response. I was going to post code for you, but now I think I'll just let you figure it out for yourself.
-Prelude
-
Ah, let's forgive the poor Spaniard. After all, they have DIFFERENT WAYS over there:p I think he was just being fervent, an unfortunate but prevalent trait of our Old World brethren...anyhoo, what I had to do was rather hackish.
See, along with my compiler, (Dev C++/ for Windows machines), there were two conio files, one being conio.h, and one being conio_mingw.h (something like that). In the first file was the declaration for "int getch()", and this is the function I was trying to use to get arrow keys and such. Unfortunately, nothing would happen unless I pressed the "enter". No good. So while browsing the second file I noticed the declaration "int _getch()". That was it! Indeed, I was now able to get "unbuffered" key presses.
So I suggest you try that. Good luck. And do it NOW, you Spanish bastard!!:D
-
This has been covered recently...... maybe a search of your own would help you..... DO IT NOW :D
-
I didnt mean to sound rude or insolent, if I have offended anyone I beg his pardon...
Nexy tym I'll be rather polite
:D
-
>I didnt mean to sound rude or insolent
I have a hard time staying angry...especially after the great day that I've had. Judging from your first post I'll assume that you are using Windows, here's one option for reading the arrow keys using the WinAPI:
Code:
#include <stdio.h>
#include <limits.h>
#include <windows.h> /* windows.h usually includes stdlib.h */
int main ( void )
{
short esc = 0;
while ( !esc ) {
esc = GetAsyncKeyState ( VK_ESCAPE );
if ( GetAsyncKeyState ( VK_UP ) & SHRT_MAX )
puts ( "Up arrow is pressed" );
else if ( GetAsyncKeyState ( VK_DOWN ) & SHRT_MAX )
puts ( "Down arrow is pressed" );
else if ( GetAsyncKeyState ( VK_LEFT ) & SHRT_MAX )
puts ( "Left arrow is pressed" );
else if ( GetAsyncKeyState ( VK_RIGHT ) & SHRT_MAX )
puts ( "Right arrow is pressed" );
}
getchar();
return EXIT_SUCCESS;
}
>Nexy tym I'll be rather polite
Polite is good, I've found that people who do volunteer work tend to do better and keep doing it if you're nice to them. ;)
-Prelude