So I'm making a simple program where I need to know if a button is pressed, and I need to know when a button is released after it's been released. I need to be able to complete an action when it's released
Winforms/WPF: MouseUp
WebForms: onkeyup
It's not really clear if you're working with webforms or winforms, but .NET provides event handlers for pretty much any event that could happen.
Here's a listing of the events Buttons handle.
The one you're looking for is 'Click'. This method will be fired whenever the user clicks on the button.
If you need to do something when the user pushes down on the mouse and again when he releases, Button isn't right for you. Many other objects implement the 'OnMouseDown' and 'OnMouseUp' events, though.
If you're talking about a winform and a keyboard press use the KeyUp event ( http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup.aspx ) If you're talking about a mouse click use MouseUp ( http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseup(v=vs.71).aspx ).
Related
I'm relatively new to C# and I want to know how can I press a key on my keyboard say the Q key and have it preform a list of functions?
I want to form a list of function under neath it. Anybody have any idea how to do this? thanks
//Something like this
if keyboard_down(Q)
{
//Do events here
}
I know this isn't the right function but that is basically what i want it to do. Winforms
You didn't mention which technology you work with.
I will assume that you are using Winforms.
In order to react to Key Press event you need to use Control.OnKeyPress - See here
See also this example which shows how to handle 'enter' key press
Depends on the way you want it.
Do you want them to fire while the form got focus or even without any focus to the form?
With focus:
Click your form in the form-designer and then go to the properties-tab. Click that little lightning bolt on top of it. There are a lot of events in that list. Double click "Keypress" and it will create an event automaticly and open the right code-snipped for you.
Depending on the focus you want, you can also mark controlls inside the form designer and then create the keypress event.
Without focus:
This would be a bit more difficultly. Here you would need to use a global keyboard hook to permanently grab pressed keys. Google for "c# keyboard hook". There are some examples out there.
Im working on an app to control some stuff over bluetooth.
Currently I have a form with some buttons. On each button I want to trigger an event when the button is pressed down and fire a different event when the button is released.
Is this possible?
Not sure what the "bluetooth" info should tell me.
But in C# Winforms, yes that is possible. See the MouseDown and MouseUp Events: http://msdn.microsoft.com/en-us/library/system.windows.forms.button_events.aspx
(You may need to combine them with the keyboard events to capture them as well)
I have few buttons in my windows application which has mnemonics.Now if i press multiple keys the events of the button clicks gets fired as i have used mnemonics.
But the behaviour is not as expected,because second button event handler is getting executed before the first button event handler has finished its execution,and also i have written my code in such a way that in the event handler of first button i am disabling my second button,still the second button event handler is getting executed.The problem here is due to mnemonics.PLease suggest me a better way to handle mnemonics as soon as possible.
Thanks in advance.
If your logic is tied to your buttons, change that. Let your buttons merely manipulations an object that implements your logic. You can then check in this object whether the requested action is allowed (i.e. whether the previous operation has finished.
Alternatively you can just disable buttons when running and re-enable then when.finished.
I have multiple buttons that are using a single command with a command parameter to act on their "down" event among all types of interactions (click, keypress, touch). Turns out this isn't enough for my application, and I must also know when the button is released. How do I do something similar for these buttons "up" event?
Look at the OnIsPressedChanged() method. You may have to derive a class from Button to be able to handle it.
Also, you may handle the MouseLeftButtonUp event, but it will fire only if the button is pressed by a mouse click. If you want to catch keyboard, you will have to process KeyUp events and determine if the the button was clicked as a result of that key (Space, Enter in some cases, a shortcut key, etc.)
Unfortuntely there's no special command for the UP-event.
But you can use EventToCommand behavior
I've found a combination of events are required to listen to:
PointerReleased, PointerCanceled, PointerCaptureLost should all be listened to in a touch environment for "up" events.
So how to begin.
I am asking when you make a game for example and you add a controlls how do I make the form's key events run when the focus isnt on the form but on some of the controlls and when i call this.Focus() or this.Select() it doesnt't happen anything, but if I use a empty form(with no controls) it works(the events respond).And when i have for example 2 buttons and call button1.focus() and press a key the button1's event handler responds (only it) adn then when i call button2.Focus() it responds for button2.How is focus distributed througt the controlls?
I know for muttons you have to click them once to put the focus on them automaticly and for textboxtes too,but when i click the form it doesnt move the focus on the form.
Im have almost no experience with Key events please explayn me how to use them and how they function understandly.
PS: sorry for the long questin
You can set the Form.KeyPreview to true to have the form react to key events.