[.net] A question about key events in a Control

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, Here is what I want to do : I have a Control which has the focus. When the user press the Alt key, I need to call a method, and only when the key is pressed ! And when it's released (keyup) I need to call another method. I overrided the OnKeyDown and OnKeyUp methods of my control, and it almost works : the event is correctly catched, but if I keep the Alt key down a few seconds, the OnKeyDown method keeps being called !! And I'm a bit lost in all the message / key processing methods of the Control class ... So, if you know how to achieve what I described ... ^^ Thx in advance for any help ^^

Share on other sites
The answer that presents itself to me is this - simply set a boolean flag to check and see if the down event has been fired before (pseudo-C#);

bool PreviouslyPressed = false;function OnKeyDown() {    if (IsAlt && !PreviouslyPressed) {        // Do something on key down        PreviouslyPressed = true;    }}function OnKeyUp() {    if (IsAlt) {        PreviouslyPressed = false;    }}

Share on other sites
Thx for the answer. I already thought about that, but I was hoping that there was an option, or an event that would do the job. I think I'll have to do that ^^
Thx again.

Share on other sites
No, I think you have to do that. It's in the spec for the Windows WM_KEYDOWN/UP messages that you keep getting keyboard events while a key is held down.

Share on other sites
No, I think you have to do that. It's in the spec for the Windows WM_KEYDOWN/UP messages that you keep getting keyboard events while a key is held down.

Share on other sites
Actually, KeyEventArgs has a flag Alt (as well as flags Control and Shift) that indicate if it is currently pressed or not, so no need for the extra flag variable in your code.

• 11
• 20
• 12
• 9
• 34
• Forum Statistics

• Total Topics
631399
• Total Posts
2999847
×