Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello everyone, I am new at this forum and i have a problem by controlling a key.

The problem that i want to solve is that way :

1) Let's say we have a form in c# application and have a label.

2) I want to use arrow keys to change tha label. But the important thing that i want is,if i press the key and handle it, the label must show the specified text while i keep pressing the key. If i press and take it back immidiately, the label text must return its original state.

3) This is just a demonstration of a problem. I want to use this property in other application , before doing that, i have to know and can use the property in proper way.

Thanks in advance..

What I have tried:

I tried to use KeyDown, KeyPress and KeyUp events but i couldn't take what i want. Once i press the key and keep it that state, the situation is same.However, i take the pressing back ,the label does not change (I want the label to return its original state if i don't keep pressing the key).
Posted
Updated 28-May-16 3:57am
Comments
Sergey Alexandrovich Kryukov 27-May-16 22:32pm    
System.Windows.Forms.Form? A Web form? Anything else?
Anyway, there is no such thing as "detect a key is pressed"; this is not how event-oriented programming works.
I have no idea what you could have missed (and why do you have this "detect" misconception), if you already knew to use those events. But I also have no idea what you really have tried. If you need help, you have to provide this information.

To give you better idea, I probably have to explain what "What I have tried" means. Please read carefully:
What have you tried so far?

You see, what you wanted to achieve looks extremely simple. Your KeyDown event handler should show one text, your KeyUp event handler should show another text, and KeyPress should not be used.

Also, you should not forget about keyboard focus. For System.Windows.Forms.Form, you may need to use KeyPreview.

—SA
forty69 27-May-16 23:47pm    
Firstly,thanks your comments and advices. I want to solve this problem in order to solve another problem that i am dealing with. The thing is that i use the serial port to communicate Arduino (microcontroller). And my aim is that control something in microcontroller via these arrow keys or the others. I want to use keys as input, and while i keep pressing one of the arrow keys ,i want the program to send same state/command to the microcontroller.However,when i do this,it sends just one command if i keep either pressing or not. Thanks again.
George Jonsson 28-May-16 0:49am    
Use the Reply button if you want to reply to a comment.
Now the only one who gets notified is you.
BillWoodruff 28-May-16 8:16am    
imho, you are making a mistake to want to use key-repeat in this way: the frequency of key-repeat may depend on a variety of system settings, hardware drivers, etc.

why not use a Slider control, or numeric up-down Control ?
Philippe Mori 28-May-16 9:49am    
You should not use non standard behavior...

For a cursor key, you don't get a KeyPress Event - you do for alpha keys and so forth - what you get is a KeyDown event, then a short delay (set in the OS keyboard settings) and then a sequence of more KeyDown events until you release the key (interval also set in the OS keyboard settings), at which point you get a KeyUp event.
This is called "autorepeat" and the initial delay is generally around half a second or so, then the key repeats at around 8 to 10 keys per second.
If you want to do something different for "long presses" then use a class level int, initially zero, and add one to it each time you get the KeyDown event for a particular key. In the KeyUp event, check it - if it's one, revert your label / send a cancel to the Arduino, then reset it to zero.

You'll have to add some stuff to detect changed keys, and so forth, but that's the general idea.
If you are handling these at the form level, you need to set the Form.KeyPreview property to true to get the events at all.
 
Share this answer
 
I think there is a much simpler solution to your design goal here, if this is WindowsForms you are working with.

The NumericUpDown Control ... when it has Focus, and its 'InterceptArrowKeys Property is set to 'true ... will automatically respond to the arrow-keys being held up or down, incrementing, or decrementing, the shown value based on how you set the initial state of the Control's 'Minimum, 'Maximum, and 'Increment properties.

If you need other behavior (like reacting to Left, Right, PageUp, PageDown, End, Home keys) you can sub-class the NumericUpDown and override KeyPreviw, ProcessCmdKeys, etc.
 
Share this answer
 
First of all, thanks a lot for your comments and advices. I solve the problem. My fault is that i dont know the functionality of KeyDown and KeyUp events. I will use my conditions in KeyDown event and while i am using it ,it says the state that i want.And KeyUp event is for the checking the specified key if it is still pressing or not. Thanks a lot again..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900