Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by nullsmind May 7, 2006 at 6:43 PM 10 replies 9.9k views
Original Post
nullsmind
nullsmind
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]
TheTroll
TheTroll
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
nullsmind
nullsmind
I wonder why VS05 hides the events unless I create a custom control.
TheTroll
TheTroll
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
Bob Janova
Bob Janova
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.
Machaira
Machaira
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?
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development
TheTroll
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.

theTroll
Machaira
Machaira
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.
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development
TheTroll
TheTroll
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
RipTorn
RipTorn

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


and

this.DoubleBuffered = true;
Bob Janova
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.
Machaira
Machaira
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]
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.