Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stopping repeated mouse-key press
#1
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.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#2
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.
Reply
#3
(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.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#4
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!
Reply
#5
(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
Reply
#6
(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
Reply
#7
(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.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#8
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
Reply
#9
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Any other way to ignore the Alt key using the INKEY method? Pete 24 1,384 12-31-2025, 05:24 PM
Last Post: Pete
  Setting mouse to ignore outside of _NewImage PhilOfPerth 11 710 12-18-2025, 07:20 PM
Last Post: Pete
  Do Loop, Sleep and Mouse Button Dimster 5 578 09-06-2025, 12:57 PM
Last Post: Dimster
  Stopping _SNDPLAYCOPY ?Possible? TerryRitchie 13 2,426 08-08-2024, 01:48 AM
Last Post: TerryRitchie
  Repeating mouse check PhilOfPerth 4 914 07-24-2024, 11:26 PM
Last Post: PhilOfPerth

Forum Jump:


Users browsing this thread: