In .Net, what's the better choice to code key presses in keyboard, key-up or keydown?

My answer here is just based on experimenting with different applications, not programming per se.

My answer here is just based on experimenting with different applications, not programming per se. Handle the keydown. Let that be the trigger for your logic.

That's what the user would expect based on interacting with other applications. For example, try a key down in Notepad, and you'll see that the DOWN triggers the reaction in Notepad. It doesn't wait for the UP.

This - try holding down a key - it'll repeat, even if you haven't let go yet. THOSE are the events you should be handling. – Greg Hurlman Nov 6 '08 at 15:55.

It doesn't matter if it's . Net or not, it matters what the user expects. Keydown is a good time to respond to the four arrow keys.

Character input is a good time to respond to input of visible characters. Keyup is usually a good time to respond to any action that is going to have an effect on a document, but keydown would be better if it's a game where the user wants immediate effect.

It's not really "which is a better choice for .NET. " You really have to think about how you want to respond to key presses. If you want to what someone is typing in a text box it's usually best to wait until they've released before trying to decide what they're doing.

But if it's something like a game where you want to respond the instant it's pressed, than you would use KeyDown. KeyDown is the moment the key is pushed down. KeyUp is after the key has been released.

One thing to consider that I've had a problem with before: If you handle something on key down that changes the window or changes UI component focus, then the new UI element may sometimes get the key up event. This happened to me at my last job. We had a control on a form that, on key down, would load a new form.

If the new form loaded fast enough, then the new form would get focus before the user released the key, and the new form would get the key up event. We had a UI control on that 2nd form that reacted to key up, and it would sometimes get triggered unintentionally. The moral of the story is; keep it consistent.

Pick one or the other and stick to it :).

The key down event happens as soon as the user presses a key, the key up event happens when they release the key after pressing it. So if you use the key up event, and the user presses a key and holds it for 5 seconds, nothing will happen until they let go of it. (Note: I know nothing about .

Net, I've just used 'key up' and 'key down' events in other libraries. ).

You have your key up/key downs backwards. – Chad Moran Nov 4 '08 at 3:11 Oh right, fixed, thanks! – Jeremy Ruten Nov 4 '08 at 3:12 I think you'll want to change the word "depress" to "release" in the first sentence.

– Matthew Schinckel Nov 4 '08 at 3:22 Oh, I thought it meant the opposite of "press" but after looking it up it looks like it's a synonym of "press". :/ So I guess I'll change it, thanks! – Jeremy Ruten Nov 4 '08 at 3:29.

Another thing to take into account: When holding modifiers, it's important to use keydown. I generally use keydown to set a variable like ctrlPressed=true; then use keyup to unset that variable ctrlPressed=false; I generally use keyPressed for all alphanumeric characters. This sort of system allows you to enable things like CTRL + K + C ala Visual Studio.

I allmost allways use KeyDown, because then I can use e. Handled=True and stop the keyevent to travel from textbox to container and down in the eventque if I want. You can use e.

Handled in KeyUp also, but then its "to late" because the key the user entered will be seen in the textbox and you have to take it away manually if you for example want to stop the user to enter digits in the textbox.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions