• Advertisement
Sign in to follow this  

[.net] How to get key input with a C# panel?

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I noticed that there are no key events for a panel such as OnKeyDown() and OnKeyUp(). Is it possible to make a user control of a panel that would have the events? Also, I'd need the double buffer property too. I'm suprised the panel is a bit different than a form. Any help is great. Update: Here's something I'm working with. Let me know how I can improve this.
    class GamePanel : Panel
    {
        public GamePanel()
        { 
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            G.keyState[(int)e.KeyCode] = true;
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            G.keyState[(int)e.KeyCode] = false;
        }
    }
[Edited by - nullsmind on May 7, 2006 7:19:43 PM]

Share this post


Link to post
Share on other sites
Advertisement
I just looked at MSDN at it does show key events for the Panel. Look here..

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsPanelEventsTopic.asp

theTroll

Share this post


Link to post
Share on other sites
No idea.. but if you notice on the MSDN they are not part of the "normal" public events. Nopt sure what all that is about.

theTroll

Share this post


Link to post
Share on other sites
For double buffering I think you have to make an override class that calls Setstyle(Controlstyles.DoubleBuffered, true) in the constructor (as Setstyle is protected).

According to the .Net SDK the key events are all "This member supports the .NET Framework infrastructure and is not intended to be used directly from your code". I don't know why, it seems like a perfectly sensible thing to want to use to me, but I guess that means they might do weird things. They are public though so you should be able to attach handlers to them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bob Janova
I don't know why, it seems like a perfectly sensible thing to want to use to me,

Why would you want to use a Panel to handle keyboard input?

Share this post


Link to post
Share on other sites
My guess is since it is a container that they expect all of the controls on it to handle the key events not the panel itself.

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
My guess is since it is a container that they expect all of the controls on it to handle the key events not the panel itself.

Exactly. I would never expect a container to handle key input.

Share this post


Link to post
Share on other sites
You "could" use it for some short cut keys or something like that. I wouldn't really use it but i am sure if we tried hard enough we could find a reason.

theTroll

Share this post


Link to post
Share on other sites



protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
{
base.OnPreviewKeyDown(e);
}


and


this.DoubleBuffered = true;

Share this post


Link to post
Share on other sites
Well, if you're using a user-painted panel as a 'poor man's custom control', you'd want to get the keyboard events on it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bob Janova
Well, if you're using a user-painted panel as a 'poor man's custom control', you'd want to get the keyboard events on it.


Assuming you had no other controls on it, yeah. Why would you do that though? [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement