Thread: Do arrow keys have two ascii values

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Do arrow keys have two ascii values

    When we press arrow key.Then do we get two ascii values corresponding to one arrow key???
    Code:
    int main()
    {
    ch=getch();cout<<ch;
    ch+=getch();
    cout<<ch;
    return(0);
    }
    If we enter any arrow key in program from above code.We'll get 0 once and scan code in next getch().Why???

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    Its giving 0 because you are returing 0, remove the bracets. It should just read 'return 0;'

    As to the answer to your question, im not sure, sorry.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If your keyboard works with ANSI escape codes, then an arrow key is actually input as a set of characters.

    For more information on ANSI escape codes, have a look here.

    If your system doesn't work with ANSI escape codes, keyboard input may occur in a variety of means. For example, under windows, an arrow key results in a specific windows message being sent to your application (which routes the event to the affected window). If you're working with that though, you will not be using functions like getch().

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The short version is that arrow keys don't have an ASCII value at all. Only printable characters and a few control characters (newline, bell ring, page feed, ...) have ASCII values.
    You can look up an ASCII table if you want.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    getch() and other keyboard input functions return two integers for all special keys, such as arrow keys and function keys (F1, F2, ... F10). When getch() returns 0 or 224 as the first value, then a special key was pressed and the program must call getch() again to get the key's integer value. That integer value duplicates one of the standard ascii values of other keys and your program may have to should encode it to make it distingushable from normal ascii keys. I normally make it a negative value, but other programmers may add 255 to it.
    Code:
    int key;
    key = getch();
    if(key == 0 || key == 224)
    {
       key = getch() + 255;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  2. arrow keys in arrays
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 03-06-2006, 01:26 PM
  3. Menu system (Using the arrow keys)
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2001, 04:31 PM
  4. Arrow Keys and Such
    By Thantos in forum Game Programming
    Replies: 5
    Last Post: 10-25-2001, 05:40 PM
  5. Using Arrow Keys in Windows
    By Xterria in forum Game Programming
    Replies: 4
    Last Post: 10-22-2001, 08:21 PM