_MOUSEINPUT problem

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!
Post Reply
davey
Newbie
Posts: 4
Joined: Wed Aug 05, 2015 9:56 pm

_MOUSEINPUT problem

Post by davey »

I am having trouble reading the mouse inputs. It seems that after there has been a mouse click, the mouse has to be read (_MOUSEINPUT) a number of times before it will know that there is no more input i.e. before it will go back to zero. This is most evident if the mouse is moved - it may have to be read 100 times before it resets to zero.

In the following test code, it works as long as the mouse is not moved, although sometimes requires 2 or 3 loops to reset. If the mouse is moved between clicks, it can require over 100 loops.

SCREEN 12
_FULLSCREEN

DO WHILE INKEY$ = "" ' <--- hit any key to exit

IF _MOUSEINPUT < 0 THEN
mi = _MOUSEINPUT
PRINT "Do your thing here. mi = ", mi, c
IF _MOUSEBUTTON(1) < 0 THEN PRINT "Left"

DO WHILE _MOUSEINPUT < 0 'mi < 0 ' <--- LET THE MOUSE "RECOVER" TO ZERO!!! Or it won't work if mouse moves
mi = _MOUSEINPUT
c = c + 1 ' <--- To prove it, count how many times it loops before going to zero
_DELAY 0.1 ' <-- Even with a delay, it still needs many loops, especially if mouse has been moved between clicks
LOOP

END IF

PRINT "mouse button up", c
c = 0: mi = 0
_DELAY .5 ' <--Slow the whole thing down so you can read it

LOOP
PRINT c, "exit"

Also, the first time through the IF statement, the input is always -1, even though the mouse has not been clicked.

Am I doing something wrong here?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: _MOUSEINPUT problem

Post by burger2227 »

Best to not read _MOUSEINPUT until the loop stops. It will read -1 until all mouse input stops so a WHILE or UNTIL loop will keep looping until it does stop.

Best way to read a button held down is to just read it every loop so that you can also read button presses after every read.

Code: Select all

DO
i = _MOUSEINPUT 'allows reads of all functions and keeps button history that is lost when it becomes 0.
b = _MOUSEBUTTONN(1)
LOOP UNTIL b = 0  
If moves are your main concern then use other kinds of loops. Only use _MOUSEINPUT UNTIL or WHILE to catch up.

Also use _LIMIT 100 or so to slow down loops as _DELAY stops everything!

Use our WIKI: http://www.qb64.net/wiki/index.php/MOUSEBUTTON
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
davey
Newbie
Posts: 4
Joined: Wed Aug 05, 2015 9:56 pm

Re: _MOUSEINPUT problem

Post by davey »

Thank you! That (plus the Wiki) helped immensely.

I have changed my code to the following and it works perfectly (even if the mouse is moved around):

DO 'main program loop
DO WHILE _MOUSEINPUT 'check mouse status
b1 = _MOUSEBUTTON(1)
LOOP
DO WHILE b1 'check for button release
i = _MOUSEINPUT
b1 = _MOUSEBUTTON(1)
Click = 1
LOOP

IF Click = 1 THEN PRINT "CLICK" ' (this is where you do your thing; could be a GOSUB)
Click = 0: b1 = 0 'reset where needed
LOOP UNTIL INKEY$ <> "" ' press any key to quit

I will keep this as an example.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: _MOUSEINPUT problem

Post by burger2227 »

If you want to verify a click at a program's button position then you can verify the x and y positions both before and after the button release to verify it much like Windows allows a user to move off a position to cancel an press operation.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply