/InputSystem/Tests/KeyboardTests.cs
C# | 211 lines | 166 code | 14 blank | 31 comment | 6 complexity | c35f7af51525b1e38abe2fc1e9c34330 MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.Diagnostics;
- using System.IO;
- using Delta.Engine;
- using Delta.InputSystem.Devices;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
- using NUnit.Framework;
-
- namespace Delta.InputSystem.Tests
- {
- /// <summary>
- /// Keyboard tests
- /// </summary>
- [Category("Visual")]
- internal class KeyboardTests
- {
- #region CheckKeyboardStates (Static)
- /// <summary>
- /// Check Keyboard states
- /// </summary>
- [Test]
- public static void CheckKeyboardStates()
- {
- Application.Start(delegate
- {
- Font.Default.Draw(
- "Keyboard IsConnected " + Input.Keyboard.IsConnected,
- Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
- Font.Default.Draw(
- "A is " + Input.Keyboard.GetState(InputButton.A),
- Rectangle.FromCenter(new Point(0.5f, 0.50f), Size.Half));
- Font.Default.Draw(
- "CursorUp is " + Input.Keyboard.GetState(InputButton.CursorUp),
- Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
- });
- }
- #endregion
-
- #region TestVirtualKeyboardCursorPosition (Static)
- /// <summary>
- /// Test virtual keyboard cursor position
- /// </summary>
- [Test]
- public static void TestVirtualKeyboardCursorPosition()
- {
- Application.Start(delegate
- {
- Font.Default.Draw(
- "Keyboard Position " + Input.Keyboard.Position,
- Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
- Font.Default.Draw(
- "Cursor Movement " + Input.Keyboard.CursorMovement,
- Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
- // Show the virtual keyboard cursor
- Circle.DrawOutline(Input.Keyboard.Position, 0.025f, Color.Red);
- });
- }
- #endregion
-
- #region TestCtrlAltShift (Static)
- /// <summary>
- /// TestCtrlAltShift, needs an extra test because Control, Alt and Shift
- /// are combined keys, we will get them separately from the input classes.
- /// </summary>
- [Test]
- public static void TestCtrlAltShift()
- {
- Application.Start(delegate
- {
- Font.Default.Draw(
- "WindowsKey " + Input.Keyboard.GetState(InputButton.WindowsKey),
- Rectangle.FromCenter(new Point(0.5f, 0.2f), Size.Half));
- Font.Default.Draw(
- "LeftWindows " + Input.Keyboard.GetState(InputButton.LeftWindows),
- Rectangle.FromCenter(new Point(0.5f, 0.25f), Size.Half));
- Font.Default.Draw(
- "RightWindows " + Input.Keyboard.GetState(InputButton.RightWindows),
- Rectangle.FromCenter(new Point(0.5f, 0.3f), Size.Half));
- Font.Default.Draw(
- "Control " + Input.Keyboard.GetState(InputButton.Control),
- Rectangle.FromCenter(new Point(0.5f, 0.35f), Size.Half));
- Font.Default.Draw(
- "LeftControl " + Input.Keyboard.GetState(InputButton.LeftControl),
- Rectangle.FromCenter(new Point(0.5f, 0.4f), Size.Half));
- Font.Default.Draw(
- "RightControl " + Input.Keyboard.GetState(InputButton.RightControl),
- Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
- Font.Default.Draw(
- "Alt " + Input.Keyboard.GetState(InputButton.Alt),
- Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
- Font.Default.Draw(
- "LeftAlt " + Input.Keyboard.GetState(InputButton.LeftAlt),
- Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
- Font.Default.Draw(
- "RightAlt " + Input.Keyboard.GetState(InputButton.RightAlt),
- Rectangle.FromCenter(new Point(0.5f, 0.6f), Size.Half));
- Font.Default.Draw(
- "Shift " + Input.Keyboard.GetState(InputButton.Shift),
- Rectangle.FromCenter(new Point(0.5f, 0.65f), Size.Half));
- Font.Default.Draw(
- "LeftShift " + Input.Keyboard.GetState(InputButton.LeftShift),
- Rectangle.FromCenter(new Point(0.5f, 0.7f), Size.Half));
- Font.Default.Draw(
- "RightShift " + Input.Keyboard.GetState(InputButton.RightShift),
- Rectangle.FromCenter(new Point(0.5f, 0.75f), Size.Half));
- });
- }
- #endregion
-
- #region ResearchStartOSK (Static)
- /// <summary>
- /// Research start OSK
- /// </summary>
- [Test]
- public static void ResearchStartOSK()
- {
- string winDirectory = Environment.GetEnvironmentVariable("WINDIR");
-
- // Start with the default path of Windows7 (and Vista ? too)
- string oskFilePath = Path.Combine(winDirectory, "system32", "osk.exe");
-
- Assert.True(FileHelper.Exists(oskFilePath));
-
- Process oskProcess = null;
- Application.Start(delegate
- {
- if (Input.Keyboard.SpaceReleased)
- {
- if (oskProcess != null)
- {
- oskProcess.Kill();
- oskProcess = null;
- } // if
-
- oskProcess = Process.Start(oskFilePath);
- } // if
- });
- }
- #endregion
-
- #region ShowOnScreenKeyboard (Static)
- /// <summary>
- /// Tests on screen keyboard, works with Windows and Xna Input module on
- /// PC and on all mobile and console platforms.
- /// </summary>
- [Test]
- public static void ShowOnScreenKeyboard()
- {
- BaseKeyboard keyboard = Input.Keyboard;
-
- keyboard.ForceShowOnScreenKeyboard();
- Application.Start(delegate
- {
- if (keyboard.CursorUpReleased)
- {
- keyboard.ForceShowOnScreenKeyboard();
- } // if
- if (keyboard.CursorDownReleased)
- {
- keyboard.HideOnScreenKeyboard();
- } // if
-
- Font.Default.Draw(
- "Press 'CursorUp' to show and 'CursorDown' to hide the " +
- "OnScreen keyboard again",
- Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
- });
- }
- #endregion
-
- #region TestTextboxHandleInput (Static)
- /// <summary>
- /// Test textbox handle input, just displays a label and handles it like
- /// a textbox, which is input controlled by the Input.Keyboard class.
- /// This test is limited to 10 fps and especially useful to keyboard event
- /// handling via WindowsKeyboard (as opposed to the polling XnaKeyboard)
- /// </summary>
- [Test]
- public static void TestTextboxHandleInput()
- {
- // Limit to 10 fps to really test if we can still type text!
- //works even fine with 1 fps: Settings.Debug.LimitFramerateNumber = 1;
- Settings.Extra.LimitFramerateNumber = 10;
-
- string someText = "";
- Application.Start(delegate
- {
- // Show text and some own little blinking cursor logic
- Font.Default.Draw(
- "Enter Text: " + someText +
- ((int)(Time.Milliseconds / 200) % 2 == 0
- ? "|"
- : ""),
- Rectangle.FromCenter(new Point(0.5f, 0.35f), Size.Half));
- Input.Keyboard.HandleInput(ref someText);
-
- Font.Default.Draw(
- "BackSpace " + Input.Keyboard.GetState(InputButton.BackSpace),
- Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
- Font.Default.Draw(
- "Space " + Input.Keyboard.SpaceIsPressed,
- Rectangle.FromCenter(new Point(0.5f, 0.6f), Size.Half));
- });
- }
- #endregion
- }
- }