Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I have a problem on how to make keyDown active when press and inactive when release. I wrote a code for display a color, which is when press it shown green and when release return to black. I attached my code below.

What I have tried:

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
            if (e.KeyCode == Keys.NumPad1)
                set1Disp.BackColor = Color.Green;

            else if (e.KeyCode == Keys.NumPad3)
                set2Disp.BackColor = Color.Green;
Updated 12-May-22 23:51pm

1 solution

Handle the KeyUp event as well, and add a bool - "KeyPressed" which you check in the Up handler to see if you should change the colour back.
Share this answer
Member 15627303 11-May-22 14:15pm    
if use KeyDown event to be inactive when release, then my code need to use with KeyUP event. correct me if I am wrong.
[no name] 11-May-22 15:16pm    
There's no such thing as "inactive" or "active" key events; they're all active all the time. They are handled (code) or ignored or defaulted or some combination thereof; those are your options.

"e.Handled" for a given key (true/false) is maybe what you're looking for.

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