diff --git a/ANX.Framework.Windows.DX10/WindowsGameHost.cs b/ANX.Framework.Windows.DX10/WindowsGameHost.cs index 2d40a828..f0712769 100644 --- a/ANX.Framework.Windows.DX10/WindowsGameHost.cs +++ b/ANX.Framework.Windows.DX10/WindowsGameHost.cs @@ -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(this.GameWindowActivated); diff --git a/ANX.Framework/Input/Keyboard.cs b/ANX.Framework/Input/Keyboard.cs index 3d96f331..4e53d8dd 100644 --- a/ANX.Framework/Input/Keyboard.cs +++ b/ANX.Framework/Input/Keyboard.cs @@ -62,10 +62,26 @@ namespace ANX.Framework.Input keyboard = AddInSystemFactory.Instance.GetDefaultCreator().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); diff --git a/ANX.Framework/Input/KeyboardState.cs b/ANX.Framework/Input/KeyboardState.cs index 00235215..ae592c57 100644 --- a/ANX.Framework/Input/KeyboardState.cs +++ b/ANX.Framework/Input/KeyboardState.cs @@ -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) diff --git a/ANX.Framework/Input/Mouse.cs b/ANX.Framework/Input/Mouse.cs index d7b09808..c9444dab 100644 --- a/ANX.Framework/Input/Mouse.cs +++ b/ANX.Framework/Input/Mouse.cs @@ -62,7 +62,7 @@ namespace ANX.Framework.Input mouse = AddInSystemFactory.Instance.GetDefaultCreator().Mouse; } - public static IntPtr WindowHandle + internal static IntPtr WindowHandle { get { diff --git a/ANX.Framework/NonXNA/InputSystem/IKeyboard.cs b/ANX.Framework/NonXNA/InputSystem/IKeyboard.cs index 50225e96..c6599c11 100644 --- a/ANX.Framework/NonXNA/InputSystem/IKeyboard.cs +++ b/ANX.Framework/NonXNA/InputSystem/IKeyboard.cs @@ -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); } diff --git a/ANX.InputSystem.Windows.XInput/ANX.InputSystem.Windows.XInput.csproj b/ANX.InputSystem.Windows.XInput/ANX.InputSystem.Windows.XInput.csproj index d1300ae5..c40891f6 100644 --- a/ANX.InputSystem.Windows.XInput/ANX.InputSystem.Windows.XInput.csproj +++ b/ANX.InputSystem.Windows.XInput/ANX.InputSystem.Windows.XInput.csproj @@ -42,6 +42,7 @@ + diff --git a/ANX.InputSystem.Windows.XInput/Creator.cs b/ANX.InputSystem.Windows.XInput/Creator.cs index a7b6db3d..785a0362 100644 --- a/ANX.InputSystem.Windows.XInput/Creator.cs +++ b/ANX.InputSystem.Windows.XInput/Creator.cs @@ -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 - - } } diff --git a/ANX.InputSystem.Windows.XInput/FormatConverter.cs b/ANX.InputSystem.Windows.XInput/FormatConverter.cs index 4c4c8006..4665aeb0 100644 --- a/ANX.InputSystem.Windows.XInput/FormatConverter.cs +++ b/ANX.InputSystem.Windows.XInput/FormatConverter.cs @@ -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; diff --git a/ANX.InputSystem.Windows.XInput/Keyboard.cs b/ANX.InputSystem.Windows.XInput/Keyboard.cs index 954173be..94f4c0f6 100644 --- a/ANX.InputSystem.Windows.XInput/Keyboard.cs +++ b/ANX.InputSystem.Windows.XInput/Keyboard.cs @@ -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; + } } } } diff --git a/ANX.InputSystem.Windows.XInput/Mouse.cs b/ANX.InputSystem.Windows.XInput/Mouse.cs index 5a501e64..298e4d63 100644 --- a/ANX.InputSystem.Windows.XInput/Mouse.cs +++ b/ANX.InputSystem.Windows.XInput/Mouse.cs @@ -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); diff --git a/Samples/WindowsGame/Game1.cs b/Samples/WindowsGame/Game1.cs index 161af515..a3db6a93 100644 --- a/Samples/WindowsGame/Game1.cs +++ b/Samples/WindowsGame/Game1.cs @@ -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(); }