InputClass.cs :  » Game » Killer-Instinct » KillerInstinct » DirectX » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » Killer Instinct 
Killer Instinct » KillerInstinct » DirectX » InputClass.cs
using System;
using System.Drawing;
using Microsoft.DirectX.DirectInput;
using Global;
using Global.GlobalClass;

namespace KillerInstinct.DirectX{
  /// <summary>
  /// Summary description for InputClass.
  /// </summary>
  public class InputClass
  {
    // keyboard
    Device      KeyboardDevice;
    Key[]      m_pressedKeys;
    // Mouse
    Device      MouseDevice;
    MouseState    MouseStateData = new MouseState();
    byte[]      MouseButtons;
    public byte    MButtonLeft    = 0;
    public byte    MButtonRight  = 1;
    public byte    MButtonMiddle = 2;
    public bool[]  MouseDown = new bool[3];
    public int    MouseX = 0;
    public int    MouseY = 0;
    public InputClass(System.Windows.Forms.Control window)
    {
      try
      {
        // keyboard
        KeyboardDevice = new Device(SystemGuid.Keyboard);
        KeyboardDevice.SetCooperativeLevel(window, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
        KeyboardDevice.Acquire();
        // mouse
        MouseDevice = new Device(SystemGuid.Mouse);
        MouseDevice.SetCooperativeLevel(window, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
        MouseDevice.Acquire();
      }
      catch (InputException)
      {
        FreeDirectInput();
        throw new Exception("Unable to initialize Input device!");
      }
    }
    // returns true if any key pressed
    public bool IsKeypressed()
    {
      if (m_pressedKeys.Length > 0)
        return true;
      else
        return false;
    }
    // keyboard
    public void GetKeyboardInput()
    {
      if (KeyboardDevice != null)
        m_pressedKeys = KeyboardDevice.GetPressedKeys();
    }
    // returns string with pressed alpha-numeric characters
    public void GetString(ref string keys)
    {
      bool shift = false;

      // is a shift key pressed?
      if (PressedKey(Key.LeftShift, Key.RightShift))
        shift = true;

      // search entire buffer for alpha-numeric characters
      for (int i=0; i < m_pressedKeys.Length; ++i)
      {
        switch (m_pressedKeys[i])
        {
          case Key.A:
          case Key.B:
          case Key.C:
          case Key.D:
          case Key.E:
          case Key.F:
          case Key.G:
          case Key.H:
          case Key.I:
          case Key.J:
          case Key.K:
          case Key.L:
          case Key.M:
          case Key.N:
          case Key.O:
          case Key.P:
          case Key.Q:
          case Key.R:
          case Key.S:
          case Key.T:
          case Key.U:
          case Key.V:
          case Key.W:
          case Key.X:
          case Key.Y:
          case Key.Z:
            if (shift)
              keys += m_pressedKeys[i].ToString().ToUpper();
            else
              keys += m_pressedKeys[i].ToString().ToLower();
            break;
          case Key.D0:
          case Key.D1:
          case Key.D2:
          case Key.D3:
          case Key.D4:
          case Key.D5:
          case Key.D6:
          case Key.D7:
          case Key.D8:
          case Key.D9:
            keys += m_pressedKeys[i].ToString();
            break;
          case Key.BackSpace:
            // remove last (if any) character
            if (keys.Length > 0)
              keys = keys.Remove(keys.Length-1, 1);
            break;
        }
      }
    }
    // Overloaded. checks if a single key was pressed
    public bool PressedKey(Key key)
    {
      return (PressedKey(key, key));
    }
    // checks if one of the following keys was pressed
    public bool PressedKey(Key key1, Key key2)
    {
      for (byte i=0; i < m_pressedKeys.Length; ++i)
      {
        if (key1 == m_pressedKeys[i] || key2 == m_pressedKeys[i])
          return true;
      }
      return false;
    }
    // mouse
    public void GetMouseInput()
    {
      if (MouseDevice != null)
      {
        MouseStateData = MouseDevice.CurrentMouseState;
        MouseButtons = MouseStateData.GetMouseButtons();
      }
    }
    public bool PressedMButton(byte button)
    {
      if (MouseButtons[button] != 0)
        MouseDown[button] = true;
      // up and down
      if (MouseDown[button] && (MouseButtons[button]==0))
      {
        MouseDown[button] = false;
        return true;
      }
      return false;
    }
    public Point GetMouseXY()
    {
      return new Point(MouseX, MouseY);
    }
    // for keyboard and mouse
    public void ReInit()
    {
      KeyboardDevice.Unacquire();
      KeyboardDevice.Acquire();
      MouseDevice.Unacquire();
      MouseDevice.Acquire();
    }
    public void FreeDirectInput()
    {
      if (KeyboardDevice != null) 
      {
        KeyboardDevice.Unacquire();
        KeyboardDevice.Dispose();
        KeyboardDevice = null;
      }
      if (MouseDevice != null) 
      {
        MouseDevice.Unacquire();
        MouseDevice.Dispose();
        MouseDevice = null;
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.