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

Started by
4 comments, last by GameDev.net 17 years, 6 months ago
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 ^^
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;    }}

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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

This topic is closed to new replies.

Advertisement