Simple Question

This is a discussion on Simple Question within the C++ Programming forums, part of the General Programming Boards category; I need to know if there is a function, or method of finding out if a key is pressed and ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    8

    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
    Posts
    2,572
    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.

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

    some entropy with that sink? entropysink.com

    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
    Location
    Ontario, Canada
    Posts
    2,718
    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
    Posts
    32
    Code:
    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, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  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

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