public List <InputDeviceEvent> GetEvents() { ks = keyboard.GetCurrentState(); KeyboardUpdate[] ke = keyboard.GetBufferedData(); List <InputDeviceEvent> events = new List <InputDeviceEvent>(); foreach (KeyboardUpdate k in ke) { //System.Diagnostics.Debug.WriteLine("key " + k.Key + " " + k.IsPressed ); events.Add(new InputDeviceEvent(this, (int)k.Key, k.IsPressed)); } return((events.Count > 0) ? events : null); }
public override Action Start() { IntPtr handle = Process.GetCurrentProcess().MainWindowHandle; KeyboardDevice = new SharpDX.DirectInput.Keyboard(DirectInputInstance); if (KeyboardDevice == null) { throw new Exception("Failed to create keyboard device"); } KeyboardDevice.SetCooperativeLevel(handle, CooperativeLevel.Background | CooperativeLevel.NonExclusive); KeyboardDevice.Acquire(); KeyboardDevice.GetCurrentState(ref KeyState); mouseDevice = new SharpDX.DirectInput.Mouse(DirectInputInstance); if (mouseDevice == null) { throw new Exception("Failed to create mouse device"); } mouseDevice.SetCooperativeLevel(handle, CooperativeLevel.Background | CooperativeLevel.NonExclusive); mouseDevice.Properties.AxisMode = DeviceAxisMode.Relative; // Get delta values mouseDevice.Acquire(); getPressedStrategy = new GetPressedStrategy <int>(IsDown); setPressedStrategy = new SetPressedStrategy <int>(KeyOrButtonDown, KeyOrButtonUp); OnStarted(this, new EventArgs()); return(null); }
public List <InputDeviceEvent> GetEvents() // Events use keys enumeration { ks = keyboard.GetCurrentState(); KeyboardUpdate[] ke = keyboard.GetBufferedData(); List <InputDeviceEvent> events = new List <InputDeviceEvent>(); foreach (KeyboardUpdate k in ke) { Keys ky = SharpKeyConversion.SharpKeyToKeys(k.Key); //System.Diagnostics.Debug.WriteLine("** Sharp key " + k.Key + " " + (int)k.Key + k.IsPressed); //System.Diagnostics.Debug.WriteLine( " => " + ky.ToString() + " norm " + ky.VKeyToString() + ":" + (int)ky ); events.Add(new InputDeviceEvent(this, (int)ky, k.IsPressed)); } return((events.Count > 0) ? events : null); }
protected override void UpdateKeyStates() { nativeKeyboard.GetCurrentState(ref nativeState); for (int i = 0; i < (int)Key.NumberOfKeys; i++) { UpdateKeyState(i, nativeState.IsPressed(KeyboardKeyMapper.Translate((Key)i))); } }
public override void DoBeforeNextExecute() { KeyboardDevice.GetCurrentState(ref KeyState); if (Wheelturn != 0) { setPressedStrategy.Add(Wheelturn < 0 ? (int)Mouse.WheelBwd : (int)Mouse.WheelFwd); Wheelturn = Wheelturn - Math.Sign(Wheelturn); } if (Tempo != null && Tempo[0].GetLapse() >= Tempo[1]) { Tempo[0] = StopTimer(); Tempo = null; foreach (var code in Codes.Reverse()) { KeyOrButtonUp(code); } Codes = null; } setPressedStrategy.Do(); CheckScriptTimer(); if ((cmd == 'I') && setPressedStrategy.IsListEmpty()) { if (SF == null) { SF = new ScriptKeyboardAndMouse(this); } SF.KeyboardAndMouse(); } // If a mouse command was given in the script, issue it all at once right here if ((int)deltaXOut != 0 || (int)deltaYOut != 0 || wheel != 0) { var input = new MouseKeyIO.INPUT[1]; input[0].type = MouseKeyIO.INPUT_MOUSE; input[0].mi = MouseInput((int)deltaXOut, (int)deltaYOut, (uint)wheel, 0, MouseKeyIO.MOUSEEVENTF_MOVE | MouseKeyIO.MOUSEEVENTF_WHEEL); MouseKeyIO.NativeMethods.SendInput(1, input, Marshal.SizeOf(input[0].GetType())); // Reset the mouse values if ((int)deltaXOut != 0) { deltaXOut = deltaXOut - (int)deltaXOut; } if ((int)deltaYOut != 0) { deltaYOut = deltaYOut - (int)deltaYOut; } wheel = 0; } currentMouseState = null; // flush the mouse state }
/// <summary> /// Collect the current data from the device /// </summary> public override void GetData( ) { // Make sure there is a valid device. if (null == m_device) { return; } // Poll the device for info. try { m_device.Poll( ); } catch (SharpDXException e) { if ((e.ResultCode == ResultCode.NotAcquired) || (e.ResultCode == ResultCode.InputLost)) { // Check to see if either the app needs to acquire the device, or // if the app lost the device to another process. try { // Acquire the device - if the (main)window is active if (Activated) { m_device.Acquire( ); } } catch (SharpDXException) { // Failed to acquire the device. This could be because the app doesn't have focus. return; // EXIT unaquired } } else { log.Error("Unexpected Poll Exception", e); return; // EXIT see ex code } } // Get the state of the device - retaining the previous state to find the lates change try { m_state = m_device.GetCurrentState( ); } // Catch any exceptions. None will be handled here, // any device re-aquisition will be handled above. catch (SharpDXException) { return; } }
public bool ReadKeyboard() { var resultCode = SharpDX.DirectInput.ResultCode.Ok; try { _Keyboard.GetCurrentState(ref _KeyboardState); } catch (SharpDX.SharpDXException ex) { resultCode = ex.Descriptor; } catch (Exception ex) { MainWindow.MessageBox((IntPtr)0, "cannot get keyboard info 00: " + ex.ToString() + "", "Oculus error", 0); return(false); } if (resultCode == SharpDX.DirectInput.ResultCode.InputLost || resultCode == SharpDX.DirectInput.ResultCode.NotAcquired) { try { _Keyboard.Acquire(); } catch (Exception ex) { MainWindow.MessageBox((IntPtr)0, "cannot get keyboard info 01: " + ex.ToString() + "", "Oculus error", 0); } return(true); } if (resultCode == SharpDX.DirectInput.ResultCode.Ok) { return(true); } return(false); }
/// <summary>Initialises the keyboard.</summary> /// <param name="control">The control to associate with DirectInput.</param> private void InitialiseKeyboard(WF.Control control) { // From idlibc.c _keyToScanCode = new int[256]; _keyToScanCode[(int) DI.Key.Escape] = 0x01; _keyToScanCode[(int) DI.Key.Space] = 0x39; _keyToScanCode[(int) DI.Key.Back] = 0x0e; _keyToScanCode[(int) DI.Key.Tab] = 0x0f; _keyToScanCode[(int) DI.Key.LeftControl] = 0x1d; _keyToScanCode[(int) DI.Key.LeftShift] = 0x2a; _keyToScanCode[(int) DI.Key.Capital] = 0x3a; _keyToScanCode[(int) DI.Key.F1] = 0x3b; _keyToScanCode[(int) DI.Key.F2] = 0x3c; _keyToScanCode[(int) DI.Key.F3] = 0x3d; _keyToScanCode[(int) DI.Key.F4] = 0x3e; _keyToScanCode[(int) DI.Key.F5] = 0x3f; _keyToScanCode[(int) DI.Key.F6] = 0x40; _keyToScanCode[(int) DI.Key.F7] = 0x41; _keyToScanCode[(int) DI.Key.F8] = 0x42; _keyToScanCode[(int) DI.Key.F9] = 0x43; _keyToScanCode[(int) DI.Key.F10] = 0x44; _keyToScanCode[(int) DI.Key.F11] = 0x57; _keyToScanCode[(int) DI.Key.F12] = 0x58; _keyToScanCode[(int) DI.Key.ScrollLock] = 0x46; _keyToScanCode[(int) DI.Key.Return] = 0x1c; _keyToScanCode[(int) DI.Key.RightShift] = 0x36; _keyToScanCode[(int) DI.Key.PrintScreen] = 0x37; _keyToScanCode[(int) DI.Key.LeftAlt] = 0x38; _keyToScanCode[(int) DI.Key.Home] = 0x47; _keyToScanCode[(int) DI.Key.PageUp] = 0x49; _keyToScanCode[(int) DI.Key.End] = 0x4f; _keyToScanCode[(int) DI.Key.PageDown] = 0x51; _keyToScanCode[(int) DI.Key.Insert] = 0x52; _keyToScanCode[(int) DI.Key.Delete] = 0x53; _keyToScanCode[(int) DI.Key.NumberLock] = 0x45; _keyToScanCode[(int) DI.Key.Up] = 0x48; _keyToScanCode[(int) DI.Key.Down] = 0x50; _keyToScanCode[(int) DI.Key.Left] = 0x4b; _keyToScanCode[(int) DI.Key.Right] = 0x4d; _keyToScanCode[(int) DI.Key.D1] = 0x02; _keyToScanCode[(int) DI.Key.D2] = 0x03; _keyToScanCode[(int) DI.Key.D3] = 0x04; _keyToScanCode[(int) DI.Key.D4] = 0x05; _keyToScanCode[(int) DI.Key.D5] = 0x06; _keyToScanCode[(int) DI.Key.D6] = 0x07; _keyToScanCode[(int) DI.Key.D7] = 0x08; _keyToScanCode[(int) DI.Key.D8] = 0x09; _keyToScanCode[(int) DI.Key.D9] = 0x0a; _keyToScanCode[(int) DI.Key.D0] = 0x0b; _keyToScanCode[(int) DI.Key.Minus] = 0x0c; _keyToScanCode[(int) DI.Key.Equals] = 0x0d; _keyToScanCode[(int) DI.Key.Q] = 0x10; _keyToScanCode[(int) DI.Key.W] = 0x11; _keyToScanCode[(int) DI.Key.E] = 0x12; _keyToScanCode[(int) DI.Key.R] = 0x13; _keyToScanCode[(int) DI.Key.T] = 0x14; _keyToScanCode[(int) DI.Key.Y] = 0x15; _keyToScanCode[(int) DI.Key.U] = 0x16; _keyToScanCode[(int) DI.Key.I] = 0x17; _keyToScanCode[(int) DI.Key.O] = 0x18; _keyToScanCode[(int) DI.Key.P] = 0x19; _keyToScanCode[(int) DI.Key.LeftBracket] = 0x1a; _keyToScanCode[(int) DI.Key.RightBracket] = 0x1b; //_keyToScanCode[(int) DI.Key.OemPipe] = 0x1c; // TODO _keyToScanCode[(int) DI.Key.A] = 0x1e; _keyToScanCode[(int) DI.Key.S] = 0x1f; _keyToScanCode[(int) DI.Key.D] = 0x20; _keyToScanCode[(int) DI.Key.F] = 0x21; _keyToScanCode[(int) DI.Key.G] = 0x22; _keyToScanCode[(int) DI.Key.H] = 0x23; _keyToScanCode[(int) DI.Key.J] = 0x24; _keyToScanCode[(int) DI.Key.K] = 0x25; _keyToScanCode[(int) DI.Key.L] = 0x26; _keyToScanCode[(int) DI.Key.Semicolon] = 0x27; //_keyToScanCode[(int) DI.Key.OemQuotes] = 0x28; // TODO _keyToScanCode[(int) DI.Key.Z] = 0x2c; _keyToScanCode[(int) DI.Key.X] = 0x2d; _keyToScanCode[(int) DI.Key.C] = 0x2e; _keyToScanCode[(int) DI.Key.V] = 0x2f; _keyToScanCode[(int) DI.Key.B] = 0x30; _keyToScanCode[(int) DI.Key.N] = 0x31; _keyToScanCode[(int) DI.Key.M] = 0x32; _keyToScanCode[(int) DI.Key.Comma] = 0x33; _keyToScanCode[(int) DI.Key.Period] = 0x34; // see idlibc.chartable for other keys _keyboard = new DI.Keyboard(_directInput); _keyboard.SetCooperativeLevel(control, DI.CooperativeLevel.NonExclusive | DI.CooperativeLevel.Background); _keyboard.Acquire(); _keyboardState = _keyboard.GetCurrentState(); }
public void Run() { gameTimer = Stopwatch.StartNew(); frameTimer = new Stopwatch(); RenderLoop.Run(renderForm, () => { double deltaTime = frameTimer.ElapsedTicks / (double)Stopwatch.Frequency; frameTimer.Restart(); frameTime += deltaTime; frameCount++; if (frameTime > 1) { frameTime = 0; Debug.FPS = frameCount; frameCount = 0; } Debug.BeginFrame(); Profiler p = Profiler.Begin("Frame"); Profiler.Begin("Update"); #region input state update if (renderForm.Focused) { Input.ks = keyboard.GetCurrentState(); if (Input.lastks == null) { Input.lastks = Input.ks; } } Input.ms = mouse.GetCurrentState(); if (Input.lastms == null) { Input.lastms = Input.ms; } Input.MousePos = realMousePos; #endregion Update(deltaTime); renderer.ScreenToWorld(Input.MousePos, renderer.ActiveCamera, out Input.MouseRayOrigin, out Input.MouseRayDirection); #region input state update Input.lastks = Input.ks; Input.lastms = Input.ms; Input.LastMousePos = Input.MousePos; #endregion Profiler.End(); Profiler.Begin("Draw"); Draw(); Profiler.End(); #if DEBUG Debug.EndFrame(p.Stopwatch.Elapsed.TotalSeconds); if (Debug.DrawDebug) { renderer.D2DContext.BeginDraw(); Debug.Draw2D(renderer); Profiler.End(); // Draw profiler //int py = frameProfiler.TotalChildren()*Profiler.lineHeight + Profiler.lineHeight; //renderer.D2DContext.FillRectangle(new RawRectangleF(renderer.ResolutionX - 360, 5, renderer.ResolutionX, 40 + py + 5), renderer.Brushes["TransparentBlack"]); //frameProfiler.Draw(renderer, new RawRectangleF(renderer.ResolutionX - 350, 10, renderer.ResolutionX - 10, 40)); float r = renderer.ResolutionX * .075f; p.DrawCircle(renderer, new Vector2(renderer.ResolutionX - r - 10, renderer.ResolutionY - r - 10), r); renderer.D2DContext.EndDraw(); } else { Profiler.End(); } #endif renderer.Present(); }); }
public void UpdateKeyboardState() { LastState = CurrentState; CurrentState = _Keyboard.GetCurrentState().PressedKeys; }
public virtual void Update() { State = DIBoard.GetCurrentState(); }
public void KeyBoardStart() { var dirInput = new SharpDX.DirectInput.DirectInput(); var allDevices = dirInput.GetDevices(); foreach (var item in allDevices) { if (SharpDX.DirectInput.DeviceType.Keyboard == item.Type) { curKeyBoard = new SharpDX.DirectInput.Keyboard(dirInput); curKeyBoard.Acquire(); } } while (true) { Thread.Sleep(100); var curKeyboardState = curKeyBoard.GetCurrentState(); Key curPressedKey = new Key();; if (curKeyboardState.PressedKeys.Count() > 0) { curPressedKey = curKeyboardState.PressedKeys[0]; } if (curPressedKey == SharpDX.DirectInput.Key.O || curPressedKey == SharpDX.DirectInput.Key.P) { var dir = curPressedKey == SharpDX.DirectInput.Key.O ? Direction.ClampOn : Direction.ClampOff; //clampCalculator.OnClampPressed(dir); Debug.WriteLine(dir.ToString()); continue; } if (InputDeviceInfo.isJoysMoving) { continue; } InputDeviceInfo.isKeyBoardMoving = curKeyboardState.PressedKeys.Count() > 0 && IsControlKey(curPressedKey); if (InputDeviceInfo.isKeyBoardMoving) { //InputDeviceInfo.isKeyBoardMoving = true; switch (curKeyboardState.PressedKeys[0]) { case SharpDX.DirectInput.Key.W: positionCalculator.OnDirectionPressed(Direction.Up); break; case SharpDX.DirectInput.Key.A: positionCalculator.OnDirectionPressed(Direction.Left); break; case SharpDX.DirectInput.Key.S: positionCalculator.OnDirectionPressed(Direction.Down); break; case SharpDX.DirectInput.Key.D: positionCalculator.OnDirectionPressed(Direction.Right); break; case SharpDX.DirectInput.Key.I: positionCalculator.OnDirectionPressed(Direction.ZUp); break; case SharpDX.DirectInput.Key.K: positionCalculator.OnDirectionPressed(Direction.ZDown); break; case SharpDX.DirectInput.Key.J: positionCalculator.OnDirectionPressed(Direction.RotateLeft); break; case SharpDX.DirectInput.Key.L: positionCalculator.OnDirectionPressed(Direction.RotateRight); break; default: break; } } else { positionCalculator.Stop(); } } }