Thread: GetKeyState equivalent function ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    GetKeyState equivalent function ...

    Hi, I'm looking for a function which will return the value of the key pressed ....

    I tried using GetKeyState, and other functions like that, but they depend upon the programmer knowing which key is to be pressed.

    getch(); is an obvious choice, but it's not a good one because of it depends on a key being pressed, but I want the possibility of one not being pressed:-

    Code:
    start time = current time
    while ( some parameters )
    {
      if ( key pressed )
      {
         start time = current time
         variable = button pressed
      }
      else if ( time between start time and this time > 5 seconds )
      {
         print something
      }
    }

    this is the most sudo code It think I've ever seen, never mind written, but it gets about what I want to do. I tried using getch() in the first if statement, but it didn't work . bah!!!

    I researched and found WM_KEYDOWN/UP, but they have been somewhat more problematic for my code than I'd have liked - in that I don't have a clue how to use them. bah!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well it's not standard, but kbhit() allows processing to be handled while waiting for input.

    Code:
    #include <conio.h>
    #include <iostream>
    
    using namespace std;
    
    int main() {
      while (1)
        if(kbhit())
          getch();
    }
    Sent from my iPadŽ

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    thanks a lot!! em, what's kbhit() actually do??

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If you have a working conio.h, then compile the attached code. It's some code I wrote (part wrote, the gotoxy and window size I got off the net) and it uses kbhit().

    It's a trivia game that works off of a keypress. Note that while it waits for a keypress, the timer in the top right continues to count down. Also note that this is old code and I know it sucks, so don't rag on it. And by the way, hard doesn't work and the end of easy is the same questions as the end of normal.
    Sent from my iPadŽ

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    kbhit() checks if there's anything in stdin; returns true if stdin isn't empty and false otherwise.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    The conio functions do not interface with standard I/O streams.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, but unfortunately, there are few other options and none of them are as simple. I really hope the next big update to the C++ standard comes with standard versions of conio.h's functions and a standard gotoxy().

    Any older programmers know the likeliness of that happening?
    Sent from my iPadŽ

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> Yes, but unfortunately, there are few other options and none of them are as simple.

    I was just pointing out that kbhit doesn't read from stdin, I don't know anything about further iostream development. Since he noted windows, the OP could also use things like ::WaitForSingleObject(..) and ::ReadConsoleInput(..) to check if the user pressed a key and then read it. I don't think you can use messages because these things are handled by csrss.exe, and I could very well be wrong.

  9. #9
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I am not very familiar with the low-level workings so I used stdin to describe the input buffer. If stdin is not used how does the conio.h functions access the input buffer, some cryptic assembly lines?

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quoi? I must have understood what you mean by "not interface with standard I/O streams".

    kbhit() abosolutely checks the keyboard buffer (otherwise known as the file descriptor - stdin) to see if there is a character in it. Were you trying to say stdin was an I/O stream?
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM