Solved

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

Posted on 2004-09-08
10
224 Views
Last Modified: 2008-02-01
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
Comment
Question by:konna432
  • 6
  • 4
10 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12005006
What type of project do you have, and what platform?
Also what is your compiler?
0
 

Author Comment

by:konna432
ID: 12005030
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
 
LVL 30

Expert Comment

by:Axter
ID: 12005047
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
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 12005060
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
 
LVL 30

Expert Comment

by:Axter
ID: 12005072
For more info, see following link:
http:Q_20337616.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:konna432
ID: 12005137
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
 

Author Comment

by:konna432
ID: 12005187
hey Axter..

mind telling me what

#define VK    27
#define VK2   91

are for?
0
 
LVL 30

Expert Comment

by:Axter
ID: 12005931
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
 

Author Comment

by:konna432
ID: 12040086
thank you for helping me out ;)
0
 
LVL 30

Expert Comment

by:Axter
ID: 12040394
>>thank you for helping me out ;)

Why the C grade? :-(
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now