Click here to Skip to main content
Licence CPOL
First Posted 16 Jul 2007
Views 20,151
Downloads 162
Bookmarked 24 times

Simple Input Interface for XNA

By | 16 Jul 2007 | Article
Makes event handlers available to game developers using Microsoft's XNA.

Introduction

As input is an important part of every game, it should not be difficult to use. C# provides a great feature - event handlers - which have not been used in the XNA framework to handle input. This class, CInput, will enable event handlers for XNA.

Using the code

All you have to do to use this code is add the Input.cs file to your project, create an instance of CInput, and call its Update function in the game.

//Excerpts from Game.cs

namespace YourGame
{
    public class YourGame : Microsoft.Xna.Framework.Game
    {
        //The variable holding a reference to the input system

        VisualIllusions.CInput Input;
        
        //Constructor

        public YourGame()
        {
            //Create the input device

            Input = new VisualIllusions.CInput();
            //Add any event handlers here or in the initialize function

            Input.OnMouseDown += new 
              VisualIllusions.CInput.OnMouseDownDelegate(Input_OnMouseDown);
        }
        
        //Update the game

        protected override void Update(GameTime gameTime)
        {
            //Update the input

            Input.Update();

            base.Update(gameTime);
        }
        
        //A function called by an event

        void Input_OnMouseDown(VisualIllusions.CInput.MouseButtons b)
        {
            System.Diagnostics.Debug.Print("A mouse button was pressed: " + 
                                           b.ToString());
        }
        
        [...]
    }
}

The class is quite simple to use, and you should be able to integrate it into your project easily by taking a look at the sample project.

Points of interest

I am currently working on GamePad support and a binding class to make it easy to dynamically bind actions to specific keys. This binding class will be serializable so that everyone can configure his/her own set of keys.

History

  • First release, and probably buggy. Let me know about any problems you notice!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

tillmyspace



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAbout event base systems in games... PinmemberAmadrias6:24 24 Jul '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 16 Jul 2007
Article Copyright 2007 by tillmyspace
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid