How do I NOT wait for a keypress?

This is a discussion on How do I NOT wait for a keypress? within the C++ Programming forums, part of the General Programming Boards category; Ok, i really do want to wait for a keypress. But the difference is that I want it to time ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    How do I NOT wait for a keypress?

    Ok, i really do want to wait for a keypress. But the difference is that I want it to time out after a predetermined amount of time.

    The only way I could imagine doing this (without using 2 programs and pipes) is by using a loop. Howeve, putting cin.anything() will wait for input which is not exactly what I want.

    Here, let me just show you some code. It will explain it a lot better than I can.

    (this code is for illustration only. my question is not a concern with THIS code)
    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        time_t start, end;
        
        start = time(0);
    
        while(  !cin.get()   &&   (difftime(end, start) <= 100)    )
           end = time(0);
        
        if(  difftime(end, start) <= 100  )
                userAbsent_proceedWithVeryLongProcess()
       
        return 0;
    }
    any ideas?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    Registered User r1ck0r's Avatar
    Join Date
    Apr 2005
    Location
    UK
    Posts
    30

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Check of kbhit() and getch() in conio.h... (NOT a std lib)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    I need something that works on *nix
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I'de suggest a multi-threaded approach. I've done next to nothing in *nix development, but I'm sure there are some friendly libraries that are readily available.

    Basis synopsis of what you want:

    Code:
    1) Create Timer and Input functions
    2) Create Two Threads that will call these functions.
    3) In your main function
        a. Execute the timer thread first, and the input thread second.
        b. Pass a state variable that tells whether a key down or key
              up event has been triggered.
    4) In your timer function
        a. Check the state variable passed to the function
        b. Increase the global timer object by such and such amount if
              a key up event has been registered.
    That's the only advice I can give you. I'm not sure what sort of technical details you _can_ use on input streams in this instance, but I'm sure there's something alot better than cin at this point.

    I would think that we would all agree on the multi-threaded approach since there's no other way that I know of (other than piping 2 processes), to call two functions that are executed at _virtually_ the same time(e.g your timer accumulator function and your input function) besides multi-threading.
    Last edited by Tronic; 04-13-2005 at 02:01 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    My god people... 2 processes with IPC?.. multi-threading? Thats some serious overkill. use select() with your specified timeout and only read from stdin if select indicates the file descriptor has changes status!

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Hahahaha, or that..:P That sounds less heavy, go with that lol.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Perspective
    My god people... 2 processes with IPC?.. multi-threading? Thats some serious overkill. use select() with your specified timeout and only read from stdin if select indicates the file descriptor has changes status!
    where in the world might i find select()?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    rock on, thanks
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why wait cursor still turning on?
    By Opariti in forum Windows Programming
    Replies: 0
    Last Post: 05-22-2009, 03:28 AM
  2. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  3. How do I get my program to wait for a keypress?
    By redche in forum C Programming
    Replies: 6
    Last Post: 05-07-2007, 08:31 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. anybody with experience using the wait() function?
    By flawildcat in forum C Programming
    Replies: 7
    Last Post: 04-22-2002, 03:43 PM

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