How can I accept keystrokes in a WinForms app?

Use the KeyDown or KeyUp events if you are using WinForms.

Use the KeyDown or KeyUp events if you are using WinForms. For example, just drop the following code into your form class to override the OnKeyUp event and close whenever the user presses the q key: protected override void OnKeyUp(KeyEventArgs e) { if (e. KeyCode == Keys.

Q) { this.Close(); } base. OnKeyUp(e); }.

The only thing I intend to do is exit when the user presses a certain key. – olive Feb 8 at 11:23 1 @user: Yes, you have to deal with events. Everything you do in WinForms will deal with events.

Windows is an event-driven environment. Not sure why you'd prefer to avoid events in the first place, but the code to do this isn't complicated. – Cody Gray Feb 8 at 11:25 How did you get the nice 'Q' key?

– Neurofluxation Feb 8 at 11:33 1 @Neuro: q – Cody Gray Feb 8 at 11:36 okey .. done it using events.. thanks a lot... – olive Feb 87 at 8:31.

In the first two there are events related to key presses, in the third one, you can use Console. Read or ReadLine methods and test the return value.

Am working with windows forms. – olive Feb 8 at 11:17.

You may want to use KeyPreview and OnKeyPreview here, depending on what controls are you using on the form. Sometimes, KeyDown and KeyUp will be handled before you get a chance to handle them yourself. BTW, for windows apps, using 'q' key without any modifiers to quit isn't such a great idea, since you might have textboxes on the form that accept input, and your app will quit if someone writes 'quebec' into the textbox.

And if you don't want to use events, then OVERRIDE same virtual methods and provide implementation for them that will do what you want. But again, preferred method here is with events.

A control really shouldn't ever handle its own events. That violates most of the rules of object-oriented design, and can wreak all sorts of havoc and unexpected behavior if someone later decides to subclass that control. – Cody Gray Feb 8 at 23:38 I was talking about FORMS, not controls, since the question is (probably) concerning creating a form that will close.

– Daniel MoÅ¡mondor Feb 9 at 0:53.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions