General-Purpose Keyboard Input (C#)

I’ve written the code below as a general-purpose keyboard input routine for C#. (It should be fairly easy to convert this to any other language.)

Basically, there are 2 types of commands to track: repeating and non-repeating. Repeating commands are those that should continuously fire. Examples of these are moving and turning. Non-repeating commands are those that should NOT continuously fire. Examples of these are pausing the game and jumping.

In other words, the way I’ve set it up here (at least for my game) allows the player to continuously move and turn the camera while those keys are pressed, but he must press and release a key to either jump or pause the game. (This of course may be different in your game; this is only for demonstration purposes.)

You’ll notice that some commands are grouped together in if-then-else blocks while others stand alone. The reason for this is that certain commands (at least in my game) should not be allowed to be issued at the same time. For example, you shouldn’t be able to turn left AND turn right at the same time, right? I also don’t want the player to be able to attack (with a sword), fire his main weapon and secondary weapon all at the same time; it should be only one of those at a time. However, the player CAN jump and attack at the same time, since these are not part of the same if-then-else block. (But again, this is how my game works; do whatever you want with yours!)

Note that there are many ways to optimize this code. For example, in the actual implementation of this code in my game, I use an array of command keys to allow the player to modify which keys to use for which commands. (In other words, the player can change which key is used to move forward and which is to used to fire his main weapon.) The code below uses hard-coded key values to issue commands; it is never desirable to hard-code anything if it can be avoided.

Let me know if you have problems with this. You are free to use this code for your own projects. You don’t have to pay me but a thank-you would be nice! :) Enjoy!

[written by Omnigames: games@omnigalactic.com]


using System;
using System.Windows.Forms;
using TrueVision3D;

namespace MyGame
{
   public class MyGameForm : System.Windows.Forms.Form
   {
      private static TVInputEngine INPUT_ENGINE = new TVInputEngineClass();
      private static Array KEY_BUFFER = new TV_KEYDATA[256];
      private static int BUFFER_COUNT = 0;
      private static Array KEY_PRESSED = new byte[256];

      //...........................................................................
      private void GetUserInput()
      {
         int key;

         /////////////////////////////////////////////////////////////////////
         // non-repeating commands
         //    (commands that should not continuously fire)
         /////////////////////////////////////////////////////////////////////

         INPUT_ENGINE.GetKeyBuffer(ref KEY_BUFFER, ref BUFFER_COUNT);

         for (int i = 0; i < BUFFER_COUNT; i ++)
         {
            if (((TV_KEYDATA) KEY_BUFFER.GetValue(i)).Pressed != 0)
            {
               key = (int) ((TV_KEYDATA) KEY_BUFFER.GetValue(i)).Key;

               //""""""""""""""""""""""""""""""""""""""""""""""""
               // pause / end game

               if (key == (int) CONST_TV_KEY.TV_KEY_F12)
               {
                  // ### pause game here
               }
               else if (key == (int) CONST_TV_KEY.TV_KEY_ESCAPE)
               {
                  // ### end game here
               }

               //""""""""""""""""""""""""""""""""""""""""""""""""
               // jump and attack

               // jump
               if (key == (int) CONST_TV_KEY.TV_KEY_J)
               {
                  // ### jump here
               }

               // attack / fire main weapon / fire second weapon
               if (key == (int) CONST_TV_KEY.TV_KEY_SPACE)
               {
                  // ### attack here
               }
               else if (key == (int) CONST_TV_KEY.TV_KEY_A)
               {
                  // ### fire main weapon here
               }
               else if (key == (int) CONST_TV_KEY.TV_KEY_B)
               {
                  // ### fire second weapon here
               }

               //""""""""""""""""""""""""""""""""""""""""""""""""
               // interact / show map / show options

               if (key == (int) CONST_TV_KEY.TV_KEY_I)
               {
                  // ### interact here
               }
               else if (key == (int) CONST_TV_KEY.TV_KEY_M)
               {
                  // ### show map here
               }
               else if (key == (int) CONST_TV_KEY.TV_KEY_O)
               {
                  // ### show character options here
               }

               //""""""""""""""""""""""""""""""""""""""""""""""""
               // cheat mode

               if (key == (int) CONST_TV_KEY.TV_KEY_C)
               {
                  // ### enable cheat mode here
               }
            }
         }

         //===================================================================

         /////////////////////////////////////////////////////////////////////
         // repeating commands
         //    (commands that should continuously fire)
         /////////////////////////////////////////////////////////////////////

         INPUT_ENGINE.GetKeyPressedArray(ref KEY_PRESSED);

         // turn left / turn right
         if ((byte) KEY_PRESSED.GetValue((int) CONST_TV_KEY.TV_KEY_LEFTARROW) != 0)
         {
            // ### turn player/camera left here
         }
         else if ((byte) KEY_PRESSED.GetValue((int) CONST_TV_KEY.TV_KEY_RIGHTARROW) != 0)
         {
            // ### turn player/camera right here
         }

         // move forward / move backward
         if ((byte) KEY_PRESSED.GetValue((int) CONST_TV_KEY.TV_KEY_UPARROW) != 0)
         {
            // ### move player/camera forward here
         }
         else if ((byte) KEY_PRESSED.GetValue((int) CONST_TV_KEY.TV_KEY_DOWNARROW) != 0)
         {
            // ### move player/camera backward here
         }
      }
   }
}
//
 
tutorialsarticlesandexamples/general-purpose_keyboard_input.txt · Last modified: 2013/11/22 13:32