Esempio n. 1
0
        /// <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;
            }
        }
Esempio n. 2
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);
                }
            }
        }