GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jun-2004, 01:21
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough

Receive arrow key input


how to receive arrow key : up,down,left,right input

for e.g.

char input;
if ( input == up ) // mean up arrow key
cout<< " up arrow key ";

thx
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #2  
Old 12-Jun-2004, 23:23
RonBurk RonBurk is offline
New Member
 
Join Date: Jun 2004
Posts: 7
RonBurk will become famous soon enough
For what version of what operating system would you like to do this? Also, are you already using a framework/library (such as MFC) that has it's very own custom way of dealing with this layered on top of operating system primitives?
  #3  
Old 13-Jun-2004, 01:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by tay
how to receive arrow key : up,down,left,right input

for e.g.

char input;
if ( input == up ) // mean up arrow key
cout<< " up arrow key ";

thx
Using standard C/C++ you can't. Some compilers have added extensions to the standard to allow this, but it's very compiler dependant.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #4  
Old 13-Jun-2004, 04:28
sho sho is offline
Junior Member
 
Join Date: Jun 2004
Posts: 49
sho will become famous soon enough
Lightbulb

Quote:
Originally Posted by tay
how to receive arrow key : up,down,left,right input

for e.g.

char input;
if ( input == up ) // mean up arrow key
cout<< " up arrow key ";

thx

Well, I think that if you are working with a standard console application you could use the <conio.h> include, and use its getch() function to get the key pressed. Example:

char input;
input=getch();

Then you could compare its value with the ASCII value of the key you wanted in your if statement.

if ( input == ASCII value of up arrow ) // mean up arrow key
cout<< " up arrow key ";

You could also use this to check for any other key.
Hope this gives you an idea.
  #5  
Old 13-Jun-2004, 10:41
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
Quote:
Originally Posted by sho
Well, I think that if you are working with a standard console application you could use the <conio.h> include, and use its getch() function to get the key pressed. Example:

char input;
input=getch();

Then you could compare its value with the ASCII value of the key you wanted in your if statement.

if ( input == ASCII value of up arrow ) // mean up arrow key
cout<< " up arrow key ";

You could also use this to check for any other key.
Hope this gives you an idea.

u suggestion are good, but i try before
arrowkey is not a standard input (i guess so)
even use getch() also nothing happen
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #6  
Old 13-Jun-2004, 12:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by tay
u suggestion are good, but i try before
arrowkey is not a standard input (i guess so)
even use getch() also nothing happen
Oh, you are using a compiler that has the non-standard function getch()? If you had mentioned that I could have given you an example...

The function and arrow keys are 2-character keys. First char is 0, 2nd char is the key. The following shows how to access the keys and will also give you the key-commbinations:
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>  /* needed for kbhit and getch */
#include <ctype.h>  /* needed for isprint         */


int  main()
{
    int  ch;
    
    ch = -1;
    while (ch != 0x1B)              /* watch for an ESC */
    {
        if (kbhit())                /* check for a keystroke */
        {
            ch = getch();           /* get the key */
            printf("%02X", ch);
            if (isprint(ch)) printf("/%c", ch);
            if (ch == 0)            /* if it's 0... */
            {
                ch = getch();       /* get the next key */
                printf("  %02X", ch);
               if (isprint(ch)) printf("/%c", ch);
            }
            printf("\n");
        }
    }
    return 0;
}
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 14-Jun-2004, 12:06
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
Quote:
Originally Posted by WaltP
CPP / C++ / C Code:
                  printf("%02X", ch);         // what is %02X means?

                  isprint(ch)                    // what is isprint(ch)?


sorry C i not really well,
can u explain more a bit to me

i try to convert to c++ but unfortunately the output is different
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #8  
Old 15-Jun-2004, 00:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
CPP / C++ / C Code:
printf("%02X", ch);
This prints ch as a hex value, 2 characters, 0 filled. I find this much easier than that cout gobbledygook

CPP / C++ / C Code:
isprint(ch)
Tests a character to see if it's printable (ASCII 0x20 to 0x7E)

Just compile the program and see what it does...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #9  
Old 15-Jun-2004, 11:57
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
thx WaltP
1 more question

if we mix with c and c++
will it cause any problem?
__________________
challenges are make life interesting,
overcome them is make life meaningful.
  #10  
Old 15-Jun-2004, 12:54
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
You can mostly use C with C++ (namespaces tend to divide the two), but don't try using C++ if you're using a compiler that only works for C.
__________________
-Aaron
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
scanf / clearing input stream zone4 C Programming Language 8 08-May-2004 15:42
IP tables rogermark100 C Programming Language 6 18-Apr-2004 08:22
Help with array and function input FearlessFife35 C Programming Language 13 16-Apr-2004 15:10
Need Help on checking user input hihellochao C Programming Language 5 27-Feb-2004 14:30
Script needed for letting user input a few days of data for tracking and analysis. tradertt MySQL / PHP Forum 3 06-Mar-2003 03:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.