I want to assign a single KEY in Keyboard to enable/Disable function.

Initally, the button should be disable. when I press the button1 it should get enable and when I press tha same button1 again it should get disable.

How can I do that in visual studio C# for windows forms application ?

Thanks for your help

1 Year
Discussion Span
Last Post by rproffitt

You could implement a KeyUp or KeyDown event from your main window and set a particular button according to which key was pressed.


Dear ddanbe,

I understand what you are saying. My question is to handle two event with single key.

For instance, I want to assign only one, lets say 'W' when I press the W first time the event should get activate (pressed W just once and take out my finger) and when I press again 'W' the event should deactivate. (As you said KeyUp and KeyDown - i should have to be pressed when I realease the key it will deactivate, I dont need that) . Hope you understand. Please give answer. Thank you again.


@m You just described your app's behavior. In psuedo code it's something like this.

The handler for the W keydown event would change some flag your event or loop that causes that look to stop.

So in the W keydown event handler something like:
' W key pressed
loopflag != loopflag; 'toggle true/false
if (loopflag) event.activate = true; 'tell the function/method to run.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.