Posts: 799
Threads: 140
Joined: Apr 2022
Reputation:
33
09-02-2025, 02:34 AM
(This post was last modified: 09-02-2025, 02:52 AM by PhilOfPerth.)
I'm trying to get a mouse key (left-mouse) to only return its result once.
I've tried using While _KeyHit <> 0: _Delay .2: Wend but it still repeats (sometimes).
I've also played around with _mousebutton, with no effect.What's the simplest way to make it wait until the key has been released?
Edit: I'm trying this at present, and it seems to work:
Do '
i = _MouseInput
Loop Until Not _MouseButton(1)
is there a better way, and is there a reason not to use this?
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) 
Please visit my Website at: http://oldendayskids.blogspot.com/
Posts: 3,446
Threads: 376
Joined: Apr 2022
Reputation:
345
This type set up:
Code: (Select All)
Do 'start of your do loop
While _MouseInput: Wend 'update the mouse buffer
MouseDown = _MouseButton(1) 'get the state of the mouse
Cls
Print "Mouse clicks:"; TotalClicks 'print the count so we can watch it only increase once per click
If MouseDown _AndAlso OldMouseUp Then 'the mouse was up, it's now down, we can count it as a click
TotalClicks = TotalClicks + 1
End If
OldMouseUp = _Negate MouseDown 'update the old button status so we can make certain it was up before counting the next click
_Limit 30
Loop Until _MouseButton(2) _OrElse _KeyHit
System
It really is just that simple. One variable to track the current button state. A second variable to record the old button state.
If the old state was UP and the new state is DOWN, it's a click.
IF the old state was DOWN and the new state is DOWN, it's not a click... It would be the button being held down and you could start counting it as a hold event.
IF the old state was UP and the new state is UP, then the mouse button is up and not being pressed.
If the old state was DOWN and the new state is UP, it means the user just released the mouse button. It's the end of a click or hold state and not the start of one.
Two variables, working together, and you can get all that info if you need it. In this case, all you're looking for is the simple CLICK result and that's only obtained when the new state is DOWN and the old state was UP previously.
Posts: 799
Threads: 140
Joined: Apr 2022
Reputation:
33
(09-02-2025, 02:51 AM)SMcNeill Wrote: This type set up:
Code: (Select All)
Do 'start of your do loop
While _MouseInput: Wend 'update the mouse buffer
MouseDown = _MouseButton(1) 'get the state of the mouse
Cls
Print "Mouse clicks:"; TotalClicks 'print the count so we can watch it only increase once per click
If MouseDown _AndAlso OldMouseUp Then 'the mouse was up, it's now down, we can count it as a click
TotalClicks = TotalClicks + 1
End If
OldMouseUp = _Negate MouseDown 'update the old button status so we can make certain it was up before counting the next click
_Limit 30
Loop Until _MouseButton(2) _OrElse _KeyHit
System
It really is just that simple. One variable to track the current button state. A second variable to record the old button state.
If the old state was UP and the new state is DOWN, it's a click.
IF the old state was DOWN and the new state is DOWN, it's not a click... It would be the button being held down and you could start counting it as a hold event.
IF the old state was UP and the new state is UP, then the mouse button is up and not being pressed.
If the old state was DOWN and the new state is UP, it means the user just released the mouse button. It's the end of a click or hold state and not the start of one.
Two variables, working together, and you can get all that info if you need it. In this case, all you're looking for is the simple CLICK result and that's only obtained when the new state is DOWN and the old state was UP previously.
Thanks Steve. I think I've got it working ok now.
Thanks for the explanation.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) 
Please visit my Website at: http://oldendayskids.blogspot.com/
Posts: 347
Threads: 45
Joined: Jun 2024
Reputation:
32
09-02-2025, 08:49 PM
(This post was last modified: 09-02-2025, 09:25 PM by Unseen Machine.)
Although its fixed already...make a UDT for Mouse, x,y, lmb, rmb etc, the define a two or maybe even three dimensional array of this type. Update it each loop and also back it up...if the current one is different to the last one...you can check for new clicks, releases, double clicks etc...
Unseen
p.s Again leads me to think, standardised, universal libs/tutorials would again be an asset to us ALL!
Posts: 3,446
Threads: 376
Joined: Apr 2022
Reputation:
345
(09-02-2025, 08:49 PM)Unseen Machine Wrote: Although its fixed already...make a UDT for Mouse, x,y, lmb, rmb etc, the define a two or maybe even three dimensional array of this type. Update it each loop and also back it up...if the current one is different to the last one...you can check for new clicks, releases, double clicks etc...
Unseen
p.s Again leads me to think, standardised, universal libs/tutorials would again be an asset to us ALL!
The problem there though, is like you've pointed out: Just getting folks to use them.
https://qb64phoenix.com/forum/showthread.php?tid=138
Posts: 347
Threads: 45
Joined: Jun 2024
Reputation:
32
(09-02-2025, 09:32 PM)SMcNeill Wrote: (09-02-2025, 08:49 PM)Unseen Machine Wrote: Although its fixed already...make a UDT for Mouse, x,y, lmb, rmb etc, the define a two or maybe even three dimensional array of this type. Update it each loop and also back it up...if the current one is different to the last one...you can check for new clicks, releases, double clicks etc...
Unseen
p.s Again leads me to think, standardised, universal libs/tutorials would again be an asset to us ALL!
The problem there though, is like you've pointed out: Just getting folks to use them.
https://qb64phoenix.com/forum/showthread.php?tid=138 Yep totally agree..but once WE have made them, we reference them in our replies, in our demos, in our examples, games, engines,etc...eventually itll filter down and we will all stand on the shoulders of the giants! I would happily just use wrapper functions in GDK and VQB if the underlying nuts and bolts where inherent! DAMN I WISH we worked that way!
Me, annoying as always, Unseen
Posts: 799
Threads: 140
Joined: Apr 2022
Reputation:
33
(09-02-2025, 09:32 PM)SMcNeill Wrote: (09-02-2025, 08:49 PM)Unseen Machine Wrote: Although its fixed already...make a UDT for Mouse, x,y, lmb, rmb etc, the define a two or maybe even three dimensional array of this type. Update it each loop and also back it up...if the current one is different to the last one...you can check for new clicks, releases, double clicks etc...
Unseen
p.s Again leads me to think, standardised, universal libs/tutorials would again be an asset to us ALL!
The problem there though, is like you've pointed out: Just getting folks to use them.
https://qb64phoenix.com/forum/showthread.php?tid=138
Thanks, Unseen.
But I don't use mouse enough to do that; as you pointed out elsewhere, some folks never use these "special" functions once they're written.
Wasteful? yep. Slopppy? yep. Lazy? probably. But it works for me. Maybe a sympton of today's "use it once, then throw it away" mentality.
I really appreciate your suggestion and help; thank you.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) 
Please visit my Website at: http://oldendayskids.blogspot.com/
Posts: 347
Threads: 45
Joined: Jun 2024
Reputation:
32
If you're ever gonna use it again, trust me, UDTs and some basic functions in a .bm/.bi are essential...you still only code it once, you just don't throw it away! It'll make your whole experience nicer (I feel) and also itll remove any trepidation about adding support for things like mouse, kb, etc...you just include your libs and are running on gas!
As always, happy coding bro!
John
Posts: 2,910
Threads: 305
Joined: Apr 2022
Reputation:
167
I'm in the camp of having a universal mouse routine (library) that I developed to work in all circumstances, just like the windows mouse. Sure, it's a bit bulky if all you want it for is a quick demo on the forum and the mouse routine is larger than the demo code, but hey, no problems, no changing it up to meet the requirements of other programs it gets used in, and best of all it's made in the USA so I can provide it to everyone here tariff free!!!
Pete
|