Beispiel #1
0
        public bool ProcessKeyboardEvent(IntPtr raw)
        {
            bool processed = false;

            RawInput rin;

            if (Functions.GetRawInputData(raw, out rin) > 0)
            {
                bool pressed =
                    rin.Data.Keyboard.Message == (int)WindowMessage.KEYDOWN ||
                    rin.Data.Keyboard.Message == (int)WindowMessage.SYSKEYDOWN;
                var scancode = rin.Data.Keyboard.MakeCode;
                var vkey     = rin.Data.Keyboard.VKey;

                bool extended0 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E0) != 0;
                bool extended1 = (int)(rin.Data.Keyboard.Flags & RawInputKeyboardDataFlags.E1) != 0;

                bool is_valid = true;

                ContextHandle handle = new ContextHandle(rin.Header.Device);
                KeyboardState keyboard;
                if (!rawids.ContainsKey(handle))
                {
                    //RefreshDevices();
                }

                if (keyboards.Count == 0)
                {
                    return(false);
                }

                // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0
                // as rin.Header.Device for the "zoom-in/zoom-out" buttons.
                // That's problematic, because no device has a "0" id.
                // As a workaround, we'll add those buttons to the first device (if any).
                int keyboard_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0;
                keyboard = keyboards[keyboard_handle];

                Key key = WinKeyMap.TranslateKey(scancode, vkey, extended0, extended1, out is_valid);

                if (is_valid)
                {
                    keyboard.SetKeyState(key, pressed);
                    processed = true;
                }

                lock (UpdateLock)
                {
                    keyboards[keyboard_handle] = keyboard;
                    processed = true;
                }
            }

            return(processed);
        }