This problem was first reported back in May of 2014 on the [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there] forum, but I wanted to repost it here since [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there] isn't available anymore and new users of QB64 may not be aware of it.
ON KEY(n) and ON TIMER(n) both work, but if they are used together in the same program there _will_ be unexpected results.
For some reason, after the "ON TIMER(n)" statement has been used and then "TIMER ON" activates it and then "TIMER OFF" disables it, the subroutine that it uses gets called by the "ON KEY(n)" statement instead of the subroutine that "ON KEY(n)" is supposed to use.
I am including an example program that demonstrates this odd behavior if anyone wants to check into it.
The example program works in QB4.5 without any problems, but QB64 has a problem with it.
Basically, if/after ON TIMER(n) GOSUB is used, then :
After TIMER ON (and then TIMER OFF) is used XX number of times, then
ON KEY(n) will use the ON TIMER(n) subroutine XX number of times before
it starts to use it's own subroutine.
I'm currently running Windows XP Home Edition SP3 (using QB64GL version 1.2) and I've also tested this as far back as SDL version 0.954 and it has the same problem.
As I recall, back in 2014, the verdict was that there IS a problem BUT the section of code that handles these events was written by Galleon and he would have to check into it himself.
'ON TIMER will be used to erase a message
'five seconds after it has been printed.
'
'Set up "K" to use the ON KEY(n) statement.
'
KEY 18, CHR$(&H60) + CHR$(&H25) '<NUMLOCK> + <CAPSLOCK> + <K>
PRINT " THIS IS USED TO DEMONSTRATE A PROBLEM THAT EXISTS IN QB64 WHEN" PRINT " 'ON TIMER' AND 'ON KEY' ARE USED TOGETHER IN THE SAME PROGRAM." PRINT " Press 'T' for a message to appear and a 5 second TIMER cycle to start." PRINT " After 5 seconds the message is replaced and a counter will be updated." PRINT " Do this 2 or 3 times and wait for the counter to update each time." PRINT " Next, press 'K' and a differnt message and counter SHOULD appear..." PRINT " ...but the counter for the timer routine counts up INSTEAD." PRINT " This will happen for as many times as 'T' was pressed earlier," PRINT " and THEN the correct subroutine for 'ON KEY' will be used." PRINT " The steps above can be repeated without restarting the program." PRINT " This demo works without any problems in QB4.5 but not in QB64."
PRINT "Press 'T' to use the ON TIMER subroutine." PRINT "Press 'K' to use the ON KEY subroutine." PRINT "Press 'Q' to quit."
REM ----- (SUBROUTINE
) -- Display a message
and turn
on the
timer.
'
PRINT " --> The TIMER ON command has just been used. ";
PRINT " After 5 seconds the counter will be updated ";
PRINT " and the TIMER OFF command will be used. ";
REM -- Quit this program.
'
'Disable the TIMER and erase the message after 5 seconds is up.
'
TIMERnum = TIMERnum + 1
PRINT "The TIMER OFF command was just used ---------> this counter ="; TIMERnum
'
6000 KEYnum = KEYnum + 1
PRINT "You are in the ON KEY subroutine ------------> this counter ="; KEYnum
[ You are not allowed to view this attachment ]