Thread: Simple Question

  1. #1
    Registered User
    Join Date
    May 2002

    Simple Question

    I need to know if there is a function, or method of finding out if a key is pressed and what the pressed key is. I have tryed some stuff with getchar() but it's not working the way I want it to work... Does anyone know of a method or a function that will let me detect if a key is pressed and what key was pressed?

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    I'm not exactly sure if this will help you...but maybe you can store the pressed character into a char variable and then type cast it as an int to give you the ASCII value.

    char c;
    cout << "enter character: ";
    cin >> c;
    cout << (int)c;
    ....or other manipulation

    some entropy with that sink?

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Ontario, Canada
    conio.h and getch()/kbhit( )

    getch( ) gives you the key pressed as soon as it is hit, not waiting for the enter key

    kbhit( ) tells you if a key has been pressed
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Jul 2002
    int getkey(void)
    	// Get ASCII code of charactor hit
    	int key = getch();
    	// ASCII code of hit is 224 - get second getch() to retrieve code of other keys e.g. arrow keys
    	if (key == 224) {
    		key = 256;			// Add 256 to this code to be able to differ from 'H' and Up arrow, etc.
    		key += getch();
    	return key;
    Use keys.h for the ASCII codes for each key, which is what this function returns.
    You'll need to add 256 to the keys like Up Arrow which are defined as 72 (same as H).
    - Tigs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM