-
single key input
I'm trying to get single keypress input without having to press enter after each key.
I can do it using conio.h but that isn't supported by the standard.
Which means in DevC++ it won't let me compile if I have
-Wall -Wstrict -prototypes -ansi -pedantic
turned on.
This is what I have
Code:
char key;
key=getch();
switch(key)
{
case 'w': mover.move(0,-1); break;
case 's': mover.move(0,1); break;
case 'a': mover.move(-2,0); break;
case 'd': mover.move(2,0); break;
}
Anyone got a simple way to do it that will work with the ansi warnings and stuff turned on?
-
does getche work for you?
Code:
char c = (char)getche();
~Inquirer
-
>Anyone got a simple way to do it that will work with the ansi warnings and stuff turned on?
No, under the constraints of the standard there is no way to do this.
-Prelude
-
Quote:
Originally posted by Inquirer
does getche work for you?
Code:
char c = (char)getche();
~Inquirer
I dunno what you mean...
I tried this:
Code:
#include <iostream.h>
#include <stdlib.h>
int main()
{
char c = (char)getche();
return 0;
}
but it says getche isn't defined, so assumably there is something I need to include...
-
>>there is something I need to include...
maybe conio.h if you have it. (again, it's non-standard)
-
[EDIT]
I am sorry, i musn't have read your first post clearly enough. I didn't realize that you don't want to include conio.h. I have heard that it is possible to do it through raw communication with the istream cin, but i don't know how to approach this. Sorry for any inconvenience.
~Inquirer
[/EDIT]
[IGNORE]
Yes, conio.h would be the one. Also, remember to cast it to a char. I had endless problems with this, and i wanted to warn you. Also, be careful of getche, because some (special) keys on the keyboard send two chars to the buffer, so if the user hits the left arrow key, it would return (char)224 the first time, and something else the second time. This can mess things up too. Let us know if you have getche, and we (I) will be happy to post some more example code o get you aterted.
[IGNORE]
~Inquirer
-
hmmm....
just go get conio.h ... shouldnt it be downloadable somewhere if it dont come with the compiler?
keyboard input would be great!
-
Read the FAQ, under "can't i just get the header" or something like that.
http://www.cprogramming.com/boardfaq.html#header
-
I HAVE conio.h That's where I was using getch from. conio.h is very nice, and getch is wonderful, BUT it's non-standard. The point is, I want to be able to do this using standard stuff only.
Thanks for your help anyway people.
-
so where do you get conio.h anyways? sounds like a nice file to have.
-
Viper, did you read the faq like Inquirer said?
You can't 'get' things like conio.h, you either have them or not.
Bloodshed DevC++ has conio.h, that compiler is about 15Mb to download, and it's free, so go look for it if you want...
-
Thanks spiritofcat.
Which compiler option are you using to make it only compile standard things?
-
I mentioned them in my original post, but I'll be clearer this time.
Options>Compiler Options: Directories tab, Add the following commands when calling compiler: -Wall -Wstrict -prototypes -ansi -pedantic
Then when you try to compile something that uses conio.h or other non-standard stuff it gives you wanrnings and refuses to compile.
-
i love bloodshed. but i didn;t search the header files yet. maybe ill find it.
-
There is no way to do it portably, but why not just create a header for each implementation called "key.h" or something, and place the appropriate one in the directory at compile time (else name them different and use ifdef's). For the Bloodshed version just do something like:
// key.h
#include <conio.h>
#define getkey _getch
...and in Bloodshed's conio.h, just do:
// conio.h
int _getch(); //...Colin Peters hid this one, define it, and it's yours :)