Global Keyup event handler

Superfly1611

Regular
Joined
Feb 17, 2004
Messages
66
Location
UK
Hi all,

I'm in the process of writing my first application for the compact framework.

I need to write an event handler that handles all keyup events. Now obviously I could simply override the OnKeyUp event of the form, which is something I have tried with limited success.

Whenever another control on the form has the focus the keyup event handler will obviously not fire.

I would have thought that this would be a common problem, I did a few searches on msdn but it didn't come up with anything that looked quite like what I was looking for.

Is there a solution to this?
 
Ok I have a working solution, however I am very interested to see if you guys think this was the right way to do it because I must confess this is a very new area for me.

At run time I recurse through each control on my form and assign the keyup event of that control an event handler like so:
Code:
        void AssignHandlers(Control container)
        {
            foreach (Control ctrl in container.Controls)
            {
                ctrl.KeyUp += new KeyEventHandler(Handle_KeyUp);
                AssignEventHandlers(ctrl);
            }
        }

        void Handle_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Down:
                    //do stuff;
                    break;
            }
        }

Any comments (good or bad) you have on how I've achieved this would be greatly appreciated.

Many Thanks
 
IceAzul said:
Set the forms .KeyPreview to true.
Hey thanks for answering, unfortunately I cannot use the KeyPreview property of the form because I'm using the compact framework and I don't seem to have that option available to me.

I think my solution is the best way of doing this but if someone has another way of doing this I'm all ears :)
 
Hmm, you must be using 1.0 of the cf. Version 2.0 supports Keypreview and many other nice things, so if it is an option, you should consider switching.
 
yeah i'm using 1.0, unfortunately switching to 2.0 isn't an option which is annoying to say the least (especially because I started developing on that platform!).
 
Back
Top