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