### Author Topic: Rat Problem  (Read 273 times)

#### Code Hunter

##### Rat Problem
« on: January 23, 2019, 06:53:15 PM »
I would appreciate some help with this:

Code: QB64 [Select]
1. CONST True = -1
2. CONST False = 0
3.
4. TYPE newMouse
5.   oldX AS INTEGER
6.   oldY AS INTEGER
7.
8.   newX AS INTEGER
9.   newY AS INTEGER
10.
11.   clicked AS INTEGER
12.   dragged AS INTEGER
13.   pressed AS INTEGER
14.
15. DIM SHARED rat AS newMouse
16.
17. rat.clicked = False
18. rat.dragged = False
19.
20. DIM getInput AS STRING
21.
22.
23. WHILE NOT Quit
24.   _LIMIT 60
25.
26.   getInput = getRat
27.   IF getInput <> "{Nil" THEN LOCATE 12, 2: PRINT getInput
28.
29.
30. FUNCTION getRat\$
31. temp = "{Nil"
32.
33.   rat.newX = _MOUSEX
34.   rat.newY = _MOUSEY
35.
36.   rat.pressed = _MOUSEBUTTON(1)
37.
38. IF rat.pressed THEN
39.   temp = "{Press"
40.   rat.clicked = False
41.
42.   IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
43.     temp = "{Drag "
44.     rat.dragged = True
45.   IF rat.clicked = False THEN
46.     temp = "{Click": BEEP
47.     rat.clicked = True
48.     rat.dragged = False
49.
50. rat.oldX = rat.newX
51. rat.oldY = rat.newY
52.
53. getRat = temp
54.

It 'seems' to work, yes, but why does it beep at the start of the program?

"WHILE _MOUSEINPUT: WEND" <<<< Doesn't that remove any of the old Mouse Inputs?

#### Ashish

• The joy of coding is endless.
##### Re: Rat Problem
« Reply #1 on: January 25, 2019, 10:32:47 AM »
The following seems to work -
Code: QB64 [Select]
1. CONST True = -1
2. CONST False = 4 'try different value for false as all variables have 0 as their initial value
3.
4. TYPE newMouse
5.     oldX AS INTEGER
6.     oldY AS INTEGER
7.
8.     newX AS INTEGER
9.     newY AS INTEGER
10.
11.     clicked AS INTEGER
12.     dragged AS INTEGER
13.     pressed AS INTEGER
14.
15. DIM SHARED rat AS newMouse
16.
17. 'rat.clicked = False
18. 'rat.dragged = False
19.
20. DIM getInput AS STRING
21.
22.
23. WHILE NOT Quit
24.     _LIMIT 60
25.
26.     getInput = getRat
27.     IF getInput <> "{Nil" THEN LOCATE 12, 2: PRINT getInput
28.
29.
30. FUNCTION getRat\$
31.     DIM temp AS STRING
32.     temp = "{Nil"
33.
34.         rat.newX = _MOUSEX
35.         rat.newY = _MOUSEY
36.
37.         rat.pressed = _MOUSEBUTTON(1)
38.
39.     IF rat.pressed THEN
40.         temp = "{Press"
41.         rat.clicked = False
42.
43.         IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
44.             temp = "{Drag "
45.             rat.dragged = True
46.         IF rat.clicked = False THEN
47.             temp = "{Click": BEEP
48.             rat.clicked = True
49.             rat.dragged = False
50.
51.     rat.oldX = rat.newX
52.     rat.oldY = rat.newY
53.
54.     getRat = temp
55.
56.
if (Me.success) {Me.improve()} else {Me.tryAgain()}

My Projects - https://github.com/AshishKingdom?tab=repositories

#### Code Hunter

##### Re: Rat Problem
« Reply #2 on: January 27, 2019, 01:30:11 AM »
The following seems to work -
Code: QB64 [Select]
1. CONST True = -1
2. CONST False = 4 'try different value for false as all variables have 0 as their initial value
3.
4. TYPE newMouse
5.     oldX AS INTEGER
6.     oldY AS INTEGER
7.
8.     newX AS INTEGER
9.     newY AS INTEGER
10.
11.     clicked AS INTEGER
12.     dragged AS INTEGER
13.     pressed AS INTEGER
14.
15. DIM SHARED rat AS newMouse
16.
17. 'rat.clicked = False
18. 'rat.dragged = False
19.
20. DIM getInput AS STRING
21.
22.
23. WHILE NOT Quit
24.     _LIMIT 60
25.
26.     getInput = getRat
27.     IF getInput <> "{Nil" THEN LOCATE 12, 2: PRINT getInput
28.
29.
30. FUNCTION getRat\$
31.     DIM temp AS STRING
32.     temp = "{Nil"
33.
34.         rat.newX = _MOUSEX
35.         rat.newY = _MOUSEY
36.
37.         rat.pressed = _MOUSEBUTTON(1)
38.
39.     IF rat.pressed THEN
40.         temp = "{Press"
41.         rat.clicked = False
42.
43.         IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
44.             temp = "{Drag "
45.             rat.dragged = True
46.         IF rat.clicked = False THEN
47.             temp = "{Click": BEEP
48.             rat.clicked = True
49.             rat.dragged = False
50.
51.     rat.oldX = rat.newX
52.     rat.oldY = rat.newY
53.
54.     getRat = temp
55.
56.

Thank you Ashish, but that doesn't work for me.  I even tried making True/Fase AS STRING { T/F } and even that doesn't work.

Can anyone provide a good mouse example where the mouse isn't recognized UNTIL the mouse button is released and NOT pressed?

#### SMcNeill

• QB64 Developer
##### Re: Rat Problem
« Reply #3 on: January 27, 2019, 04:15:42 AM »
It beeps because at startup the mouse is both released and not pressed.

IF Rat.pressed THEN....  (rat.pressed = 0 so it’s not — go into the ELSE )

ELSE
IF rat.clicked = FALSE THEN... (rat.pressed = 0, so we do the stuff here)
temp = “”: BEEP <— and you beep

Set a flag to check to see if the mouse has been clicked down at least once before BEEPING for a release.

If rat.pressed THEN
RatPressedOnce = TRUE
‘More stuff
ELSE
IF rat.clicked = FALSE AND RatPressedOnce = TRUE THEN
‘Do stuff
ENDIF
END IF
« Last Edit: January 27, 2019, 04:17:29 AM by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

#### Pete

• Cuz I sez so, varmint!
##### Re: Rat Problem
« Reply #4 on: January 27, 2019, 11:18:09 AM »
Sample mouse code to wait until left mouse button is released before tacking action...

Code: QB64 [Select]
1.     CALL mouse(mi%, mx%, my%, lb%)
2.     IF lb% THEN LOCATE 1, 1: PRINT "Left mouse button down...     ": mflag% = -1
3.     WHILE _MOUSEBUTTON(1): CALL mouse(mi%, mx%, my%, lb%): WEND
4.     IF mflag% AND lb% = 0 THEN
5.         LOCATE 1, 1: PRINT "Left mouse button released...     ": mflag% = 0
6.
7. SUB mouse (mi%, mx%, my%, lb%)
8.     mi% = _MOUSEINPUT
9.     mx% = _MOUSEX
10.     my% = _MOUSEY
11.     lb% = _MOUSEBUTTON(1)

If you don't want a sub...

Code: QB64 [Select]
1.     mi% = _MOUSEINPUT: lb% = _MOUSEBUTTON(1)
2.     IF lb% THEN LOCATE 1, 1: PRINT "Left mouse button down...     ": mflag% = -1
3.     WHILE _MOUSEBUTTON(1): mi% = _MOUSEINPUT: lb% = _MOUSEBUTTON(1): WEND
4.     IF mflag% AND lb% = 0 THEN
5.         LOCATE 1, 1: PRINT "Left mouse button released...     ": mflag% = 0

It is the ...

WHILE _MOUSEBUTTON(1): mi% = _MOUSEINPUT: lb% = _MOUSEBUTTON(1): WEND

...that waits for the button to be released.

Pete