Getting events when Button object is released - c#

On Windows 7 Phone, using Silverlight framework
I'd like to handle when a Button is released.
It's easy to tell when the button is pressed (Click event which is fired either when pressed or when released according to the ClickMode property)
I've played with all the other events provided with the Events editor (KeyUp, LostFocus, MouseLeave etc..)
But I'm yet to find something that is definitive in regards to getting an event when a button is released.
Ultimately, I'm trying to handle doing a click vs a long click when pressing on a button
Thanks

For your situation, KeyUp is only half of the story. You also need to handle KeyDown where you will save the current time which you will then compare to the current time value after KeyUp to determine whether the press was short or long. You also need to make sure that you track one particular key in case your handler(s) is/are intercepting all key strokes.
If for some reason ClickDown/Up don't work out you could try handling the Click event but starting with a ClickMode of press, then changing ClickMode to release on the press handler. This process, though not simple, would give you a chance to implement the down-hold and timer-release sequence that you're looking for.

Related

Keyboard Accelerator with RepeatButton

There is a RepeatButton which repeats Click event in time Interval if you are holding the button. And there is pretty nice KeyboardAccelerator support in UWP for supporting keyboard shortcuts. But together it does not work as expected:
<RepeatButton Content="Click me" Interval="500" Click="RptBtn_Click" >
<RepeatButton.KeyboardAccelerators>
<KeyboardAccelerator Modifiers="Control" Key="K" />
</RepeatButton.KeyboardAccelerators>
</RepeatButton>
Now pressing Control+K calls RptBtn_Click event periodically, but not with 500 ms interval as set in RepeatButton, but much faster. This is happening because KeyBoardAccelerator calls RptBtn_Click event repeatedly, but doesn't hold the button for performing RepeatButton behavior.
Documentation says:
By design, the accelerator autorepeats (for example, when the user
presses Ctrl+Shift and then holds down M, the accelerator is invoked
repeatedly until M is released). This behavior cannot be modified.
Ok, I understand why. But I don't know how to add Keyboard shortcut to RepeatButton
in this scenario you won't be able to modify the behavior of the interval for the repeat of the app accelerator, only the UI click event itself.
You could manually handle a timer in the event handler to mark the event as handled unless the interval has passed since the last invoke. Alternatively, you could remove the accelerator and handle the keyDown events.
According to Microsoft's Documentation, when you use an accelerator the first time, you need to set KeyboardAcceleratorInvokedEventArgs.Handled=True. That stops the repeating, as I understand it. Unfortunately, based on your code, you won't have access to that object because it looks like you're able to call the MenuFlyoutItem.Click event method, which, as I understand it, shouldn't/couldn't be done. The documentation states that your KeyboardAccelerator tag should include the property "Invoke" which is set to the method you want to call when your key combo is pressed.
Additionally, the call is to a different method with a different signature than the "click" event, so you will either need to call your click event from the accelerator invocation, or you will need to have both your accelerator and your click event call some third method that accomplishes the task you want to do. I have opted for using a third method to do the work.
Unfortunately for me, I cannot get KeyboardAccelerators to work AT ALL!.

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...
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.
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.

CoreWindow.KeyDown doesn't get called if Enter is pressed with focus on clickable element

Is there any way I can still catch the keypress if Enter is pressed on a clickable element.
Because the Windows Store Apps API treats Enter as click event rather than a keypress if the focus is on a clickable element (i.e. a GridViewItem).
Unfortunately I have to let the user right-click on a GridViewItem which sets the focus to the element.
Moving the focus would be an option but I'd prefer not to do that if I don't have to.
It seems, this is not in the nature of WInrt. You can follow the specification provided on MSDN:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.keydown.aspx
You can still try the Key Down event of GridView
In WinRT, some events are user-initiated only. As a result, some WinRT APIs can only be invoked in those types of events. Button.Click is one of those events. As a result, you cannot programmatically raise this event because it would undermine the security feature that limits certain API to those types of events.
Make sense?
Best of luck!

How to separately handling MouseClick and MouseDoubleClick event on ChartControl

I'm implementing a module which needs to handle both MouseClick and MouseDoubleClick on a ChartControl of DevExpress. The version that I'm using is v12.2.
When I double click on that chart, both events are fired. I'd like (and I think that it must be) it just fires one event, in this case, MouseDoubleClick.
So, anyone know how to fix that problem?
What's I've tried:
Handle MouseClick or Click event and see MouseEventArgs#Clicks property. But it's always 1.
What's I'm using:
Declare a boolean variable to tell if MouseDoubleClick is fired. On MouseClick handling, just waiting for a moment, then do the codes if that variable does not turn on. I think this is a bad implementation.
You need a time machine to see the difference between the two. Inevitably a double-click starts with a single click, you always see the first click first.
You can get a time machine that sees the future by using a timer that delays the past. Set its interval to SystemInformation.DoubleClickTime + 16 and start it in the Click event, stop it in the DoubleClick event. If the Tick event fires then it was a single click.
That works, but do note that the delayed response to a single click is fairly annoying. Best not to annoy your user with a user interface like that.

Use right-click with Windows Forms Button

I am writing an app where the user should be able to alter the action of a button.
The user should right-click a button, and choose an option from a pop-up context menu. Once the choice has been made the button will perform a different action when the user uses a normal click.
I've already gotten the "Click" event working for a normal button click, however the "MouseClick" event handler isn't working correctly.
The "MouseClick" event gets activated on regular left-clicks, but never get's called for right-click.
Is there some default event handling being performed that is ignoring that right-click?
I'm sorry to say that this would be a serious UI blooper. Perhaps it would make more sense to add a small combobox next to the button.
Perhaps something like this?
http://www.codeproject.com/KB/buttons/SplitButton.aspx
If you want to display a context menu with actions to choose from it should be enough to assign a ContextMenuStrip to the ContextMenuStrip property. There is usually no need to manually handle the mouse events for that.
surely it would be better to implement it on the MouseDown event rather than the MouseUp event. i dont understand how this is much different from MouseClick event
edit: Just tried this and it works a treat :)
In Button (and certain other controls), the MouseClick event is only fired for the left button. Refer to MSDN.
If you want to know about the right button, respond to the MouseUp event--though as other posters have pointed out, this isn't a great UI idiom.
Use the Mouse UP event... test that Button.X and Button.Y are within the size of the button otherwise you have moved the mouse away from the button.
Terry G

Categories

Resources