Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Need codes for getting the input of the "arrow" on the keyboard in c++

Hi guys..

how are you doing?
i'm not doing too good here..

anyway i think my question title has sum up the whole thing.. hehe :P

i would like to know the codes for getting the input from the keyboard when a user preses the arrow keys.
ie: up, down. left. right

how would that represent in term of c++? i have been trying to code this for the past fews days.. and so far i have not found or understand how to do it.. hahahahahhaa.. (-_-")

thank you ever so much guys... :D
0
konna432
Asked:
konna432
  • 6
  • 4
1 Solution
 
AxterCommented:
What type of project do you have, and what platform?
Also what is your compiler?
0
 
konna432Author Commented:
the project is for the user to press either left of right key to go forward and backward..

i am using Linux.. well.. using C++ for the programme

i would like to read the key as a char array.. this is because i am using cin.getline()
any idea?
0
 
AxterCommented:
Here's some example code for Win32 console application:

enum { LEFT_ARROW=75, UP_ARROW=72, RIGHT_ARROW=77, DOWN_ARROW=80};

 int ch = getch();
  if(ch == 0) {
      ch = getch();      // retrieve the second byte
        switch (ch) {
            case UP_ARROW:
                // do some code
                break;
            case DOWN_ARROW:
                // do some code;
                break;
            case LEFT_ARROW:
                // do some code;
                break;
            case F1:
                // do some code;
                break;
            case F2:
                // do some code;
                break;
             // ... etc ...
        }                        
    }

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AxterCommented:
Example Linux code from a previous question:

In Linux it's slightly more complicated than that.  You have to set the terminal attributes slightly, and the arrows are actually 3 characters in a row.  You might have to play with this a little on your terminal, but something like the following should work for you:

#include <iostream>
#include <stdio.h>
#include <termio.h>
#include <curses.h>

#define VK    27
#define VK2   91
#define UP    65
#define DOWN  66
#define RIGHT 67
#define LEFT  68

using namespace std;

void set_keypress(void) {
  struct termios new_settings, stored_settings;
  // Get the current settings
  tcgetattr(0,&stored_settings);
  new_settings = stored_settings;

  // Remove printing of special characters
  new_settings.c_lflag &= (~ICANON);

  // Remove wait time
  new_settings.c_cc[VTIME] = 0;
  tcgetattr(0,&stored_settings);
  new_settings.c_cc[VMIN] = 1;
  tcsetattr(0,TCSANOW,&new_settings);
}

int main()
{
  set_keypress();

  int key;
  while(true)
  {
    key = getchar();
    cout << "key = " << key << endl;
    if(key == VK)
    {
      if(getchar() == VK2)
      {
        switch(getchar())
        {
        case UP:
          cout << "key = UP ARROW" << endl;
          break;
        case DOWN:
          cout << "key = DOWN ARROW" << endl;
          break;
        case LEFT:
          cout << "key = LEFT ARROW" << endl;
          break;
        case RIGHT:
          cout << "key = RIGHT ARROW" << endl;
          break;
        }
      }
    }
  }

  return 0;
}
0
 
AxterCommented:
For more info, see following link:
http:Q_20337616.html
0
 
konna432Author Commented:
Thank you Axter (^_^)

you are the man.. :D i'll try to complie it now.. see if it work
else i'll get back to you and ask you some more questions about it (^_^)
0
 
konna432Author Commented:
hey Axter..

mind telling me what

#define VK    27
#define VK2   91

are for?
0
 
AxterCommented:
My Linux knowledge is limited.
27 is the escape key in DOS/Windows.
I believe in Linux 27 signals that spacial character is to follow.

I'm assuming that VK2 is a certain sub set of the extended/special characters.
0
 
konna432Author Commented:
thank you for helping me out ;)
0
 
AxterCommented:
>>thank you for helping me out ;)

Why the C grade? :-(
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now