checking for input without pausing loops
Hy everyone !
I am currently learning c++; and in one of my programs i need the user's input (using key events and virtual keycodes) to be able to interupt a loop at anytime, without putting the loop on hold ( at least visibly) to check for input.
I need to do it without to use of a screenbuffer ;
I have tried just about everything i could thing of but i can't seem to find a way to achieve this.
thanks in advance for any help !
[edited by - ckaosatom on February 13, 2003 8:26:53 PM]
Just trying to get it done !http://ckaosdevblog.blogspot.com/
This will only help if you are programming for windows, Not WIN32, this will work in the console.
first you need to include windows.h
first you need to include windows.h
#include <windows.h>//This function reads in the datachar GetData(){ HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);//You need this to do the windows input functions char chUserInput; //The user''s input if(WAIT_TIMEOUT == WaitForSingleObject(hInput, 20)//Wait 20 milliseconds if no user input return leave functions return chUserInput = ''`'';//Make this something you don''t use like the tilde key //Now do what you been doing with your input system }//end of GetData
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Thanks for the quick and usefull help !
Console all the way for now
But there are a few thing i'm not sure i understand in you explanation:so i'll just rephrase to check if i got it right
- the 20 milisecond trick, wich would be too short a time to actually allow for input, is still usable due to the încredible speed at which one loop is executed and the "user input check", is almost like an operation constantly running as a 'background task" ?
i left out the begining of the code : so i would just have to include "your code" at the begining of the while loop ?
main()
{
houtput= GetStdHandle(STD_OUTPUT_HANDLE);
hinput=GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hinput,ENABLE_PROCESSED_INPUT);
SetConsoleCursorPosition(houtput,curspos);
while (segPos.X>0 && segPos.Y {
cout<<"blaah";//just a test, instead of the funtion beeing called here
if (inputrec.Event.KeyEvent.wVirtualKeyCode== VK_RIGHT )
{
blah blah //breaks the loop if true
}
ReadConsoleInput(hinput, &inputrec, 1, &events);
}
}
[edited by - ckaosatom on February 13, 2003 11:43:10 PM]
[edited by - ckaosatom on February 13, 2003 11:45:31 PM]
Console all the way for now
But there are a few thing i'm not sure i understand in you explanation:so i'll just rephrase to check if i got it right
- the 20 milisecond trick, wich would be too short a time to actually allow for input, is still usable due to the încredible speed at which one loop is executed and the "user input check", is almost like an operation constantly running as a 'background task" ?
i left out the begining of the code : so i would just have to include "your code" at the begining of the while loop ?
main()
{
houtput= GetStdHandle(STD_OUTPUT_HANDLE);
hinput=GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(hinput,ENABLE_PROCESSED_INPUT);
SetConsoleCursorPosition(houtput,curspos);
while (segPos.X>0 && segPos.Y {
cout<<"blaah";//just a test, instead of the funtion beeing called here
if (inputrec.Event.KeyEvent.wVirtualKeyCode== VK_RIGHT )
{
blah blah //breaks the loop if true
}
ReadConsoleInput(hinput, &inputrec, 1, &events);
}
}
[edited by - ckaosatom on February 13, 2003 11:43:10 PM]
[edited by - ckaosatom on February 13, 2003 11:45:31 PM]
Just trying to get it done !http://ckaosdevblog.blogspot.com/
What I would do is make your code that gets the user's input its own function and place:
if(WAIT_TIMEOUT == WaitForSingleObject(hInput, 20);
return '`';
Right above ReadConsoleInput
And no I made a few games that ran on 20 milliseconds for that worked like a charm. Remember its going through that function 50 times a second not bad.
Jeff D
A little show on how I would do it:
[edited by - Jeff D on February 14, 2003 1:19:51 AM]
if(WAIT_TIMEOUT == WaitForSingleObject(hInput, 20);
return '`';
Right above ReadConsoleInput
And no I made a few games that ran on 20 milliseconds for that worked like a charm. Remember its going through that function 50 times a second not bad.
Jeff D
A little show on how I would do it:
char GetUserInput(){ FlushConsoleInputBuffer(hInput) if(WAIT_TIMEOUT == WaitForSingleObject(hInput, 20); return '`'; //All your input stuff goes here ReadConsoleInput() }while(segPos.X>0 && segPos.Y){ UserInput = GetUserInput() //Then Check and do whatever you want to do here }A line I use still today is after findong out that the user pressed a key all I did was:return InputRecord.Event.KeyEvent.wVirtualKeyCode;
[edited by - Jeff D on February 14, 2003 1:19:51 AM]
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
just got home and tried your suggestions : it works perfectly ! Thanks again for your help, it was really helpfull!
Just trying to get it done !http://ckaosdevblog.blogspot.com/
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement