Arrow keys!

This is a discussion on Arrow keys! within the C Programming forums, part of the General Programming Boards category; I'm trying to make a program that reads when an arrow key is pressed using getch() but it doesnt work.I ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Question Arrow keys!

    I'm trying to make a program that reads when an arrow key is pressed using getch() but it doesnt work.I use the ASCII values of the arrow keys to test if an arrow key has been pressed in the switch.

    Whats wrong and how can I make that a program notes when an arrow key has been pressed?



    #include <stdio.h>
    #include <stdlib.h>

    main(void)
    {
    int x=0,y=0,c;

    do{
    system("cls");
    printf("x=%d,y=%d",x,y);
    c=getch();

    switch(c){
    case 24:
    y+=1;
    break;
    case 25:
    y-=1;
    break;
    case 26:
    x+=1;
    break;
    case 27:
    x-=1;
    break;
    }
    }while(c!='q');


    return 0;
    }
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    paninaro3000@yahoo.com

  2. #2
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    ┐Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    KEYS

    I have read both threads and still dont know how to make that a program reads an arrow key.

    NOW HELP!!
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    paninaro3000@yahoo.com

  4. #4
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    ┐Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >NOW HELP!!
    You're not going to get very far in life if you think people will jump at your every order. Try asking nicely next time, you may receive a better response. I was going to post code for you, but now I think I'll just let you figure it out for yourself.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Ah, let's forgive the poor Spaniard. After all, they have DIFFERENT WAYS over there I think he was just being fervent, an unfortunate but prevalent trait of our Old World brethren...anyhoo, what I had to do was rather hackish.

    See, along with my compiler, (Dev C++/ for Windows machines), there were two conio files, one being conio.h, and one being conio_mingw.h (something like that). In the first file was the declaration for "int getch()", and this is the function I was trying to use to get arrow keys and such. Unfortunately, nothing would happen unless I pressed the "enter". No good. So while browsing the second file I noticed the declaration "int _getch()". That was it! Indeed, I was now able to get "unbuffered" key presses.

    So I suggest you try that. Good luck. And do it NOW, you Spanish bastard!!



    ITSA
    Socket Library!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This has been covered recently...... maybe a search of your own would help you..... DO IT NOW
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    75
    I didnt mean to sound rude or insolent, if I have offended anyone I beg his pardon...

    Nexy tym I'll be rather polite

    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    paninaro3000@yahoo.com

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I didnt mean to sound rude or insolent
    I have a hard time staying angry...especially after the great day that I've had. Judging from your first post I'll assume that you are using Windows, here's one option for reading the arrow keys using the WinAPI:
    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <windows.h> /* windows.h usually includes stdlib.h */
    
    int main ( void )
    {
      short esc = 0;
      while ( !esc ) {
        esc = GetAsyncKeyState ( VK_ESCAPE );
        if ( GetAsyncKeyState ( VK_UP ) & SHRT_MAX )
          puts ( "Up arrow is pressed" );
        else if ( GetAsyncKeyState ( VK_DOWN ) & SHRT_MAX )
          puts ( "Down arrow is pressed" );
        else if ( GetAsyncKeyState ( VK_LEFT ) & SHRT_MAX )
          puts ( "Left arrow is pressed" );
        else if ( GetAsyncKeyState ( VK_RIGHT ) & SHRT_MAX )
          puts ( "Right arrow is pressed" );
      }
      getchar();
      return EXIT_SUCCESS;
    }
    >Nexy tym I'll be rather polite
    Polite is good, I've found that people who do volunteer work tend to do better and keep doing it if you're nice to them.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 03:35 PM
  2. Interfacing with arrow keys...
    By adityakarnad in forum Game Programming
    Replies: 1
    Last Post: 08-30-2003, 10:25 PM
  3. Ascii code for arrow keys
    By beginner in forum C Programming
    Replies: 1
    Last Post: 11-07-2002, 12:29 PM
  4. msdos arrow keys?
    By seditee in forum Game Programming
    Replies: 3
    Last Post: 05-07-2002, 11:29 PM
  5. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 10:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21