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;
}
}
}
}
|