Author Topic: Rat Problem  (Read 273 times)

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.   END IF
  46.   IF rat.clicked = False THEN
  47.     temp = "{Click": BEEP
  48.     rat.clicked = True
  49.     rat.dragged = False
  50.   END IF
  51.  
  52. rat.oldX = rat.newX
  53. rat.oldY = rat.newY
  54.  
  55. getRat = temp
  56.  

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?

Offline 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.     _DISPLAY
  30.  
  31. FUNCTION getRat$
  32.     DIM temp AS STRING
  33.     temp = "{Nil"
  34.  
  35.         rat.newX = _MOUSEX
  36.         rat.newY = _MOUSEY
  37.  
  38.         rat.pressed = _MOUSEBUTTON(1)
  39.     WEND
  40.  
  41.     IF rat.pressed THEN
  42.         temp = "{Press"
  43.         rat.clicked = False
  44.  
  45.         IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
  46.             temp = "{Drag "
  47.             rat.dragged = True
  48.         END IF
  49.     ELSE
  50.         IF rat.clicked = False THEN
  51.             temp = "{Click": BEEP
  52.             rat.clicked = True
  53.             rat.dragged = False
  54.         END IF
  55.     END IF
  56.  
  57.     rat.oldX = rat.newX
  58.     rat.oldY = rat.newY
  59.  
  60.     getRat = temp
  61.  
  62.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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

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.     _DISPLAY
  30.  
  31. FUNCTION getRat$
  32.     DIM temp AS STRING
  33.     temp = "{Nil"
  34.  
  35.         rat.newX = _MOUSEX
  36.         rat.newY = _MOUSEY
  37.  
  38.         rat.pressed = _MOUSEBUTTON(1)
  39.     WEND
  40.  
  41.     IF rat.pressed THEN
  42.         temp = "{Press"
  43.         rat.clicked = False
  44.  
  45.         IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
  46.             temp = "{Drag "
  47.             rat.dragged = True
  48.         END IF
  49.     ELSE
  50.         IF rat.clicked = False THEN
  51.             temp = "{Click": BEEP
  52.             rat.clicked = True
  53.             rat.dragged = False
  54.         END IF
  55.     END IF
  56.  
  57.     rat.oldX = rat.newX
  58.     rat.oldY = rat.newY
  59.  
  60.     getRat = temp
  61.  
  62.  

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?

Online SMcNeill

  • QB64 Developer
    • Steve’s QB64 Archive Forum
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!

Offline 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.     END IF
  7.  
  8. SUB mouse (mi%, mx%, my%, lb%)
  9.     mi% = _MOUSEINPUT
  10.     mx% = _MOUSEX
  11.     my% = _MOUSEY
  12.     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
  6.     END IF

It is the ...

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

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

Pete