| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have this C++ project that involves creating a console maze game, the problem is
that i need to read input from the keyboard and get that character immediately without having to press enter, so for instance the grid has a hero: xxxxxxxxxxxxxx and if the user presses the <- key the hero moves left. x x How can i read a in instantly? x H x x x xxxxxxxxxxxxxx |
|
#2
|
|||
|
|||
|
On which OS ?
-- julien barbier |
|
#3
|
|||
|
|||
|
//in windows the answer is simple!
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) #define VK_A 0x41 #define VK_B 0x42 #define VK_C 0x43 #define VK_D 0x44 #define VK_E 0x45 #define VK_F 0x46 #define VK_G 0x47 #define VK_H 0x48 #define VK_I 0x49 #define VK_J 0x4A #define VK_K 0x4B #define VK_L 0x4C #define VK_M 0x4D #define VK_N 0x4E #define VK_O 0x4F #define VK_P 0x50 #define VK_Q 0x51 #define VK_R 0x52 #define VK_S 0x53 #define VK_T 0x54 #define VK_U 0x55 #define VK_V 0x56 #define VK_W 0x57 #define VK_X 0x58 #define VK_Y 0x59 #define VK_Z 0x5A #include <iostream> #include <conio.c> // the library for the gotoxy() function #include <winbase.h> // the library for the Sleep() function using namespace std; main() { int x = 5; int y = 5; int tempx; int tempy; int altered = 0; int returned = 0; system("cls"); while (returned == 0) { gotoxy(tempx, tempy); cout<<" "; tempx = x; tempy = y; gotoxy(x,y); cout<<"dan"; while (altered == 0) { if (KEY_DOWN(VK_UP)) { y = y-1; altered =1; Sleep(200); } else if (KEY_DOWN(VK_DOWN)) { y = y+1; altered =1; Sleep(200); } else if (KEY_DOWN(VK_LEFT)) { x = x-1; altered =1; Sleep(200); } else if (KEY_DOWN(VK_RIGHT)) { x = x+1; altered =1; Sleep(200); } else if (KEY_DOWN(VK_RETURN)) { returned = 1; altered =1; Sleep(200); } } altered = 0; } } //im too lazy to comment that, but compile it and run it and mod it to your liking. basically what it does is query the key state of the keyboard and when the key is pressed, well, it knows.... one thing you have to remember is the Sleep function because otherwise it recognizes the key pressing too fast and runs through the loop several times before you lift your finger //-drizzle |
|
#4
|
|||
|
|||
|
About the sleeps: normally you can adjust
the fact that you will not receive several key messages without having the key_up. I don't know if conio does that, but if not you can adjust it yourself. -- julien barbier |
|
#5
|
|||
|
|||
|
to _julien_
hi we are on linux redhat 9
|
|
#6
|
|||
|
|||
|
so you should switch to non ECHO and non ICANON
mode for you term, and switch to O_NONBLOCK. Then you'll just have to read from 0 you keycode. That gives something like that: Code:
/* ** main.c for in /u/ept3/barbie_j/c ** ** Made by julien barbier ** Login julien dot barbier at epitech dot net ** ** Started on Fri Jun 11 15:48:02 2004 julien barbier ** Last update Fri Jun 11 15:56:14 2004 julien barbier */ #include <fcntl.h> #include <sys/ioctl.h> #if defined(__NetBSD__) # include <termios.h> # include <termcap.h> #endif #if defined(__sun) # define__lint # include <sys/types.h> # include <stropts.h> # include <curses.h> # include <sys/conf.h> # include <termio.h> # include <term.h> char *tgoto(char *cap, int col, int row); #endif #if defined(__alpha) # include <termio.h> # include <term.h> #endif #define SLEEP_TIME 1000 void initialise_ios_general(void) { #if (defined(__NetBSD__) || defined(__alpha)) struct termios termios; int status; status = ioctl(0, TIOCGETA, &termios); if (status == -1) exit(-1); termios.c_lflag &= ~(ICANON | ECHO); termios.c_cc[VMIN] = 1; termios.c_cc[VTIME] = 0; status = ioctl(0, TIOCSETA, &termios); if (status == -1) exit(-1); status = fcntl(0, F_SETFL, O_NONBLOCK); if (status == -1) exit(-1); #endif } void initialise_ios_sun(void) { #if defined(__sun) struct termio termio; intstatus; status = ioctl(0, TCGETA, &termio); if (status == -1) exit(-1); termio.c_lflag &= ~(ICANON | ECHO); termio.c_cc[VMIN] = 1; termio.c_cc[VTIME] = 0; status = ioctl(0, TCSETA, &termio); if (status == -1) exit(-1); status = fcntl(0, F_SETFL, O_NONBLOCK); if (status == -1) exit(-1); #endif } void initialise_ios(void) { #if defined(__sun) initialise_ios_sun(); #else initialise_ios_general(); #endif } unsigned int get_key(void) { unsigned char c; unsigned int key; while (read(0, &c, 1) != -1) ; while (read(0, &c, 1) < 0) usleep(0); key = c; usleep(SLEEP_TIME); while (read(0, &c, 1) != -1) { key <<= 8; key |= c; } return (key); } int main(void) { unsigned int key; initialise_ios(); while (1) { key = get_key(); printf("0x%x\n", key); } return (0); } Sorry I don't have Red Hat here, but this code works on netbsd x86, sun and alpha. So it would be easy to adapt to your distribution. Feel free to email me if you encounter a problem. -- julien barbier |
|
#7
|
|||
|
|||
|
thanks
thanks to everyone for all the help
|
|
#8
|
|||
|
|||
|
just so you know.... i found that the sleep function does work the best.... and the time is in milliseconds so its not that much of a delay
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Maze game: input char without pressing enter |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|