#### Archived

This topic is now archived and is closed to further replies.

# Problem!

This topic is 6395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''ll put it simply:
  /* DEFINE KEY CHARACTERS */ char upkey; char downkey; char leftkey; char rightkey; char keypress; /* PROTOTYPES */ void empty(void); /* KEY ASSIGNMENTS */ printf("Choose ''UP'' Key\n"); upkey=getch(); // assign up key empty(); // empty keyboard buffer do { printf("Choose ''DOWN'' Key\n"); downkey=getch(); } while(downkey==upkey); // check for double assignment empty(); do { printf("Choose ''LEFT'' Key\n"); leftkey=getch(); } while(leftkey==downkey || leftkey==upkey); empty(); do { printf("Choose ''RIGHT'' Key\n"); rightkey=getch(); } while(rightkey==leftkey || rightkey==downkey || rightkey==upkey); // ... code keypress=getch(); switch(keypress) { case upkey: y--; break; case downkey: y++; break; case leftkey: x--; break; case rightkey: x++; break; } // ... code /* EMPTY FUNCTION */ void empty(void) { while(kbhit()) { getch(); } } 
It works okay... for most keys... upon trying to hit the up arrow, down arrow, left arrow, and right arrow respectively, I found that in game, hitting "up" would send me up, hitting "down" would send me up, hitting "left" would send me left, and hitting "right" would send me left. Since the keyboard buffer was emptied after each one, I figured maybe the arrow keys required a larger variable space, so I used the wchar_t variable to hold them, which is supposed to be doubly as large, so it should fit them... but it didn''t. Same result. Any ideas? - Goblin "A woodchuck would chuck as much wood as a woodchuck could if a woodchuck could chuck wood. Deal."

##### Share on other sites
If you try using getch() to get a key -- like an arrow key -- that has a 2-byte (extended) ASCII code, you''ll just end up with a null character. The up arrow key, for instance, is denoted by a null character followed by (char)72. To get those keypresses with getch(), you have to call getch() twice, something like this:
char k;if ((k = getch()) == 0){    switch (k = getch())    {    case 72:        // code to handle up arrow key goes here        break;    case 75:        // code to handle left arrow key goes here        break;    case 77:        // code to handle right arrow key goes here        break;    case 80:        // code to handle down arrow key goes here        break;    }}

The way the code you''ve listed is set up, you''re only reading the null character, so that''s what gets assigned as every key. I think. If you want to use getch() for input then you''ll have to come up with some way to flag whether the user''s key selections are regular ASCII codes, or extended ASCII codes, so you don''t have any problems with input in the program.

It''s not the best way to do keyboard input for a game though. Are you programming in Windows? If so, it''s much easier just to use GetAsyncKeyState(), since it won''t have the problems that getch() has: double-keypress speed and simultaneous keypresses, both of which are necessities for games. Or there''s always DirectInput if you''re at all familiar with DirectX. If you''re working in DOS, a keyboard handler written in assembly is practically a requirement for games, especially if it''s something fast-paced like an action game.

-Ironblayde
Aeon Software

##### Share on other sites
Assembly?!

*melts*

- Goblin
"A woodchuck would chuck as much wood as a woodchuck could if a woodchuck could chuck wood. Deal."