Skip to main content
GameDev.net gamedev.net
🔒 Locked

getch - C++

Started by daniel_i_l Aug 6, 2006 at 3:00 PM 1 replies 1.1k views
Original Post
daniel_i_l
daniel_i_l
How can I get input without the user haveing to press enter (like in a game for controling the arrows) in a console (dos) window with C++? - I'm not using Borland so my compiler (Dev) doesn't support the getch that comes with conio.h. Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Driv3MeFar
Driv3MeFar
You could wait for _kbhit() to return non-zero and then cin.get() the key that was hit, but I don't think its a very good solution, as _kbhit() is deprecated and may only work in MSVC++ (I'm not sure on this as thats really the only compiler I use often).
Bonedry
Bonedry
Quote:
Original post by daniel_i_l
How can I get input without the user haveing to press enter (like in a game for controling the arrows) in a console (dos) window with C++? - I'm not using Borland so my compiler (Dev) doesn't support the getch that comes with conio.h.
Thanks.


Windows API has a function, GetAsyncKeyState (Clicky), that can be used for this.

#include <windows.h>while(true){    if(GetAsyncKeyState(VK_LEFT) & 0x80000000)        ; // The left arrow key is down.    if(GetAsyncKeyState(VK_RIGHT) & 0x80000000)        ; // The right arrow key is down.    // Etc.}


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.