Funny, an "anti OPTION _EXPLICIT" switch, I like it :)
We’re trying to get everything ready for the next stable release of QB64, and this is one of the features that “Steve’s Repo” has that I definitely wanted to get into QB64 by the next release. ;D
Here is my list of items I'd love to still see addressed prior to the release of the next stable release:
Issue #1: SLEEP command with a specified sleep time does not work in console.
Sample code:
NOTE: Run the code below twice:
First Run: Run it as is and note that the sleep command works fine.
Second Run: Uncomment the lines that are currently commented out to run the program in a console. Note that the program will never advance past the sleep command until a key is pressed.
---------------------------------------
' $CONSOLE:ONLY
' _DEST _CONSOLE: _SOURCE _CONSOLE
PRINT "If the SLEEP command is working, then the program should continue"
PRINT "in 5 seconds without any need to press a key...."
SLEEP 5
PRINT "If we arrive here <without pressing a key> then SLEEP is working,"
PRINT "otherwise it is not working."
---------------------------------------
PRINT "TimeStart:"; TIME$
FakeSleep 2
PRINT "TimeStop :"; TIME$
SUB FakeSleep (Time AS _FLOAT)
DIM ExitTime AS _FLOAT
ExitTime = TIMER + Time
DO
a$ = INPUT$(1)
PRINT "Time in Loop: "; TIMER - ExitTime
LOOP UNTIL TIMER > ExitTime
END SUB
It's not a matter of setting a timer; it's a matter of ReadConsoleInput not responding until an event occurs.
It's a lot like INPUT is in QB64 -- no matter what time event you have going, once the program hits INPUT, it pauses until that input event is over.
SLEEP is a "Pause until a key is hit" type routine, and with the console, ReadConsoleInput is a command that "pauses everything until an input event occurs."
If we exit ReadConsoleInput, we're going to exit SLEEP as well.
// con_in is a handle to the console input buffer
// wait is the maximum number of milliseconds to wait for a key, mouse or other input event
if (WaitForSingleObject(con_in, wait) == WAIT_OBJECT_0) {
// there is data, read it - could be keyboard, mouse or other console events
} else {
// no events on con_in, or the wait period expired, go do something else for a while
}
I had the same problem (needing a non-blocking ReadConsoleInput()) in a Console program I was writing. The solutions was the WaitForSingleObject() function. For instance:Code: [Select]// con_in is a handle to the console input buffer
// wait is the maximum number of milliseconds to wait for a key, mouse or other input event
if (WaitForSingleObject(con_in, wait) == WAIT_OBJECT_0) {
// there is data, read it - could be keyboard, mouse or other console events
} else {
// no events on con_in, or the wait period expired, go do something else for a while
}
There are various other returns from WaitForSingleObject(). See the Win32 documentation for details.
$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE
PRINT "Foo"
t# = TIMER
SLEEP 2
PRINT USING "##.### seconds"; TIMER - t#
$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE
PRINT "Foo"
SLEEP
SLEEP
_DELAY .2 //time to lift the finger off the key we hit
_KEYCLEAR //and then clear the buffer
END //so the up key doesn't break the END event.
Using your first sample code there is a problem. To best illustrate, I'm adding a single line of code at the end so that you end up with this:
$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE
PRINT "Foo"
t# = TIMER
SLEEP 2
PRINT USING "##.### seconds"; TIMER - t#
INPUT "Enter some text: ", Text$
Using your first sample code there is a problem. To best illustrate, I'm adding a single line of code at the end so that you end up with this:
$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE
PRINT "Foo"
t# = TIMER
SLEEP 2
PRINT USING "##.### seconds"; TIMER - t#
INPUT "Enter some text: ", Text$
If allowed to run with no user input, it runs just fine. However, if you press a key during the 2 second pause the program advances as expected without waiting for the 2 seconds, but when it reaches the input statement note that as you try to type you see no characters displayed. You have to press ENTER twice to get past the input statement.
I experience the same problem with the second sample where I again add the input statement:
$CONSOLE:ONLY
_DEST _CONSOLE: _SOURCE _CONSOLE
PRINT "Foo"
SLEEP
INPUT "Enter some text: ", Text$
fdwMode = ENABLE_EXTENDED_FLAGS;
SetConsoleMode(hStdin, fdwMode);
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
SetConsoleMode(hStdin, fdwMode);
GetConsoleMode(hStdin, (LPDWORD)&dwMode);
fdwMode = ENABLE_EXTENDED_FLAGS;
SetConsoleMode(hStdin, fdwMode);
fdwMode = dwMode | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
SetConsoleMode(hStdin, fdwMode);
Sent a reply.
PR Merged.Thanks Fellippe,
Question: Does the changes affect LINE INPUT as well?
Pushed an option into the IDE today so you can toggle to Ignore Warnings on and off. (Variable not defined, or CONST name already in use warnings.)
DIM foo AS LONG
To add BIN$ to QB64 it might have to be called _BIN$ and who wants that?