Thread: Keystroke Detection

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    28

    Keystroke Detection

    How do I detect keystrokes that aren't characters, such as cursor keys or shift keys?
    ---Rainer
    Digital pimp, hard at work

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How do I detect keystrokes that aren't characters, such as cursor keys or shift keys?
    It depends on your operating system. Windows gives you a nice way to detect keystrokes with virtual keys. Or you could just grab the scancodes and detect keystrokes that way:
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int keycode ( bool& extended )
    {
      int c;
    
      c = getch();
      if ( c == 0 || c == 224 ) {
        extended = true;
        c = 256 + getch();
      }
    
      return c;
    }
    
    int main()
    {
      int c;
      bool extended;
    
      while ( ( c = keycode ( extended ) ) != 0x1B ) {
        if ( extended )
          cout<<"Extended: ";
        else
          cout<<"Not Extended: ";
        cout<< c <<endl;
      }
    }
    Naturally, this solution requires you to be able to get raw input such as with getch.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    28
    thanks for the help...i guess
    ---Rainer
    Digital pimp, hard at work

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >...i guess
    If it wasn't any help you can clarify your question, such as what compiler/platform you use and why my answer was not helpful. That way I have a better idea of exactly what you want and can direct you toward a solution more accurately.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    28
    all i need to know (for now) is how to use this code to detect up arrow, down arrow, and enter.

    EDIT:
    my compiler: djgpp with rhide IDE
    my os: windows 98 (the program is a dos program, though)
    Last edited by Rainer; 01-19-2004 at 05:50 PM.
    ---Rainer
    Digital pimp, hard at work

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >all i need to know (for now) is how to use this code to detect up arrow, down arrow, and enter.
    Use the program I gave you. When it runs, hit the up arrow, then the down arrow, then enter. Write down the values that are printed, those are the scancodes that you can use to detect the keystrokes. Then hit esc to end the program.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    28
    I see... you used int c, instead of char c. I didn't notice that before.
    ---Rainer
    Digital pimp, hard at work

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    I find the following functions very useful for things like that.

    NB: 1. Check an ASCII table to see a listing of these characters.
    2. You need to use the ctype.h library

    iscntrl() - returns true if it is a cntrl character
    The ASCII control characters are all the values below space (decimal 32) and includes the delete character.

    isspace() - returns true if it is a whitespace
    Checks for whitespace (horizontal tab, line feed, vertical tab, form feed and carriage return)

    isgraph() - returns true if it is a graphic character
    Checks for a graphic character. These are all of the ASCII values greater than the space (decimal 32) and less than the delete (decimal 127)

    Hope these helps

  9. #9
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    i believe anything on the keyboard has an ascii value
    see the post from prelude.
    you can use that value and a getch() to write a function that reads and tests the interpreted value entered.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  2. matrixes for collision detection
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 11-09-2002, 10:31 PM
  3. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM
  4. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 01:31 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM