Thread: Checking to see what is being pressed on the Keyboard

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    22

    Checking to see what is being pressed on the Keyboard

    I am an amateur programmer fluent in QBasic and trying to convert to C/C++. In QBasic there is something called INKEY$ which returns the code of whatever is being pressed on the keyboard at that particualer moment that the line of code runs. Is there something like this in C++? Right now I'm using a Borland compiler but I'll have Visual C++ soon. I couldn't seem to find anything on the FAQ board, the tutorials, or even in the couple of books I have. Please help, as I feel this was one of the most basic lessons of QBasic, and I find it very useful. =P

    -Joe

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well, you could do it this way, but you'll need to include conio.h

    Code:
    int letter = 0;
    
    if(kbhit())             //Was a key pressed?
    {
        letter = getch();  //Get the value of the key that was pressed
    
        //Do whatever with it
        switch(letter)
        {
             case 0:
             case 1:
             //....
        }
    }

  3. #3
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Hey sup,

    Also, you can either use getch() or getche()
    getch() will return the character you type
    getche() does the same but also shows the character typed on the screen

    Rgrds,
    Marc
    No matter how much you know, you NEVER know enough.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    22
    thank you! I knew about kbhit(), but i was confused, at first I thought it was just like QBasic's INKEY$, but now I see that it is simply either zero or nonzero... and getch()... well, that still confuses me a little, but I think I can figure it out from here.

    Thanks a lot!

  5. #5
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    getch is for "get char". so if you say key = _getch(); you can print out key to see what value each key has.

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I'm pretty sure they're non-standard though. There are many standard ways of doing this... http://cboard.cprogramming.com/images/top_search.gif

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    22

    Lightbulb

    Hey! I just got this great idea to make things easier for myself and other former QBasic proggies. I made a function making use of kbhit() and getch() that just returns the ASCII code of whatever is being pressed whenever you want, and called it inkey() just to make it easier for myself. =D I have yet to actually try it, but it SHOULD work. Of course you'd need conio.h.

    Code:
    int inkey()
    {
    	if (kbhit()!=0)
    	{
    		return getch();
    	}
    	else
    	{
    		return 0;
    	}
    }
    Eh? Eh?

  8. #8
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    if (kbhit()!=0)

    You don't need to have the !=0 thing. C automatically interprets a zero value as false and a nonzero value as true.

    So, you could just use

    if(kbhit())

    Which is a tad clearer imo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Wich key is pressed on the keyboard
    By Coding in forum Windows Programming
    Replies: 0
    Last Post: 03-20-2008, 02:20 PM
  4. Checking to see if key is pressed
    By ki113r in forum C Programming
    Replies: 6
    Last Post: 08-28-2007, 09:23 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM