Solved

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

Posted on 2004-09-08
10
220 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 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

12 Experts available now in Live!

Get 1:1 Help Now