Some work on keyboard input. It is currently not working, see issue #449 for further information

This commit is contained in:
Glatzemann 2011-11-14 19:37:05 +00:00
parent 2c30d8ff7e
commit e02485ab9e
11 changed files with 96 additions and 14 deletions

View File

@ -70,7 +70,8 @@ namespace ANX.Framework
this.game = game;
//this.LockThreadToProcessor();
this.gameWindow = new WindowsGameWindow();
Mouse.WindowHandle = this.gameWindow.Handle;
Mouse.WindowHandle = this.gameWindow.Handle; //TODO: find a way to initialize all InputSystems with one Handle
Keyboard.WindowHandle = this.gameWindow.Handle;
//TouchPanel.WindowHandle = this.gameWindow.Handle;
//this.gameWindow.IsMouseVisible = game.IsMouseVisible;
this.gameWindow.Activated += new EventHandler<EventArgs>(this.GameWindowActivated);

View File

@ -62,10 +62,26 @@ namespace ANX.Framework.Input
keyboard = AddInSystemFactory.Instance.GetDefaultCreator<IInputSystemCreator>().Keyboard;
}
internal static IntPtr WindowHandle
{
get
{
return keyboard != null ? keyboard.WindowHandle : IntPtr.Zero;
}
set
{
if (keyboard != null)
{
keyboard.WindowHandle = value;
}
}
}
public static KeyboardState GetState()
{
return keyboard.GetState();
}
public static KeyboardState GetState (PlayerIndex playerIndex)
{
return keyboard.GetState(playerIndex);

View File

@ -77,7 +77,7 @@ namespace ANX.Framework.Input
public bool IsKeyDown(Keys key)
{
return keyState[(int)key] == KeyState.Down;
return keyState != null ? keyState[(int)key] == KeyState.Down : false;
}
public bool IsKeyUp(Keys key)

View File

@ -62,7 +62,7 @@ namespace ANX.Framework.Input
mouse = AddInSystemFactory.Instance.GetDefaultCreator<IInputSystemCreator>().Mouse;
}
public static IntPtr WindowHandle
internal static IntPtr WindowHandle
{
get
{

View File

@ -6,8 +6,9 @@ using ANX.Framework.Input;
namespace ANX.Framework.NonXNA
{
public interface IKeyboard
public interface IKeyboard : IDisposable
{
IntPtr WindowHandle { get; set; }
KeyboardState GetState();
KeyboardState GetState(PlayerIndex playerIndex);
}

View File

@ -42,6 +42,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />

View File

@ -60,7 +60,7 @@ namespace ANX.InputSystem.Windows.XInput
{
public class Creator : IInputSystemCreator
{
#region IInputSystemCreator Member
public string Name
{
@ -94,9 +94,6 @@ namespace ANX.InputSystem.Windows.XInput
{
get { return new Keyboard(); }
}
#endregion
}
}

View File

@ -56,6 +56,21 @@ namespace ANX.InputSystem.Windows.XInput
{
internal class FormatConverter
{
public static KeyboardState Translate(SharpDX.DirectInput.KeyboardState keyboardState)
{
int keyCount = keyboardState.PressedKeys.Count;
Keys[] keys = new Keys[keyCount];
for (int i = 0; i < keyCount; i++)
{
keys[i] = (ANX.Framework.Input.Keys)((int)keyboardState.PressedKeys[i]);
}
KeyboardState ks = new KeyboardState(keys);
return ks;
}
public static Buttons Translate(SharpDX.XInput.GamepadButtonFlags buttons)
{
Buttons tb = 0;

View File

@ -55,19 +55,70 @@ using DXKeyboard=SharpDX.DirectInput.Keyboard;
// particular purpose and non-infringement.
#endregion // License
namespace ANX.InputSystem.Windows.XInput
{
public class Keyboard:IKeyboard
public class Keyboard : IKeyboard
{
private DXKeyboard keyboroard;
#region Private Members
private DirectInput directInput;
private DXKeyboard nativeKeyboard;
private KeyboardState nativeState;
#endregion // Private Members
public IntPtr WindowHandle
{
get;
set;
}
public Keyboard()
{
this.nativeState = new KeyboardState();
}
public Framework.Input.KeyboardState GetState()
{
throw new NotImplementedException();
return GetState(Framework.PlayerIndex.One);
}
public Framework.Input.KeyboardState GetState(Framework.PlayerIndex playerIndex)
{
throw new NotImplementedException();
if (this.nativeKeyboard == null && this.WindowHandle != null && this.WindowHandle != IntPtr.Zero)
{
this.directInput = new DirectInput();
this.nativeKeyboard = new DXKeyboard(this.directInput);
this.nativeKeyboard.SetCooperativeLevel(this.WindowHandle, CooperativeLevel.NonExclusive | CooperativeLevel.Background);
this.nativeKeyboard.Acquire();
}
if (this.nativeKeyboard != null)
{
nativeKeyboard.GetCurrentState(ref this.nativeState);
if (this.nativeState.PressedKeys.Count > 0)
{
return FormatConverter.Translate(this.nativeState);
}
}
return new Framework.Input.KeyboardState();
}
public void Dispose()
{
if (this.nativeKeyboard != null)
{
this.nativeKeyboard.Unacquire();
this.nativeKeyboard.Dispose();
this.nativeKeyboard = null;
}
if (this.directInput != null)
{
this.directInput.Dispose();
this.directInput = null;
}
}
}
}

View File

@ -62,7 +62,7 @@ using MouseX = SharpDX.DirectInput.Mouse;
namespace ANX.InputSystem.Windows.XInput
{
class Mouse:IMouse
class Mouse : IMouse
{
[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point lpPoint);

View File

@ -109,7 +109,7 @@ namespace WindowsGame1
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
{
this.Exit();
}