Esempio n. 1
1
 private void CreateNativeKeyboard()
 {
     nativeState = new DInput.KeyboardState();
     directInput = new DInput.DirectInput();
     nativeKeyboard = new DInput.Keyboard(directInput);
     nativeKeyboard.SetCooperativeLevel(windowHandle,
         DInput.CooperativeLevel.NonExclusive | DInput.CooperativeLevel.Background);
     nativeKeyboard.Acquire();
 }
Esempio n. 2
0
        public Game()
        {
            renderForm            = new RenderForm("D3D11 Planets");
            renderForm.MouseMove += (object sender, System.Windows.Forms.MouseEventArgs e) => {
                realMousePos = new Vector2(e.Location.X, e.Location.Y);
            };
            renderForm.WindowState        = System.Windows.Forms.FormWindowState.Maximized;
            renderForm.AllowUserResizing  = true;
            renderForm.ClientSizeChanged += (object sender, EventArgs e) => {
                resizePending = true;
            };

            DInput.DirectInput directInput = new DInput.DirectInput();
            keyboard = new DInput.Keyboard(directInput);
            mouse    = new DInput.Mouse(directInput);

            keyboard.Acquire();
            mouse.Acquire();

            renderer = new Renderer(this, renderForm);

            Shaders.Load(renderer.Device, renderer.Context);
            Resources.Load(renderer.Device);

            Initialize();
        }
        protected override void Dispose(bool disposeManagedResources)
        {
            if (!IsDisposed)
            {
                if (disposeManagedResources)
                {
                    // Dispose managed resources.
                }

                // There are no unmanaged resources to release, but
                // if we add them, they need to be released here.
                try
                {
                    this._keyboard.Unacquire();
                }
                catch
                {
                    // NOTE : This is intentional
                }
                finally
                {
                    this._keyboard.Dispose();
                    this._keyboard = null;
                }

                ((DirectXInputManager)Creator).ReleaseDevice <Keyboard>(this._kbInfo);

                log.Debug("DirectXKeyboard device disposed.");
            }

            // If it is available, make the call to the
            // base class's Dispose(Boolean) method
            base.Dispose(disposeManagedResources);
        }
Esempio n. 4
0
        public override void Stop()
        {
            SF = null;
            // Don't leave any keys pressed
            for (int i = 1; i < MyKeyDown.Length; i++)
            {
                if (MyKeyDown[i])
                {
                    KeyOrButtonUp(i);
                }
            }

            if (KeyboardDevice != null)
            {
                KeyboardDevice.Unacquire();
                KeyboardDevice.Dispose();
                KeyboardDevice = null;
            }

            if (mouseDevice != null)
            {
                mouseDevice.Unacquire();
                mouseDevice.Dispose();
                mouseDevice = null;
            }

            if (DirectInputInstance != null)
            {
                DirectInputInstance.Dispose();
                DirectInputInstance = null;
            }
        }
Esempio n. 5
0
 public Keyboard(DirectInput DirectInput)
 {
     _Keyboard = new SharpDX.DirectInput.Keyboard(DirectInput);
     _Keyboard.Acquire();
     UpdateKeyboardState();
     UpdateKeyboardState();
 }
Esempio n. 6
0
        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);
        }
        /// <summary>Disposes resources.</summary>
        public override void Dispose()
        {
            if(_keyboard != null)
            {
                try { _keyboard.Unacquire(); }
                catch { }

                try { _keyboard.Dispose(); }
                catch { _keyboard = null; }
            }

            if(_joystick != null)
            {
                try { _joystick.Unacquire(); }
                catch { }

                try { _joystick.Dispose(); }
                catch { }
            }

            if(_directInput != null)
            {
                try { _directInput.Dispose(); }
                catch { }
                _directInput = null;
            }
        }
        protected override void Initialize()
        {
            this._keyboard = new SharpDX.DirectInput.Keyboard(this._directInput);

            //_keyboard.SetDataFormat( MDI.DeviceDataFormat.Keyboard );

            if (this._coopSettings != 0)
            {
                this._keyboard.SetCooperativeLevel(((DirectXInputManager)Creator).WindowHandle, this._coopSettings);
            }

            if (IsBuffered)
            {
                this._keyboard.Properties.BufferSize = BufferSize;
            }

            try
            {
                this._keyboard.Acquire();
            }
            catch (Exception e)
            {
                throw new Exception("Failed to aquire keyboard using DirectInput.", e);
            }
        }
Esempio n. 9
0
        internal Keyboard()
        {
            DirectInputKeyboard = new DI.Keyboard(new DI.DirectInput());
            DirectInputKeyboard.Properties.BufferSize = 128;
            DirectInputKeyboard.Acquire();

            KeyboardInstance = this;
        }
Esempio n. 10
0
        /// <summary>
        /// Hlavní konstruktor.
        /// </summary>
        /// <param name="window">Herní okno.</param>
        public Keyboard(GlibWindow window)
            : base(window)
        {
            keyboard = new KeyboardDirect(Input);
            keyboard.Properties.BufferSize = 128;
            keyboard.Acquire();

            state = new KeyboardState();
        }
Esempio n. 11
0
 internal Keyboard(DKeyboard keyboard)
 {
     System.Diagnostics.Debug.Assert(keyboard != null);
     _dKeyboard = keyboard;
     _inputState = KeyboardInput.CreateEmpty();
     _lastPressedKeys = new List<Key>();
     (_timer = new GameTimer()).Start();
     _currentDirectInputState = new KeyboardState();
 }
		private void CreateNativeKeyboard()
		{
			nativeState = new DInput.KeyboardState();
			directInput = new DInput.DirectInput();
			nativeKeyboard = new DInput.Keyboard(directInput);
			if (window.IsWindowsFormAndNotJustAPanel)
				nativeKeyboard.SetCooperativeLevel(window.Handle,
					DInput.CooperativeLevel.NonExclusive | DInput.CooperativeLevel.Background);
			nativeKeyboard.Acquire();
		}
Esempio n. 13
0
        public override void Dispose()
        {
            if (nativeKeyboard != null)
            {
                nativeKeyboard.Unacquire();
                nativeKeyboard = null;
            }

            directInput = null;
        }
        public override void Dispose()
        {
            if (nativeKeyboard != null)
            {
                nativeKeyboard.Unacquire();
                nativeKeyboard = null;
            }

            directInput = null;
            IsAvailable = false;
        }
 private void CreateNativeKeyboard()
 {
     nativeState    = new DInput.KeyboardState();
     directInput    = new DInput.DirectInput();
     nativeKeyboard = new DInput.Keyboard(directInput);
     if (window.IsWindowsFormAndNotJustAPanel)
     {
         nativeKeyboard.SetCooperativeLevel(window.Handle,
                                            DInput.CooperativeLevel.NonExclusive | DInput.CooperativeLevel.Background);
     }
     nativeKeyboard.Acquire();
 }
Esempio n. 16
0
        private Keyboard StartKeyboard()
        {
            var      dirInput   = new SharpDX.DirectInput.DirectInput();
            var      allDevices = dirInput.GetDevices();
            Keyboard keyboard   = null;

            foreach (var item in allDevices)
            {
                if (SharpDX.DirectInput.DeviceType.Keyboard == item.Type)
                {
                    keyboard = new SharpDX.DirectInput.Keyboard(dirInput);
                    keyboard.Acquire();
                }
            }
            return(keyboard);
        }
 public int _InitializeKeyboard()
 {
     _InitializeKeyboardAuth = 1;
     try
     {
         directInput = new DirectInput();
         _Keyboard   = new SharpDX.DirectInput.Keyboard(directInput);
         _Keyboard.Properties.BufferSize = 128;
         _Keyboard.Acquire();
     }
     catch
     {
         _InitializeKeyboardAuth = 0;
     }
     return(_InitializeKeyboardAuth);
 }
Esempio n. 18
0
        private void _initializeInputs()
        {
            Console.Write("Initializing inputs... ");

            _directInput = ToDispose<DirectInput>(new DirectInput());

            _dKeyboard = ToDispose<DKeyboard>(new DKeyboard(_directInput));
            _dKeyboard.Properties.BufferSize = 256;
            _dKeyboard.SetCooperativeLevel(_form, CooperativeLevel.Foreground | CooperativeLevel.Exclusive);
            Keyboard = new Keyboard(_dKeyboard);

            _dMouse = ToDispose<DMouse>(new DMouse(_directInput));
            _dMouse.Properties.AxisMode = DeviceAxisMode.Relative;
            _dMouse.SetCooperativeLevel(_form, CooperativeLevel.Foreground | CooperativeLevel.NonExclusive);
            Mouse = new Mouse(_form, _dMouse);
            Console.WriteLine("done.");
        }
 public DIKeys(DirectInput di)
 {
     DIBoard = new SharpDX.DirectInput.Keyboard(di);
 }
        /// <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();
        }
Esempio n. 21
0
        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();
                }
            }
        }
Esempio n. 22
0
        public void ThreadStart()
        {
            byte[] keybdState = new byte[256];
            GetKeyboardState(keybdState);

            NumLock    = (keybdState[(int)VK.VK_NUMLOCK] & 1) != 0;
            CapsLock   = (keybdState[(int)VK.VK_CAPITAL] & 1) != 0;
            ScrollLock = (keybdState[(int)VK.VK_SCROLL] & 1) != 0;

            Shift  = (keybdState[(int)VK.VK_SHIFT] & 0x80) != 0;
            LShift = (keybdState[(int)VK.VK_LSHIFT] & 0x80) != 0;
            RShift = (keybdState[(int)VK.VK_RSHIFT] & 0x80) != 0;

            Ctrl  = (keybdState[(int)VK.VK_CONTROL] & 0x80) != 0;
            LCtrl = (keybdState[(int)VK.VK_LCONTROL] & 0x80) != 0;
            RCtrl = (keybdState[(int)VK.VK_RCONTROL] & 0x80) != 0;

            Alt  = (keybdState[(int)VK.VK_MENU] & 0x80) != 0;
            LAlt = (keybdState[(int)VK.VK_LMENU] & 0x80) != 0;
            RAlt = (keybdState[(int)VK.VK_RMENU] & 0x80) != 0;

            var directInput = new SharpDX.DirectInput.DirectInput();

            var keybdGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Keyboard, DeviceEnumerationFlags.AllDevices))
            {
                keybdGuid = deviceInstance.InstanceGuid;
            }

            if (keybdGuid == Guid.Empty)
            {
                Environment.Exit(1);
            }

            var keybd = new SharpDX.DirectInput.Keyboard(directInput);

            keybd.Properties.BufferSize = 128;
            keybd.Acquire();

            string         keys             = "";
            VK             vk               = 0x00;
            KeyboardUpdate Scan             = new KeyboardUpdate();
            Stopwatch      holdInSW         = new Stopwatch();
            Stopwatch      holdInIntervalSW = new Stopwatch();

            holdInSW.Start();
            holdInIntervalSW.Start();

            var dwThread = Native.WinAPI.GetCurrentThreadId();

            while (true)
            {
                Thread.Sleep(1);
                Native.DesktopSwitch.PollAutoDesktopThreadSwitch(dwThread);
                keybd.Poll();
                var datas = keybd.GetBufferedData();

                if (datas.Length != 0)
                {
                    holdInSW.Restart();
                }
                else if (holdInSW.Elapsed > PressedInHoldTime && Scan.IsPressed)
                {
                    if (holdInIntervalSW.Elapsed > PressedInInterval)
                    {
                        HandleKey(keys, vk, Scan.Key, Scan.IsPressed);
                        holdInIntervalSW.Restart();
                    }
                }

                foreach (var state in datas)
                {
                    /*if (state.Key == Key.F12) {
                     *      Hooks.RemoveHooks();
                     *      Environment.Exit(0);
                     *      Debug.Assert(false);
                     *      var a = (1 + 9) - 10;
                     *      Debug.WriteLine((15 / a));
                     *      return;
                     * }*/
                    if (state.IsPressed)
                    {
                        switch (state.Key)
                        {
                        case Key.Capital:
                            CapsLock = !CapsLock;
                            break;

                        case Key.NumberLock:
                            NumLock = !NumLock;
                            break;

                        case Key.ScrollLock:
                            ScrollLock = !ScrollLock;
                            break;
                        }
                    }
                    switch (state.Key)
                    {
                    case Key.RightShift:
                        RShift = state.IsPressed;
                        break;

                    case Key.LeftShift:
                        LShift = state.IsPressed;
                        break;

                    case Key.LeftControl:
                        LCtrl = state.IsPressed;
                        break;

                    case Key.RightControl:
                        RCtrl = state.IsPressed;
                        break;

                    case Key.LeftAlt:
                        LAlt = state.IsPressed;
                        break;

                    case Key.RightAlt:
                        RAlt = state.IsPressed;
                        break;
                    }

                    Shift = LShift || RShift;
                    Ctrl  = LCtrl || RCtrl;
                    Alt   = LAlt || RAlt;

                    byte[] keyState = new byte[256];

                    if (CapsLock)
                    {
                        keyState[(int)VK.VK_CAPITAL] = 0x01;
                    }
                    if (NumLock)
                    {
                        keyState[(int)VK.VK_NUMLOCK] = 0x01;
                    }
                    if (ScrollLock)
                    {
                        keyState[(int)VK.VK_SCROLL] = 0x01;
                    }
                    if (Shift)
                    {
                        keyState[(int)VK.VK_SHIFT] = 0x80;
                    }
                    if (Ctrl)
                    {
                        keyState[(int)VK.VK_CONTROL] = 0x80;
                    }
                    if (Alt)
                    {
                        keyState[(int)VK.VK_MENU] = 0x80;
                    }

                    keys = ScancodeToUnicode(state.Key, keyState);
                    vk   = ScancodeToVKCode(state.Key);
                    Scan = state;

                    HandleKey(keys, vk, Scan.Key, Scan.IsPressed);
                }
            }
        }