Solved

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

Posted on 2004-09-08
10
232 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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
 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

803 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