• Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement