Пример #1
0
 public CustomDiUpdate(KeyboardUpdate update)
 {
     Value = update.Value;
     Index = (int)update.Key;
     Value = update.IsPressed ? 1 : 0;
     Type  = MapType.Button;
 }
 public void KeyStateToBytes(KeyboardUpdate update)
 {
     Console.WriteLine(update);
     Console.WriteLine(update.Key);
     Console.WriteLine(update.IsPressed);
     Console.WriteLine(update.IsReleased);
     Console.WriteLine();
 }
Пример #3
0
 static void ProcessKB(KeyboardUpdate keyboard)
 {
     for (int i = 0; i < Enum.GetNames(typeof(Key)).Length; i++)
     {
         if ((int)keyboard.Key == i)
         {
             if (keyboard.IsPressed)
             {
                 keys[i] = new KeyState()
                 {
                     Pressed = keyboard.IsPressed, Held = true, Released = keyboard.IsReleased
                 };
             }
             else
             {
                 keys[i] = new KeyState()
                 {
                     Pressed = keyboard.IsPressed, Held = keys[i].Held && !keyboard.IsReleased, Released = keyboard.IsReleased
                 };
             }
         }
     }
 }
        public RLocalButtonState KeyStateToStructure(KeyboardUpdate update, bool raw)
        {
            int button;

            if (!map.TryGetValue(update.Key.ToString(), out button))
            {
                return(null);
            }

            var buttonState = new RLocalButtonState();

            buttonState.button   = button;
            buttonState.rawInput = update.Key.ToString();

            if (button == 1300)
            {
                buttonState.button = RLocalInput.MapButtonToId["Z"];
                buttonState.value  = update.IsPressed ? ushort.MaxValue : 0;
                return(buttonState);
            }
            else if (button == 1301)
            {
                buttonState.button = RLocalInput.MapButtonToId["RotationZ"];
                buttonState.value  = update.IsPressed ? ushort.MaxValue : 0;
                return(buttonState);
            }
            else
            {
                buttonState.value = update.IsPressed ? 128 : 0;
            }

            if (button >= 1000)
            {
                int angle = -1;
                switch (button)
                {
                case 1000:
                    up = update.IsPressed; break;

                case 1002:
                    right = update.IsPressed; break;

                case 1004:
                    down = update.IsPressed; break;

                case 1006:
                    left = update.IsPressed; break;
                }

                bool isPressed = (up || right || down || left);

                if (isPressed)
                {
                    if (up && right)
                    {
                        button = 1001;
                    }
                    else if (right && down)
                    {
                        button = 1003;
                    }
                    else if (down && left)
                    {
                        button = 1005;
                    }
                    else if (left && up)
                    {
                        button = 1007;
                    }
                    else
                    {
                        button = up ? 1000 : right ? 1002 : down ? 1004 : left ? 1006 : button;
                    }

                    if (isPressed)
                    {
                        switch (button)
                        {
                        case 1000: angle = 0; break;

                        case 1001: angle = 4500; break;

                        case 1002: angle = 9000; break;

                        case 1003: angle = 13500; break;

                        case 1004: angle = 18000; break;

                        case 1005: angle = 22500; break;

                        case 1006: angle = 27000; break;

                        case 1007: angle = 31500; break;
                        }
                    }
                }

                buttonState.button = 1100;
                buttonState.value  = angle;
            }

            return(buttonState);
        }
Пример #5
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);
                }
            }
        }
Пример #6
0
        public static void PollInput()
        {
            if (!m_bIsInitialized)
            {
                return;
            }

            lock (m_inputMutex)
            {
                try
                {
                    m_keyboard.Poll();
                }
                catch (SharpDXException e)
                {
                    if (e.ResultCode == ResultCode.InputLost || e.ResultCode == ResultCode.NotAcquired)
                    {
                        try
                        {
                            m_keyboard.Acquire();
                            m_keyboard.Poll();
                        }
                        catch
                        {
                            m_currentKeyboardState = new KeyboardState();
                            m_currentMouseState    = new MouseState();
                            return;
                        }
                    }
                    else
                    {
                        throw;
                    }
                }

                try
                {
                    m_mouse.Poll();
                }
                catch (SharpDXException e)
                {
                    if (e.ResultCode == ResultCode.InputLost || e.ResultCode == ResultCode.NotAcquired)
                    {
                        try
                        {
                            m_mouse.Acquire();
                            m_mouse.Poll();
                        }
                        catch
                        {
                            return;
                        }
                    }
                    else
                    {
                        throw;
                    }
                }


                List <SInputButtonEvent> buttonEvents = new List <SInputButtonEvent>(100);

                try
                {
                    KeyboardUpdate[] keyboardUpdates = m_keyboard.GetBufferedData();
                    for (int i = 0; i < keyboardUpdates.Length; i++)
                    {
                        KeyboardUpdate    update = keyboardUpdates[i];
                        SInputButtonEvent buttonEvent;
                        buttonEvent.buttonEvent = update.IsPressed ? EButtonEvent.Pressed : EButtonEvent.Released;
                        buttonEvent.button      = (EInputButton)update.Key;
                        buttonEvents.Add(buttonEvent);
                    }

                    MouseUpdate[] mouseUpdates = m_mouse.GetBufferedData();
                    for (int i = 0; i < mouseUpdates.Length; i++)
                    {
                        MouseUpdate update = mouseUpdates[i];

                        if (update.IsButton)
                        {
                            SInputButtonEvent buttonEvent;
                            buttonEvent.buttonEvent = (update.Value & 0x80) != 0 ? EButtonEvent.Pressed : EButtonEvent.Released;
                            buttonEvent.button      = EInputButton.Unknown;
                            switch (update.Offset)
                            {
                            case MouseOffset.Buttons0:
                                buttonEvent.button = EInputButton.MouseLeftButton;
                                break;

                            case MouseOffset.Buttons1:
                                buttonEvent.button = EInputButton.MouseRightButton;
                                break;

                            case MouseOffset.Buttons2:
                                buttonEvent.button = EInputButton.MouseMiddleButton;
                                break;

                            case MouseOffset.Buttons3:
                                buttonEvent.button = EInputButton.MouseButton3;
                                break;

                            case MouseOffset.Buttons4:
                                buttonEvent.button = EInputButton.MouseButton4;
                                break;

                            case MouseOffset.Buttons5:
                                buttonEvent.button = EInputButton.MouseButton5;
                                break;

                            case MouseOffset.Buttons6:
                                buttonEvent.button = EInputButton.MouseButton6;
                                break;

                            case MouseOffset.Buttons7:
                                buttonEvent.button = EInputButton.MouseButton7;
                                break;
                            }

                            buttonEvents.Add(buttonEvent);
                        }
                    }

                    lock (m_inputStateMutex)
                    {
                        m_currentMouseState    = m_mouse.GetCurrentState();
                        m_currentKeyboardState = m_keyboard.GetCurrentState();
                    }
                }
                catch (SharpDXException e)
                {
                    if (e.ResultCode == ResultCode.InputLost)
                    {
                        // We lost input we will try to reacquire next frame
                        return;
                    }
                    else
                    {
                        throw;
                    }
                }

                // TODO henning support multiple controller for now we take the first connected
                for (int i = 0; i < m_controllers.Length; ++i)
                {
                    if (m_controllers[i].IsConnected)
                    {
                        State controllerState = m_controllers[i].GetState();
                        if (controllerState.PacketNumber != m_currentGamepadState.PacketNumber)
                        {
                            ProcessGamepad(controllerState, buttonEvents);
                            break;
                        }
                    }
                }

                UpdateAxis();

                string textInput = "";
                lock (m_textBufferMutex)
                {
                    textInput         = string.Copy(m_textInputBuffer);
                    m_textInputBuffer = "";
                }

                ReadOnlyCollection <SInputButtonEvent> readOnlyButtonEvents = new ReadOnlyCollection <SInputButtonEvent>(buttonEvents);

                //Copy the current activation states of the input classes to separate array so that they cannot be deactivated during iteration
                for (int i = 0, count = (int)EInputClass.Count; i < count; i++)
                {
                    m_cachedInputClassStates[i] = m_inputListeners[i].bIsActive;
                }

                //todo valentin: Multithreading needs cleanup once multiple threads need callback
                if (readOnlyButtonEvents.Count > 0 || !string.IsNullOrWhiteSpace(textInput))
                {
                    // Notify Listener
                    m_bIsIterating = true;
                    for (int i = 0, count = (int)EInputClass.Count; i < count; i++)
                    {
                        if (m_cachedInputClassStates[i])
                        {
                            var callbacks = m_inputListeners[i].callbacks;
                            for (int k = 0, callbackCount = callbacks.Count; k < callbackCount; k++)
                            {
                                callbacks[k](readOnlyButtonEvents, string.Copy(textInput));
                            }
                        }
                    }
                    m_bIsIterating = false;

                    //Add/Remove callbacks that were altered during iteration
                    for (int i = 0, count = m_iterationAdditions.Count; i < count; i++)
                    {
                        RegisterListener(m_iterationAdditions[i].callback, m_iterationAdditions[i].inputClass);
                    }

                    for (int i = 0, count = m_iterationRemovals.Count; i < count; i++)
                    {
                        UnregisterListener(m_iterationAdditions[i].callback, m_iterationAdditions[i].inputClass);
                    }

                    m_iterationAdditions.Clear();
                    m_iterationRemovals.Clear();
                }
            }
        }
Пример #7
0
 public ControllerEventArgs(KeyboardUpdate state) : base()
 {
     RawState = state;
 }
Пример #8
0
 public DKeyboardEventArgs(KeyboardUpdate state) : base(state)
 {
     Key      = state.Key;
     Pressed  = state.IsPressed;
     Released = state.IsReleased;
 }