Reading Arrow keys from console

This is a discussion on Reading Arrow keys from console within the C Programming forums, part of the General Programming Boards category; How do I read the up, down, left, and right arrow keys as a character? Using getch(), they all seem ...

  1. #1
    D
    Guest

    Reading Arrow keys from console

    How do I read the up, down, left, and right arrow keys as a
    character?

    Using getch(), they all seem to have the ascii value of zero, and I can't distinguish them individually.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    try this:

    Code:
    int testnum = getch();
    printf("%d",testnum);
    find out what the numbers for the arrow keys are, then do

    Code:
    int input;
    input = getch();
    switch(input)
    {
     case 14: left(); break; // depends on what numbers you get from the arrow keys in the test code if it's 40, then do case 40:.
     ...etc...

  3. #3
    D
    Guest

    I tried that

    It doesn't work. The arrow keys don't have individual ascii values. They are all null. Even more annoying is the fact that they stay resident in stdin.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Not with my compiler...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The arrow keys are NOT null. They are actually two keystrokes. One is an escape or control sequence, the second is the actual response.

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

  6. #6
    D
    Guest

    Borland Turbo C++ 3.0

    #include <stdio.h>

    int main() {

    int num;
    clrscr();
    num = getch();

    if (num) printf("%d", num);
    else puts("NULL");
    return 0;
    }

    run this is a *.c file

    type in any arrow key, you will get "NULL"

    I know in Turbo Pascal 7, there is a demo program called Breakout that uses arrow keys. Since borland makes it, the I/O
    might be similar. I might have to check it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrow keys as keyboard Input
    By bradleym83 in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2005, 08:39 PM
  2. Arrow key reading
    By sufthingol in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2005, 03:08 PM
  3. Interfacing with arrow keys...
    By adityakarnad in forum Game Programming
    Replies: 1
    Last Post: 08-30-2003, 10:25 PM
  4. arrow keys, ctrl and alt keys
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2002, 03:53 PM
  5. Arrow keys in console?
    By SyntaxBubble in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2002, 05:12 PM

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