Thread: How to read and recognize arrow key taken as an input in c/c++

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    How to read and recognize arrow key taken as an input in c/c++

    I am facing this problem from last 2-3 days , i have searched about it but couldn't get the help.
    I want to know that how can we take arrow keys as an input and then how can we recognize them that whether the key is "UPARROW" or "DOWNARROW" or "RIGHTARROW" or "LEFTARROW".

    plz provide some help....... i will be thankful

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by surajgyl View Post
    i have searched about it but couldn't get the help.
    Did you ?
    I see better results : Let me google that for you

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by manasij7479 View Post
    Did you ?
    I see better results : Let me google that for you
    Sir will u plz help me out to understand this code.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    The code is:-
    Code:
    #include<iostream.h>
    Code:
    #include<conio.h>
    #include<dos.h>
    
    int main()
    {
    clrscr();
    int ch;
    while(1)
    {
    ch=getch();
    if(ch==0)
    {
    ch=getch();
    if(ch==72) cout<<"up";
    else if(ch==75) cout<<"left";
    else if(ch==77) cout<<"right";
    else if(ch==80) cout<<"down";
    cout<<endl;
    }
    else break;
    }
    delay(2000);
    return 0;
    }
    

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    ...and your question now is?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by QuantumPete View Post
    ...and your question now is?
    Sir,
    the question is, how do we compare these keys. i mean i know everykey has an ascii value, and the source code i have mentioned above is comparing ascii values of arrow keys as "72,75,77 and 80" but according to my knowledge 72 is the ascii value of "H", and that is my question, how we can compare those keys ???

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    no solution

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Don't double post. There's an "edit" button for a reason.

    Also, the source code you found/posted is terrible. It isn't standard C (cout?), it uses all outdated headers, and, as you said, the value of the arrow keys isn't represented by 72, 75, 77, and 80.

    I wrote a quick test (note: WON'T WORK IF NOT *NIX) to determine the *real* value of the escape codes that represented an arrow:
    Code:
    	int ch, i;
    	struct termios term;
    	
    	tcgetattr( STDIN_FILENO, &term );
    	term.c_lflag &= ~( ICANON | ECHO );
    	tcsetattr( STDIN_FILENO, TCSANOW, &term );
    	
    	for (i = 0; i < 4; i++) {
    		puts("enter arrow");
    		ch = getchar() & 0xFF;
    		ch += getchar() & 0xFF;
    		ch += getchar() & 0xFF;
    		printf("%c | %d\n", ch, ch); 
    	}
    This took the (usually 3) characters that represent an arrow, and captured them all. The result:
    Code:
    enter arrow
    ? | 183
    enter arrow
    ? | 184
    enter arrow
    ? | 185
    enter arrow
    ? | 186
    In order: up, down, left, right.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by memcpy View Post
    It isn't standard C (cout?)
    See: cout - C++ Reference


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read Arrow Keys in C++
    By kaveh8 in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2011, 01:02 AM
  2. Program input to recognize as hex and not characters
    By Diavolche in forum C Programming
    Replies: 12
    Last Post: 04-27-2010, 05:51 PM
  3. How to read arrow keys in Linux?
    By edugarcia in forum C Programming
    Replies: 4
    Last Post: 11-09-2004, 03:20 PM
  4. how do you get arrow key input in DOS??
    By kwm32 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-25-2004, 01:28 PM
  5. Arrow key input?????
    By Vorkosigan in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2004, 09:19 PM