Thread: ASCII Keypress

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    Question ASCII Keypress

    Hello!

    I'm wondering if there is a piece of code that returns the ascii value of the key that is pressed.
    Cin and getc won't catch arrow keys or function keys.

    Any help appreciated!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What OS are you using?

    There are a lot of ways and I believe no standard one.
    For example, getch() I think it is on curses.h or conio.h or ncurses.h
    Don't remember exactly how to use it (I think it returns 0 for arrows or function key and then you have to getch() once again to determine which arrow or function key).

    Look here for example

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Use int a = getchar(), b = getchar(), c = getchar();. The arrow keys return three values, 27, 91, and 65, for up, 66, for down, 67, for right, or 68 for left. So, to manipulate this, do something like...

    Code:
    if ((a == 27) && (b == 91))
    {
         switch (c)
         {
              case 65 :
                   cout << "You pressed up." << endl;
                   break;
              case 66 :
                   cout << "You pressed down." << endl;
                   break;
              case 67 :
                   cout << "You pressed right." << endl;
                   break;
              case 68 :
                   cout << "You pressed left." << endl;
                   break;
              default :
                   break;
         }
    }
    I'm sorry if this is false, I'm using a GCC-less Mac. The above is entirely from memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-06-2009, 11:20 PM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM

Tags for this Thread