Author Topic: ON KEY(n) is using ON TIMER(n)'s subroutine sometimes  (Read 2718 times)

0 Members and 1 Guest are viewing this topic.

Offline Stuart

  • Newbie
  • Posts: 11
    • View Profile
ON KEY(n) is using ON TIMER(n)'s subroutine sometimes
« on: December 06, 2018, 02:53:06 pm »
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.


Code: QB64: [Select]
  1.    'ON TIMER will be used to erase a message
  2.    'five seconds after it has been printed.
  3.    '
  4.    ON TIMER(5) GOSUB 5000
  5.  
  6.  
  7.  
  8.    'Set up "K" to use the ON KEY(n) statement.
  9.    '
  10.    KEY 15, CHR$(&H0) + CHR$(&H25) '<K>
  11.    ON KEY(15) GOSUB 6000
  12.    KEY 16, CHR$(&H20) + CHR$(&H25) '<NUMLOCK> + <K>
  13.    ON KEY(16) GOSUB 6000
  14.    KEY 17, CHR$(&H40) + CHR$(&H25) '<CAPSLOCK> + <K>
  15.    ON KEY(17) GOSUB 6000
  16.    KEY 18, CHR$(&H60) + CHR$(&H25) '<NUMLOCK> + <CAPSLOCK> + <K>
  17.    ON KEY(18) GOSUB 6000
  18.  
  19.  
  20.  
  21.    KEY(15) ON: KEY(16) ON: KEY(17) ON: KEY(18) ON
  22.  
  23.  
  24.  
  25.  
  26.  
  27.    SCREEN 0: WIDTH 80, 50
  28.    _FONT 14
  29.  
  30.    CLS
  31.    PRINT
  32.    PRINT
  33.    COLOR 15
  34.    PRINT "         THIS IS USED TO DEMONSTRATE A PROBLEM THAT EXISTS IN QB64 WHEN"
  35.    PRINT "         'ON TIMER' AND 'ON KEY' ARE USED TOGETHER IN THE SAME PROGRAM."
  36.    PRINT
  37.    PRINT
  38.    PRINT
  39.    COLOR 2
  40.    PRINT "    Press 'T' for a message to appear and a 5 second TIMER cycle to start."
  41.    PRINT
  42.    PRINT "    After 5 seconds the message is replaced and a counter will be updated."
  43.    PRINT
  44.    PRINT "    Do this 2 or 3 times and wait for the counter to update each time."
  45.    PRINT
  46.    PRINT
  47.    PRINT
  48.    COLOR 3
  49.    PRINT "    Next, press 'K' and a differnt message and counter SHOULD appear..."
  50.    PRINT
  51.    PRINT "    ...but the counter for the timer routine counts up INSTEAD."
  52.    PRINT
  53.    PRINT "    This will happen for as many times as 'T' was pressed earlier,"
  54.    PRINT "    and THEN the correct subroutine for 'ON KEY' will be used."
  55.    PRINT
  56.    PRINT
  57.    PRINT
  58.    COLOR 7
  59.    PRINT "        The steps above can be repeated without restarting the program."
  60.    PRINT
  61.    PRINT "        This demo works without any problems in QB4.5 but not in QB64."
  62.    PRINT
  63.    PRINT
  64.    PRINT
  65.  
  66.  
  67.  
  68.    COLOR 14
  69.    PRINT
  70.    PRINT
  71.    PRINT "Press 'T' to use the ON TIMER subroutine."
  72.    PRINT
  73.    PRINT "Press 'K' to use the ON KEY subroutine."
  74.    PRINT
  75.    PRINT "Press 'Q' to quit."
  76.  
  77.    LN = CSRLIN + 2
  78.  
  79.  
  80. 30 K$ = UCASE$(INKEY$)
  81.    IF K$ = "T" THEN GOSUB 100
  82.    IF K$ = "Q" THEN 200
  83.  
  84.    _LIMIT 10
  85.    GOTO 30
  86.  
  87.  
  88.  
  89.  
  90.     REM ----- (SUBROUTINE) -- Display a message and turn on the timer.
  91.     '
  92. 100 TIMER ON
  93.  
  94.     LOCATE LN, 1
  95.     COLOR 15, 4
  96.     PRINT " --> The TIMER ON command has just been used. ";
  97.     PRINT "                                              ";
  98.     PRINT " After 5 seconds the counter will be updated  ";
  99.     PRINT " and the TIMER OFF command will be used.      ";
  100.     COLOR 7, 0
  101.  
  102.     RETURN
  103.  
  104.  
  105.  
  106.  
  107.     REM -- Quit this program.
  108. 200 COLOR 7, 0: CLS: SYSTEM
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.      REM ----- (SUBROUTINE) -- for ON TIMER.
  119.      '
  120.      'Disable the TIMER and erase the message after 5 seconds is up.
  121.      '
  122. 5000 TIMER OFF
  123.  
  124.      TIMERnum = TIMERnum + 1
  125.      IF C = 10 THEN C = 15 ELSE C = 10
  126.      COLOR C
  127.  
  128.      LOCATE LN, 1
  129.      PRINT TAB(80); " "
  130.      PRINT "The TIMER OFF command was just used ---------> this counter ="; TIMERnum
  131.      PRINT TAB(80); " "
  132.      PRINT TAB(80); " "
  133.  
  134.      SOUND 1000, .4
  135.      RETURN
  136.  
  137.  
  138.  
  139.      REM ----- (SUBROUTINE) -- for ON KEY.
  140.      '
  141. 6000 KEYnum = KEYnum + 1
  142.  
  143.      IF CC = 11 THEN CC = 15 ELSE CC = 11
  144.      COLOR CC
  145.  
  146.      LOCATE LN + 5, 1
  147.      PRINT "You are in the ON KEY subroutine ------------> this counter ="; KEYnum
  148.  
  149.      SOUND 500, .4
  150.      RETURN
  151.  
  152.  

  [ You are not allowed to view this attachment ]  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: ON KEY(n) is using ON TIMER(n)'s subroutine sometimes
« Reply #1 on: December 09, 2018, 07:22:52 pm »
HI
running your code I get these results:

each time  I press  T then pressing K at the first time it increases T counter and then the K counter each time I repeat this sequence of Keys...
it seems like the offset of subroutine Timer is stored and it lasts in memory when the control passes to the On Key event...
as the first time there is no adjournment of Offset for ON event routines.


Programming isn't difficult, only it's  consuming time and coffee