Question:
Is it possible to pick up the arrow keys (famous little arrows) on the keyboard? if yes, how?
Answer:
In pure C/C++ you can't do this. To handle arrow keys you must work with a specific API.
You can do this using Windows API (Win32) , SDL2 , SFML , Allegro , Ogre3d , GTK , Qt , wxWidgets and etc, which make the interaction between the operating system and your code.
For example, with SDL
, in the execution loop you would have something like:
SDL_Event event;
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_KEYDOWN:
printf( "Seta para baixo pressionada\n" );
break;
case SDL_KEYUP:
printf( "Seta para cima pressionada\n" );
break;
default:
break;
}
}
In wxWidgets
it would be something like this:
void MyWin::OnKeyPress(wxKeyEvent& event)
{
long keycode = event.GetKeyCode();
if (keycode == WXK_UP)
{
// Seta para cima pressionada.
event.Skip();
}
}
In SFML
it is something like:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
// pressionou para esquerda
}
Ideally, you should choose your own API for the project you want to run, as the way in which each of them handles this is a little different. For example, for games it can be SDL, SFML, Ogre3D. For programs it can be Qt, wxWidgets, WIN32, GTK and etc.
The main difference is that in game APIs the keys are checked in "real time". In the APIS for developing desktop programs, the keys are treated as events.