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

keyboard input in the console

Started by Gor435 Jan 20, 2005 at 11:09 PM 10 replies 1.3k views
Original Post
Gor435
Gor435
how do i process up down left and right buttons in the console window using c++?
nprz
nprz
I'm not sure how to do it for linux, but for Windows you can use getch()
you have to include
the first character it will read will always be of value 224.
Gor435
Gor435
close but no cigar im using windows btw

what if i wanna do somthing like this

if(up key is press){move up // <-- dont worry about this part i know what to do here :P}


any thoughts??
nprz
nprz
Quote:
Original post by Gor435
close but no cigar im using windows btw

what if i wanna do somthing like this

*** Source Snippet Removed ***

any thoughts??


const int KEY_UP = 72;const int KEY_DOWN = 80;const int KEY_LEFT = 75;const int KEY_RIGHT = 77;const int KEY_EXTRA = 224;...input = getch();if (input == KEY_EXTRA){	input = getch();	if (input == KEY_UP)	{		// move up code	}}


something like that
Leffe
Leffe
I'd suggest using a full Curses implementation like PDcurses instead.

The code one would use then might look something like this:

#include <curses.h>initscr();loop    int ch = getch();    if (ch == KEY_LEFT) // KEY_LEFT is defined by Curses.        move_left();end loopendwin();


On Linux I'd suggest using NCurses (which is probably quite compatible with PDcurses), PDcurses is quite multiplatform though I think.
Fruny
Fruny
Quote:
Original post by Gor435
how do i process up down left and right buttons in the console window using c++?


C++ does not provide support for that kind of things. All its input and output is done through streams and file abstractions. As the other posters suggested, there are platform-specific APIs that will let you handle it.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Joshnathan
Joshnathan
uh, it has been a while since I have done it, but gametutorials.com had a nice tutorial on it (now it is paying though...). You should use something like "ReadConsoleInput()", although I don't know which parameters it takes anymore. Do a google search on that and you should be able to find the answer quickly :D

Hope that helps,
Joshua
-----------------------------Sismondi GamesStarted c++ in October 2004...
Gor435
Gor435
thank you nprz that works and is exactly what i was looking for exept one problem now the program halts and waits for you to press a key when it calls getch() is their a way around this??

ps i had to modify the code you posted a bit but basicly that is the proper way to use it. ty
pex22
pex22
That's getch.
getche is like getch, but it also prints the selected key to stdout (the dos/console screen).
what you want is something like this function (getch,getche and this function are DOS ones but work fine in console): bioskey()
Wait for a key to be pressed: while (bioskey(1)==0);
Get the key that is waiting: key = bioskey(0);
Check if shift keys were used: modifiers = bioskey(2);
where:
if (modifiers & 0x01) // LEFT
if (modifiers & 0x02) // RIGHT
if (modifiers & 0x04) // CTRL
if (modifiers & 0x08) // ALT

But what's wrong with ReadConsoleInput? i dont think you suppose to use bioskey in a console application..
pex.
Gor435
Gor435
i looked up ReadConsoleInput before i even posted the first time it didnt make any sense i understand why you need the handle and why you need the NULL at the end but i dont understand the other parameters i played with it a while and didnt get anywhere so i posted.
pex22
pex22
MSDN and its ReadConsoleInput Example didnt helped?
then i'll try:
INPUT_RECORD inputBuffer[128];DWORD numberOfReadedRecords;if (!ReadConsoleInput(StdinHandle,inputBuffer,128,&numberOfReadedRecords) )fprintf(stderr,"ReadConsoleInput error: %d\n",GetLastError());

inputBuffer is used to take the readed input
128 is the max records that inputBuffer will take.
numberOfReadedRecords is the number of records that was readed.
here, it will always be less than 128 (less or equal to 127. it begins from 0).
now you'll have to process the input records.
from the msdn example:
        for (i = 0; i < cNumRead; i++)         {            switch(inputBuffer.EventType)             {                 case KEY_EVENT: // keyboard input                     KeyEventProc(inputBuffer.Event.KeyEvent);                     break;                  case MOUSE_EVENT: // mouse input                     MouseEventProc(inputBuffer.Event.MouseEvent);                     break;                  case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing                     ResizeEventProc(                         inputBuffer.Event.WindowBufferSizeEvent);                     break;                  case FOCUS_EVENT:  // disregard focus events                  case MENU_EVENT:   // disregard menu events                     break;                  default:                     MyErrorExit("unknown event type");                     break;             }         }

I'm not sure since i dont program for consoles, but i think ReadConsoleInput returns only when at least 1 input was recorded.
To determine if there is an unread input record, you can do:
DWORD temp;if (!GetNumberOfConsoleInputEvents(StdinHandle,&temp) )fprintf(stderr,"GetNumberOfConsoleInputEvents Error: %d\n",GetLastError());if (temp)// Waiting Inputs! do ReadConsoleInputelse// Idle Stdin

note that i cant be sure that im right. try it, i think it should be like that.

pex.
pex.
Gor435
Gor435
thats cool at least i know when i tried it i wasnt too far off ty i will work with this and see how it works

ty

Topic Locked

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

Sign in to reply to this topic.