Sign in to follow this  
paic

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this