Archived

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

arrow keys

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

Recommended Posts

i''m programming in c++ in a console(on windows) in my small program/game i want to be able to tell if and which arrow key has been pressed. i think what i would like to do is catch the signal off the keyboard port, before the scancode is converted to whatever. that way i could just put that scancode in some variable and loop it so i can tell which arrow key has been pressed. anyways my question is simply is there a way to recieve the scancode from the keyboard directly and if so how would i do that. any code or a pointer in the right direction would be great. thanks

Share on other sites
Well, I''m not sure about getting it totally directly, but SDL''s keyboard functionality is really easy to use and works extremely well.

www.libsdl.org

Share on other sites
It might require more management, but you can set a "message loop" on the input buffer using console-specific functions in the Win32 API. In fact, just today, I was doing this. This will allow you to receive all sorts of keys, and even mouse events and window resizing events, I believe, if you want them. The reference is here. Here''s what the loop that I just wrote today looks like:
  INPUT_RECORD InputEvent;  DWORD NumEvents;  DWORD EventsRead;  bool Exit = false;  while (!Exit)  {    GetNumberOfConsoleInputEvents(ghInputBuffer, &NumEvents);    while (NumEvents > 0)    {      ReadConsoleInput(ghInputBuffer, &InputEvent, 1, &EventsRead);      switch (InputEvent.EventType)      {        case KEY_EVENT:        {          const KEY_EVENT_RECORD& KeyEvent = InputEvent.Event.KeyEvent;          switch (KeyEvent.wVirtualKeyCode)          {            case VK_ESCAPE:            {              if (KeyEvent.bKeyDown == FALSE)                Exit = true;              break;            }          }          break;        }      }      NumEvents -= 1;    }    Draw();    Sleep(100);  }
Of course, this will make it harder to do some things, since you probably won''t be able to use scanf or cin. Well, you may be able to still use those by using PeekConsoleInput() instead of ReadConsoleInput(), and then whenever you peek and find a special key, like the arrow keys, then just do a full ReadConsoleInput() to remove them from the buffer. Otherwise, the stuff remains in the buffer for scanf and cin to use.

Or maybe my suggestions are more complicated for you than your needs demand. Maybe it could help, though...

Share on other sites
more specifically what i really want to do, and what i''ve been trying to do for 3 days now in a console with c++ is this... everytime i call this function say blah(). I want blah to check if an arrow key is currently be pressed down. if up arrow is pressed down i want my variable direction to equal 1. if left arrow pressed direction to equal 2. and so on with 3 for down and 4 for right. i''ve tried and honestly haven''t been able to come up with a solution. i image if anybody has tried this it should be easy for them, so if i could just get some answers that would be cool, thanks

Share on other sites
Here you go, simple code:

#include <iostream>#include <windows.h>main(){	INPUT_RECORD *str = new INPUT_RECORD[1];	DWORD num = 0;	do	{		ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), str, 1, &num);		if(num != 0 && str[0].Event.KeyEvent.bKeyDown)		{			WORD key = str[0].Event.KeyEvent.wVirtualKeyCode;			switch(key)			{			case VK_UP:				{					// UP key					std::cout << "up ";				} break;			case VK_DOWN:				{					// DOWN key					std::cout << "down ";				} break;			case VK_LEFT:				{					// LEFT key					std::cout << "left ";				} break;			case VK_RIGHT:				{					// RIGHT key					std::cout << "right ";				} break;			case VK_ESCAPE:				{					// Quit					exit(0);				} break;			}			num = 0;		}	} while(true);}

Share on other sites
You should look up Console Functions on msdn.microsoft.com. There are some very cool tricks you can pull with consoles. And learning handles is the step to Windows programming.

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share on other sites
sorry to be so whining, but first thanks for that code it worked somewhat nicely. The one other thing i guess i forgot to mention that i need this function to do is when NO arrow key is being pressed to continue. what i tried was adding a default that did nothing.

default:
{
;
} break;

i was hoping that would just exit the loop if no keys were being pressed. but instead my program sits on this function until a key is pressed and then continues in my program. I also tried a continue in place of break. i don''t know if that was smart but prolly not because it didn''t work. any help would be awsome, thanks.

Share on other sites
Use the function PeekConsoleInput instead. The parameters are the same, but that function only locks if there actually is input pending.

Share on other sites
What about GetAsyncKeyState? Put that in a function in your game loop. It checks for a key press and returns a Virtual-Key Code. It also wont wait for a keypress like cin. So it wont hold up your program and it can run in real time.