GetKeyState equivalent function ...

This is a discussion on GetKeyState equivalent function ... within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm looking for a function which will return the value of the key pressed .... I tried using GetKeyState, ...

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

    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,066
    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,262
    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,066
    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
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    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,066
    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
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    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,066
    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, 12:49 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10: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, 12:03 PM

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