help with ReadConsoleInput()

This is a discussion on help with ReadConsoleInput() within the Windows Programming forums, part of the Platform Specific Boards category; Hi. I'm writing a console program that uses ReadConsoleInput() to receive input from a mouseclick. My problem is that the ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    6

    help with ReadConsoleInput()

    Hi. I'm writing a console program that uses ReadConsoleInput() to receive input from a mouseclick. My problem is that the program gets "stuck" at ReadConsoleInput() when there are no "events". I need some sort of a timeout function that gets out of ReadConsoleInput() after a certain amount of no event time, say 5 seconds. Here's how my program is like:

    Code:
    for (int i=0; i<=30; i++){
    
    ...
    
    while (1){
    
    ReadConsoleInput(hStdin,irInBuf,128, &cNumRead);
    
    if (irInBuf[cNumRead-1].Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED ) {
    ...
    break;
    }
    
    }
    
    ...
    
    }
    I tried using WaitForSingleObject(hStdin, 5000L) and putting it after the line ReadConsoleInput(hStdin,irInBuf,128, &cNumRead);
    But that only seem to work for the first time around the for loop. I would really appreciate any help I can get on this.

    Thank you.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You're on the right track. You can test the console handle before calling ReadConsoleInput:
    Code:
    if (WaitForSingleObject(hStdin, 5000) == WAIT_OBJECT_0)
    {
       // Input (keyboard and/or mouse, depending on console mode) is available...
       ReadConsoleInput(hStdin,irInBuf,128, &cNumRead);
    }
    else
    {
       // WaitForSingleObject returned WAIT_TIMEOUT (or WAIT_FAILED if hStdin is not a valid handle)...
       // Handle the timeout...
    }
    Alternatively, you can use the GetNumberOfConsoleInputEvents function to poll the console for input events.

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    This thread should be on the Windows Programming board...

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    6
    Thank you for the reply! (the program works now) And sorry about posting it on the wrong board!

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You could also use GetNumberOfConsoleInputEvents if the number of input records is zero Then you can skip whatever code you normally execute.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    A belated move to the correct forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    6

    another problem

    Hi. I'd like to get rid of a problem with this code (same as before):

    Code:
    while (1) {
    
    WaitResult = WaitForSingleObject(hStdin, 5000);	
    
    if (WaitResult == WAIT_OBJECT_0)
    {
         ReadConsoleInput(hStdin,
    		&irInBuf,
    		1,
    		&cNumRead);
    }
    
    else
        break;
    
    if (irInBuf.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED || irInBuf.Event.MouseEvent.dwButtonState == RIGHTMOST_BUTTON_PRESSED){
    ...
    						
    }
    }
    The code works fine if I don't move the mouse. My problem is that WaitForSingleObject doesn't time out after 5 seconds if I move the mouse around. Is there a way around it? I am only interested in mouseclicks, so it would be great if I can somehow disable mouse movements from being mouse events.
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ReadConsoleInput behaves bad.
    By antex in forum Windows Programming
    Replies: 2
    Last Post: 10-25-2005, 12:02 PM
  2. help with ReadConsoleInput please
    By Verbenaca in forum Windows Programming
    Replies: 1
    Last Post: 05-21-2005, 07:19 AM
  3. ReadConsoleInput Reading Multiple Records
    By manofsteel972 in forum Windows Programming
    Replies: 1
    Last Post: 10-26-2004, 10:57 PM
  4. ReadConsole() / ReadConsoleInput()
    By LuckY in forum Windows Programming
    Replies: 2
    Last Post: 02-10-2003, 11:54 AM

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