XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Keyboard events in XNA 2.0

Last post 9/24/2008 7:06 AM by Rupe. 3 replies.
  • 9/22/2008 1:28 PM

    Keyboard events in XNA 2.0

    I have been looking around and I can't seem to find any information on this.

    I remember in C#, I was able to use events. I believe, and correct me if I am wrong, but events would be more efficient than checking for keyboard state 60 times a second. I was wondering if XNA had something along the lines of

    Keyboard k;

    k.onKeyDown+=blahblahblah

    I can't seem to find anyway to handle Keyboard and Mouse input with events in XNA. Any help would be appreciated! Thanks!

  • 9/22/2008 2:18 PM In reply to
    • (2340)
    • premium membership MVP
    • Posts 1,225

    Re: Keyboard events in XNA 2.0

    The keyboard events are part of the Form class in System.Windows.Forms.   The GameWindow you get when you use the Game class doesn't use WinForms (or maybe it does, it doesn't matter since it's not exposed to you as one) so you don't get the keyboard events.

    However I don't see why you think events would be more efficient than polling the keyboard state.  It's not like every time you call KeyboardState.IsKeyDown you 're enacting a bunch of keyboard I/O routines or anything...you've already retrieved a snapshot of keyboard state when you call GetState.  From that point onward you're basically just retrieving values out of a struct, which is probably just accessing an array.  Events on the other hand require calling multiple delegates every time the state of a single key changes...that's not exactly cheap. 

    If you really want events and you're not working with WinForms, it shouldn't be hard at all to set up something yourself.  You can just make a KeyboardInput class, and expose an OnKeyDown and OnKeyUp events.  Then just give the class an Update function that you call once a frame, and in that Update function you can check the keyboard state and fire events as needed.  The GetPressedKeys method of KeyboardState might be useful for this purpose.



    Matt Pettineo | DirectX/XNA MVP


    Ride into The Danger Zone | PIX With XNA Tutorial
  • 9/22/2008 8:42 PM In reply to

    Re: Keyboard events in XNA 2.0

    Phreakuency:
    I have been looking around and I can't seem to find any information on this.

    I remember in C#, I was able to use events. I believe, and correct me if I am wrong, but events would be more efficient than checking for keyboard state 60 times a second.

    If you're looking for efficiency, you'll be just fine using XNA's Keyboard.GetState() and processing up and down keys in your Update method. After all, you're most likely coding a single-threaded app in XNA, rather than the asynchronous environment of a standard Winforms app. It's not like you have a dedicated thread that's polling the keyboard state.

    If you really do need to hook into the Windows message pump for your events, you can always reference System.Windows.Forms and implement an IMessageFilter, register it with Application.AddMessageFilter(), and then fire your own keyboard-based events from within your filter. I doubt you'll be any more efficient (probably less so, with the cross-thread sync you'll have to do), but it does occasionally make certain things possible. For instance, I have used this so I can get raw input data from nonstandard devices.

  • 9/24/2008 7:06 AM In reply to

    Re: Keyboard events in XNA 2.0

    I like the Windows Forms events too. I did my own input events because I prefer being able to hook and unhook event handlers easily.

    I don't know if I took the long way around, but I created my own input manager with KeyboardEventArgs, MouseEventArgs, & GamePadEventArgs.
    The InputManager checks the current input devices states against the previous states and fires off events such as KeyboardPressed, KeyboardHeld, & KeyboardReleased (similarly for Mouse and GamePad).

    I can share the implementation details if you're interested.


Page 1 of 1 (4 items) Previous Next