Author Topic: Click  (Read 4530 times)

0 Members and 1 Guest are viewing this topic.

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Click
« on: November 01, 2021, 06:29:18 pm »
Hello everyone,

the question of controlling the click and double click of the mouse I had solved with the code that follows.
The function is called to capture the double click.

It's probably not a great code either but it worked until version 1.6 of QB64.
I upgraded to version 2.1 of QB64 but the compiler stops and reports this error (see image).

Any ideas on this?

Code: QB64: [Select]
  1. FUNCTION Click%
  2.         DO
  3.                 provj$=inkey$
  4.                 provxxw& = _KEYHIT
  5.                
  6.                 IF _EXIT THEN call Fine  'click sulla X della finestra
  7.                 WHILE _MOUSEINPUT: WEND
  8.         mb = _MOUSEBUTTON(1) OR _MOUSEBUTTON(2) OR _MOUSEBUTTON(3)  '-1=preme / 0=solleva
  9.                 IF mb AND oldMouse = 0 THEN
  10.                         passato=0
  11.                         IF plusClick%(timer) = 2 THEN
  12.                                 Click%=2
  13.                         else
  14.                                 Click%=1
  15.                         end if
  16.                 END IF
  17.                 oldMouse = mb
  18.     LOOP UNTIL Click%  or provj$<>"" or provxxw&<>0 or _MOUSEWHEEL <> 0
  19.        
  20. FUNCTION plusClick%(t1#)
  21. static passato,t2#
  22.         DO
  23.                 provj$=inkey$
  24.                 provxxw& = _KEYHIT
  25.  
  26.         WHILE _MOUSEINPUT: WEND
  27.         mb2 = _MOUSEBUTTON(1) OR _MOUSEBUTTON(2) OR _MOUSEBUTTON(3)  '-1=preme / 0=solleva
  28.         IF mb2=0 THEN  'rilasciato
  29.                         if timer-t2# <.3 then
  30.                                 t2#=timer
  31.                                 t1#=timer
  32.                                 passato=0
  33.                                 plusClick%=2
  34.                                 exit FUNCTION
  35.                         end     if
  36.                         if timer-t1#<.3 then
  37.                                 if passato=0 then  'primo rilascio entro il tempo
  38.                                         passato=38
  39.                                         t1#=timer
  40.                                         t2#=timer
  41.                                         plusClick%=1
  42.                                 elseif passato=38 then  'rilascio ignorato
  43.                                         passato=0
  44.                                         t1#=timer
  45.                                         t2#=timer
  46.                                         plusClick%=1
  47.                                 end if
  48.                         else  'tempo passato in ogni caso
  49.                                 t1#=timer
  50.                                 t2#=timer
  51.                                 passato=0
  52.                                 plusClick%=1
  53.                         end if
  54.                 END IF
  55.                
  56.     LOOP until plusClick% or provj$<>"" or provxxw&<>0 or _MOUSEWHEEL <> 0
  57.  

Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Click
« Reply #1 on: November 01, 2021, 06:44:07 pm »
Missing elements of program, the IDE is red lining at line 6 because subroutine, fine, not present.

QB64 Version 2.1 does not exist, maybe you mean 2.01?

Ah! It's line 57 that is probably hanging you up, use another variable name not the function name, plusClick%, for this line:
Code: QB64: [Select]
  1. LOOP until plusClick% or provj$<>"" or provxxw&<>0 or _MOUSEWHEEL <> 0
  2.  

I'd say use pC% = such and such until after loop and then say:
plusClick% = pC% before leaving function to set the Function value.

Like this:
Code: QB64: [Select]
  1. Function Click%
  2.     Do
  3.         provj$ = InKey$
  4.         provxxw& = _KeyHit
  5.  
  6.         ' If _Exit Then call Fine  'click sulla X della finestra   <<<< missing fine subroutine
  7.         While _MouseInput: Wend
  8.         mb = _MouseButton(1) Or _MouseButton(2) Or _MouseButton(3) '-1=preme / 0=solleva
  9.         If mb And oldMouse = 0 Then
  10.             passato = 0
  11.             If plusClick%(Timer) = 2 Then
  12.                 Click% = 2
  13.             Else
  14.                 Click% = 1
  15.             End If
  16.         End If
  17.         oldMouse = mb
  18.     Loop Until Click% Or provj$ <> "" Or provxxw& <> 0 Or _MouseWheel <> 0
  19.  
  20. Function plusClick% (t1#)
  21.     Static passato, t2#
  22.     Do
  23.         provj$ = InKey$
  24.         provxxw& = _KeyHit
  25.  
  26.         While _MouseInput: Wend
  27.         mb2 = _MouseButton(1) Or _MouseButton(2) Or _MouseButton(3) '-1=preme / 0=solleva
  28.         If mb2 = 0 Then 'rilasciato
  29.             If Timer - t2# < .3 Then
  30.                 t2# = Timer
  31.                 t1# = Timer
  32.                 passato = 0
  33.                 pC% = 2
  34.                 Exit Function
  35.             End If
  36.             If Timer - t1# < .3 Then
  37.                 If passato = 0 Then 'primo rilascio entro il tempo
  38.                     passato = 38
  39.                     t1# = Timer
  40.                     t2# = Timer
  41.                     pC% = 1
  42.                 ElseIf passato = 38 Then 'rilascio ignorato
  43.                     passato = 0
  44.                     t1# = Timer
  45.                     t2# = Timer
  46.                     pC% = 1
  47.                 End If
  48.             Else 'tempo passato in ogni caso
  49.                 t1# = Timer
  50.                 t2# = Timer
  51.                 passato = 0
  52.                 pC% = 1
  53.             End If
  54.         End If
  55.  
  56.     Loop Until pC% Or provj$ <> "" Or provxxw& <> 0 Or _MouseWheel <> 0
  57.     plusClick% = pC%
  58.  
  59.  
  60.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Click
« Reply #2 on: November 01, 2021, 07:16:19 pm »
Oh you might need to fix this too, same way:
Code: QB64: [Select]
  1. Function Click%
  2.     Do
  3.         provj$ = InKey$
  4.         provxxw& = _KeyHit
  5.  
  6.         ' If _Exit Then call Fine  'click sulla X della finestra   <<<< missing fine subroutine
  7.         While _MouseInput: Wend
  8.         mb = _MouseButton(1) Or _MouseButton(2) Or _MouseButton(3) '-1=preme / 0=solleva
  9.         If mb And oldMouse = 0 Then
  10.             passato = 0
  11.             If plusClick%(Timer) = 2 Then
  12.                 Cl% = 2
  13.             Else
  14.                 Cl% = 1
  15.             End If
  16.         End If
  17.         oldMouse = mb
  18.     Loop Until Cl% Or provj$ <> "" Or provxxw& <> 0 Or _MouseWheel <> 0
  19.     Click% = Cl%
  20.  
  21.  
  22.  

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Click
« Reply #3 on: November 01, 2021, 07:50:42 pm »
Missing elements of program, the IDE is red lining at line 6 because subroutine, fine, not present.

QB64 Version 2.1 does not exist, maybe you mean 2.01?

Technically neither does 2.01, as its actually 2.0.1
But oddly enough if you look at his screen cap it does say V2.1? 🤷‍♂️
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Click
« Reply #4 on: November 01, 2021, 08:03:49 pm »
Well I got all the digits right ;-))
  [ You are not allowed to view this attachment ]  

BTW my screen says v2.0
« Last Edit: November 01, 2021, 08:39:51 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Click
« Reply #5 on: November 01, 2021, 11:41:59 pm »
I use a timer for detecting double clicks, too. https://www.qb64.org/forum/index.php?topic=4262.msg136424#msg136424

There are many ways to code this, but the #1 criteria is, does it work?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Click
« Reply #6 on: November 02, 2021, 12:29:35 am »
FUNCTION plusClick%(t1#)
.
.
...STUFF
.
.
    LOOP until plusClick% or provj$<>"" or provxxw&<>0 or _MOUSEWHEEL <> 0


The issue lies in the above.  plusClick is your function name, and the glitch where you could use it in such a manner has been fixed.  You're now trying to recursively call your routine over and over, and that no longer works.

You'll need to assign a temp variable to the code, inside the FUNCTION routine, instead of using plusClick% itself, until you get to the final assignment where plusClick = *whatever*.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: Click
« Reply #7 on: November 02, 2021, 04:31:42 am »
Thanks Pete, I will also check your interesting method.

The proposed solutions work.

In short, it is a matter of avoiding "shortcuts" and sliding routines and functions in a linear way, without jumps.



However, I confess, I am worried!

The solutions don't seem to solve what seems to be a compiler problem.

In fact, filed the problem of this post, the compilation went on BUT stops countless times showing the same error message "Incorrect number of arguments passed to function".

The error occurs on rather trivial single commands, related to the execution of functions (these, perhaps, I will have to review them considering what has been said before) but also on the simple command SWAP (inside a function)

In short... something has changed since version 1.6 and 2.0.1. (2.1 in the window).
The compiler now seems more attentive to the flow of instructions, as well as syntax, as is normal for us to be.
The problem - if there is, but I think so - is how the compiler treats the code associated with the functions.
Checking all the code inside the functions is not easy (there are many!) and above all it is not easy to understand how the compiler "reasons" now to avoid errors in the future.

Sure, I could continue with the 1.6 but it's not a good policy to ignore the updates and improvements.







Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Click
« Reply #8 on: November 02, 2021, 05:03:10 am »
Think of it as writing a function for COS...

Function COS (angle)

DO
   ...some math
LOOP UNTIL COS = whatever

Now, it's obvious here that COS is a function that we all know and love...  The LOOP UNTIL COS = whatever line is going to error out on us, obviously.  Right?  After all, we didn't tell it what angle we wanted to use with the function COS (angle).


You're doing the *exact* same thing with your custom function:

FUNCTION plusClick%(t1#)

DO
    ...STUFF
LOOP UNTIL plusClick% <> 0

plusClick is a function which wants a (t1#) passed to it.  Where's the (t1#) that you're passing to the call to the function in the LOOP UNTIL line??

You're calling the function without passing it any value for the parameter, thus the "Incorrect number of arguments passed to function" error.

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Click
« Reply #9 on: November 02, 2021, 08:16:40 am »
This one is interesting in a lot of ways.

1) Before version 2.0 the IDE allowed the parameter to be absent. I would not have expected it to miss that coding error. I guess this part of the break in function warning issued with this newer release.

2) Before version 2.0, the code, as written, would run without crashing. Now, unless the recursive function calls are fixed, as in my remake, below, the recursive calls in the Click% function cause it to crash.

3) The modified code works, but just as in the original code, it does not disallow for the reporting of a double click from more than the same button. In other words, we can get a recorded double click by clicking left then right, left then middle, or middle and right or middle and left. Also, it will report a double click if we hold a mouse button down and then release and quickly click it. This is not a traditional double click, at least not in Windows systems.

Anyway, if it helps, here is the modified code, which will run n V2.0...

Code: QB64: [Select]
  1.     PRINT Click%
  2.     _LIMIT 30
  3.  
  4. FUNCTION Click%
  5.     DO
  6.         provj$ = INKEY$
  7.         provxxw& = _KEYHIT
  8.  
  9.         'IF _EXIT THEN call Fine  'click sulla X della finestra
  10.         WHILE _MOUSEINPUT: WEND
  11.         mb = _MOUSEBUTTON(1) OR _MOUSEBUTTON(2) OR _MOUSEBUTTON(3) '-1=preme / 0=solleva
  12.         IF mb AND oldMouse = 0 THEN
  13.             passato = 0
  14.             IF plusClick%(TIMER) = 2 THEN
  15.                 Click% = 2
  16.                 EXIT DO ' <--------------- Exit loop and function here.
  17.             ELSE
  18.                 Click% = 1
  19.                 EXIT DO ' <--------------- Exit loop and function here.
  20.             END IF
  21.         END IF
  22.         oldMouse = mb
  23.     LOOP UNTIL provj$ <> "" OR provxxw& <> 0 OR _MOUSEWHEEL <> 0
  24.  
  25. FUNCTION plusClick% (t1#)
  26.  
  27.     STATIC passato, t2#
  28.     DO
  29.         provj$ = INKEY$
  30.         provxxw& = _KEYHIT
  31.  
  32.         WHILE _MOUSEINPUT: WEND
  33.         mb2 = _MOUSEBUTTON(1) OR _MOUSEBUTTON(2) OR _MOUSEBUTTON(3) '-1=preme / 0=solleva
  34.         IF mb2 = 0 THEN 'rilasciato
  35.             IF TIMER - t2# < .3 THEN
  36.                 t2# = TIMER
  37.                 t1# = TIMER
  38.                 passato = 0
  39.                 plusClick% = 2
  40.                 EXIT DO ' <--------------- Exit loop and function here.
  41.             END IF
  42.             IF TIMER - t1# < .3 THEN
  43.                 IF passato = 0 THEN 'primo rilascio entro il tempo
  44.                     passato = 38
  45.                     t1# = TIMER
  46.                     t2# = TIMER
  47.                     plusClick% = 1
  48.                     EXIT DO ' <--------------- Exit loop and function here.
  49.                 ELSEIF passato = 38 THEN 'rilascio ignorato
  50.                     passato = 0
  51.                     t1# = TIMER
  52.                     t2# = TIMER
  53.                     plusClick% = 1
  54.                     EXIT DO ' <--------------- Exit loop and function here.
  55.                 END IF
  56.             ELSE 'tempo passato in ogni caso
  57.                 t1# = TIMER
  58.                 t2# = TIMER
  59.                 passato = 0
  60.                 plusClick% = 1
  61.                 EXIT DO ' <--------------- Exit loop and function here.
  62.             END IF
  63.         END IF
  64.  
  65.     LOOP UNTIL provj$ <> "" OR provxxw& <> 0 OR _MOUSEWHEEL <> 0

Now if you will excuse me, I have to get back to my work in the Virginia Governor election. I've made 1,000 phone calls so far. I would have made more, but on that one-thousandth call, Steve finally figured out it was me calling each time, and shut off his phone!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: Click
« Reply #10 on: November 03, 2021, 03:12:29 am »
Ah Pete! Real life! Incredible... there is also that...

Thank you for your time and valuable information

I believe that the problem lies in recursiveness: it must be carefully avoided
Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)

Offline krovit

  • Forum Regular
  • Posts: 179
    • View Profile
Re: Click
« Reply #11 on: November 03, 2021, 08:32:27 am »
To better clarify things I attach the small and stupid program that follows (and that is useless!) that is compilable with versions up to 1.61 but not with 2.01

Code: QB64: [Select]
  1. flag = 1
  2. v0$ = "10"
  3. v1$ = "2021"
  4.  
  5. Print inser$(flag, v0$, v1$)
  6.  
  7.  
  8. Function inser$ (flag, v0$, v1$)
  9.  
  10.     If flag = 0 Then
  11.         Swap inser$, v0$
  12.     Else
  13.         Swap inser$, v1$
  14.     End If
  15.  
  16.  


Nothing is easy, especially when it appears simple (and nothing could be as dangerous as trying to do good to others)