Radiohead
Radiohead

Reputation: 109

How to get a button event to repeat fire when holding down said button

I want to be able to hold down a button (i.e. MouseLeftButtonDown) and have it's event repeat fire.

I code Mouse.Capture(button) in the MouseLeftButtonDown event and Mouse.Capture(null) in MouseLeftButtonUp event.

If I hold the mouse button down, the MouseLeftButtonDown only fires once. I can see this not by setting a breakpoint but by using Console.Writeline().

I had this very code working at some point. Then I refactored and put the mouse events in an Interface. Everything still works except this repeat action now!

Update - More information:

Here is the before call which was in a specific class:

Mouse.Capture(sender as MenuGelButton)

MenuGelButton is a xaml creation of ellipses and text to make a cool button.

Since adding the interface (which is common code for several xaml creations all of which inherit the interface), here is the current call from within the interface:

Mouse.Capture(sender as UIElement)

Update of Update:

A mouse click event is not designed to auto repeat fire. A keyboard event is. I had this working (and it continues to work) on a keyboard event not the mouse event. Sorry for the confusion...

Upvotes: 2

Views: 2492

Answers (2)

bwall
bwall

Reputation: 1060

I believe you are looking for Microsoft's RepeatButton. You can use it in XAML just like you would use a Button, and it fires a click event multiple times.

From their documentation:

A RepeatButton is a button that raises Click events repeatedly from the time it is pressed until it is released. Set the Delay property to specify the time that the RepeatButton waits after it is pressed before it starts repeating the click action. Set the Interval property to specify the time between repetitions of the click action. Times for both properties are specified in milliseconds.

Upvotes: 2

Noctis
Noctis

Reputation: 11783

Try doing something like this:

  • mouse button --> fires event
  • in the event, set a timer of your liking (how many times per second would you like the action to happen?)
  • in a loop, every time timer fires, check to see if mouse button is still "down". If yes, fire. If not, don't fire.
  • you'll probably want to add another event of mouse button up, that will stop the timer, so it won't fire anymore (or bind to a bool that will let you know if you want to fire ... up to you ..)

Hope it helps.

Upvotes: 1

Related Questions