Keypress Event Handler linked to the Form itself.

Posted on 2007-07-26
Last Modified: 2011-09-20
Is there a way to give the form itself a KeyPressEventArgs?

I am writing a program for individuals with learning disabilities. Part of the design philosphy of the form is to have as little as possible that is clickable. Therefore they can't click into an area that they shouldn't. This is needed since their coordination with a mouse is very poor. That is the purpose of the application.

So, having a menu & minimize and maximize buttons are kind of a bad thing. In my application, the only things clickable is the "close program" X button in the top right hand corner, and the buttons that I coded in.

What I'm looking to do is create a keyboard shortcut. This Keyboard shortcut will then open up the "setup" form where different aspects of the application can be modified.

This is what I tried...

// In the Initialize Compenent section.
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Keyboard_Press);

// Then down below that...
private void Keyboard_Press(object sender, System.Windows.Forms.KeyPressEventArgs e)
Setup frmSetup = new Setup();

This should open up a Setup Form whenever any key is pressed, no? However nothing happens. I even put a debug break in there, and the program never pauses to go into debug mode.

I guess that means that the form never trully ever had control.

Anyone have any ideas?
Question by:Decadre
    LVL 6

    Accepted Solution

    Set the KeyPreview property of the form to True, this will make the form intercept all Key Events so that the forms Events will fire first.
    LVL 6

    Assisted Solution

    Your code would work perfectly, but you probably have a textbox, or some other control which has the focus and catches the keypresses before the form. If you set  this.KeyPreview = True;  ... it should work as you meant for it to.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now