Using the arrow keys

This is a discussion on Using the arrow keys within the C Programming forums, part of the General Programming Boards category; Is there an ASCII number for the arrow keys on your keyboard? Right now I'm using the num pad arrows ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    15

    Using the arrow keys

    Is there an ASCII number for the arrow keys on your keyboard?

    Right now I'm using the num pad arrows (8 - Up,2 - Down,4 - Left,6 - Right) and it would be ideal to use the real arrows.

    Is there a way I can call on them, and will getchar() recoginize them is they are pressed?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    15
    I'm using mainly Windows 98 to develop and sometimes WinXp with Visual Studio 6.0.

    Any idea's?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Function/arrow keys are two-character values, 0+key or scan code

    Try this program to get the values of your keys in Windows. It'll also show you a way to deal with the function keys.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int i;
        
        i = 0;
        while (i != 0x1B)   // exit on ESC
        {
            i = getch();
            printf("   %02X ", i);
            if (i > 0x20 && i < 0x7F)   // If printable
                printf("[%c] ", i);     // ... output the character
            if (i == 0)     // test for a function key
            {
                i = getch();
                printf("%02X ", i);
                if (i > 0x20 && i < 0x7F)
                    printf("[%c] ", i);
            }
            printf("\n");
        }        
    }
    Yes, you could use isprint() but I wanted to limit the header files.

    Also, the FAQ mentions the "other" value that a few keys use instead of 0, 0xE0 (224) which you can also look for. I forgot which keys they are for, but they may be necessary if you want to check for ALL possible keys.
    Last edited by WaltP; 05-30-2003 at 09:31 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    if (i > 0x20 && i < 0x7F) // If printable
    Why not:

    if( isprint( i ) )

    Of course, I guess the code is already non-portable, so why bother...

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

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by quzah
    Of course, I guess the code is already non-portable, so why bother...

    Quzah.
    True, but that's not always a bad thing

    If you have no plans to run under Linux (or others), why not. The program can operate as you want it to.

    BTW, I edited my post about isprint() while you were responding. Wierd how that happens.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    True, but that's not always a bad thing

    If you have no plans to run under Linux (or others), why not. The program can operate as you want it to.
    Yeah. I just tend to toss out alternate suggestions, especially when there's a standard library functions to handle it.

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

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by quzah
    Yeah. I just tend to toss out alternate suggestions, especially when there's a standard library functions to handle it.
    But isn't it a fact that there is not a standard function in C/C++ to read a keystroke and process it immediately as getch() does? vVv's suggestion of tcsetattr() and (n)curses is also nonstandard.

    If not and such a standard exists, what is it? I'm unfamiliar with it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by vVv
    I believe quzah was suggesting that you can or should keep as many bits of your program portable, so you can write a couple of wrappers for non-portable pieces...
    All well and good -- for experts that write in Windows, Linux, Unix, OS/2, etc.

    These forums tend to be used by beginners. IMHO telling someone they can't do something because it's not standard is not helpful when in fact they can in their environment.

    Punkture did not tell us his environment so you answered correctly -- in Unix/Linux...

    ...but since isprint() does exist and makes your code even simpler, why not use it?)
    Lazy. I didn't want to have to explain how to use it and what header to load. I don't think isprint() necessarily makes it simpler, just more compact. If you've never seen isprint() before, >0x20 && < 0x7F makes more sense. After all, on my system 'print' could include 0x81 to 0xFE because my printer can handle the charset. So lack of knowledge is what I assumed.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

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