Пример #1
0
        internal static IEnumerable <EventArgsExtTuşBasımı> FromRawDataApp(CallbackData data)
        {
            var wParam = data.WParam;
            var lParam = data.LParam;


            const uint maskKeydown  = 0x40000000;
            const uint maskKeyup    = 0x80000000;
            const uint maskScanCode = 0xff0000;

            var flags         = (uint)lParam.ToInt64();
            var wasKeyDown    = (flags & maskKeydown) > 0;
            var isKeyReleased = (flags & maskKeyup) > 0;

            if (!wasKeyDown && !isKeyReleased)
            {
                yield break;
            }

            var       virtualKeyCode = (int)wParam;
            var       scanCode       = checked ((int)(flags & maskScanCode));
            const int fuState        = 0;

            char[] chars;

            KeyboardNativeMethods.TryGetCharFromKeyboardState(virtualKeyCode, scanCode, fuState, out chars);
            if (chars == null)
            {
                yield break;
            }
            foreach (var ch in chars)
            {
                yield return(new EventArgsExtTuşBasımı(ch));
            }
        }
Пример #2
0
        /// <summary>
        /// Creates <see cref="KeyPressEventArgsExt"/> from Windows Message parameters,
        /// based upon a system-wide hook.
        /// </summary>
        /// <param name="wParam">The first Windows Message parameter.</param>
        /// <param name="lParam">The second Windows Message parameter.</param>
        /// <returns>A new KeyPressEventArgsExt object.</returns>
        internal static KeyPressEventArgsExt FromRawDataGlobal(int wParam, IntPtr lParam)
        {
            if (wParam != Messages.WM_KEYDOWN)
            {
                return(new KeyPressEventArgsExt((char)0));
            }

            KeyboardHookStruct keyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

            int virtualKeyCode = keyboardHookStruct.VirtualKeyCode;
            int scanCode       = keyboardHookStruct.ScanCode;
            int fuState        = keyboardHookStruct.Flags;

            char ch;

            if (virtualKeyCode == KeyboardNativeMethods.VK_PACKET)
            {
                ch = (char)scanCode;
            }
            else
            {
                bool isSuccessfull = KeyboardNativeMethods.TryGetCharFromKeyboardState(virtualKeyCode, scanCode, fuState, out ch);
                if (!isSuccessfull)
                {
                    return(new KeyPressEventArgsExt((char)0));
                }
            }

            KeyPressEventArgsExt e = new KeyPressEventArgsExt(ch, keyboardHookStruct.Time);

            return(e);
        }
        /// <summary>
        /// Creates <see cref="KeyPressEventArgsExt"/> from Windows Message parameters,
        /// based upon a local application hook.
        /// </summary>
        /// <param name="wParam">The first Windows Message parameter.</param>
        /// <param name="lParam">The second Windows Message parameter.</param>
        /// <returns>A new KeyPressEventArgsExt object.</returns>
        private static KeyPressEventArgsExt FromRawDataApp(int wParam, IntPtr lParam)
        {
            //http://msdn.microsoft.com/en-us/library/ms644984(v=VS.85).aspx

            const uint maskKeydown  = 0x40000000;        // for bit 30
            const uint maskKeyup    = 0x80000000;        // for bit 31
            const uint maskScanCode = 0xff0000;          // for bit 23-16

            uint flags = 0u;

#if IS_X64
            // both of these are ugly hacks. Is there a better way to convert a 64bit IntPtr to uint?

            // flags = uint.Parse(lParam.ToString());
            flags = Convert.ToUInt32(lParam.ToInt64());
#else
            //updated from ( uint )lParam, which threw an integer overflow exception in unicode characters
            flags = ( uint )lParam.ToInt64();
#endif

            //bit 30 Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
            bool wasKeyDown = (flags & maskKeydown) > 0;
            //bit 31 Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.
            bool isKeyReleased = (flags & maskKeyup) > 0;

            if (!wasKeyDown && !isKeyReleased)
            {
                return(new KeyPressEventArgsExt((char)0));
            }

            int       virtualKeyCode = wParam;
            int       scanCode       = checked ((int)(flags & maskScanCode));
            const int fuState        = 0;

            char ch;

            if (virtualKeyCode == KeyboardNativeMethods.VK_PACKET)
            {
                ch = ( char )scanCode;
            }
            else
            {
                bool isSuccessfull = KeyboardNativeMethods.TryGetCharFromKeyboardState(virtualKeyCode, scanCode, fuState, out ch);
                if (!isSuccessfull)
                {
                    return(new KeyPressEventArgsExt(( char )0));
                }
            }

            return(new KeyPressEventArgsExt(ch));
        }
        internal static IEnumerable <KeyPressEventArgsExt> FromRawDataGlobal(CallbackData data)
        {
            var wParam = data.WParam;
            var lParam = data.LParam;

            if ((int)wParam != Messages.WM_KEYDOWN && (int)wParam != Messages.WM_SYSKEYDOWN)
            {
                yield break;
            }

            KeyboardHookStruct keyboardHookStruct =
                (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

            var virtualKeyCode = keyboardHookStruct.VirtualKeyCode;
            var scanCode       = keyboardHookStruct.ScanCode;
            var fuState        = keyboardHookStruct.Flags;

            if (virtualKeyCode == KeyboardNativeMethods.VK_PACKET)
            {
                var ch = (char)scanCode;
                yield return(new KeyPressEventArgsExt(ch, keyboardHookStruct.Time, new VirtualKeyCode
                {
                    virtualKeyCode = virtualKeyCode,
                    scanCode = scanCode
                }));
            }
            else
            {
                char[] chars;
                KeyboardNativeMethods.TryGetCharFromKeyboardState(virtualKeyCode, scanCode, fuState, out chars);
                if (chars == null)
                {
                    yield return(new KeyPressEventArgsExt(null, keyboardHookStruct.Time, new VirtualKeyCode
                    {
                        virtualKeyCode = virtualKeyCode,
                        scanCode = scanCode
                    }));
                }
                else
                {
                    foreach (var current in chars)
                    {
                        yield return(new KeyPressEventArgsExt(current, keyboardHookStruct.Time, new VirtualKeyCode
                        {
                            virtualKeyCode = virtualKeyCode,
                            scanCode = scanCode
                        }));
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Creates <see cref="KeyEventArgsExt"/> from Windows Message parameters, based upon
        /// a system-wide hook.
        /// </summary>
        /// <param name="wParam">The first Windows Message parameter.</param>
        /// <param name="lParam">The second Windows Message parameter.</param>
        /// <returns>A new KeyEventArgsExt object.</returns>
        private static KeyEventArgsExt FromRawDataGlobal(int wParam, IntPtr lParam)
        {
            KeyboardHookStruct keyboardHookStruct = ( KeyboardHookStruct )Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
            Keys keyData = AppendModifierStates(( Keys )keyboardHookStruct.VirtualKeyCode);

            bool isKeyDown = (wParam == Messages.WM_KEYDOWN || wParam == Messages.WM_SYSKEYDOWN);
            bool isKeyUp   = (wParam == Messages.WM_KEYUP || wParam == Messages.WM_SYSKEYUP);

            //sent explicitly as a Unicode character
            if (keyboardHookStruct.VirtualKeyCode == KeyboardNativeMethods.VK_PACKET)
            {
                return(new KeyEventArgsExt(keyData, keyboardHookStruct.Time, isKeyDown, isKeyUp, ( char )AppendModifierStates(( Keys )keyboardHookStruct.ScanCode)));
            }

            //Translate based on the application's keyboard layout
            char ch;

            KeyboardNativeMethods.TryGetCharFromKeyboardState(keyboardHookStruct.VirtualKeyCode, keyboardHookStruct.ScanCode, keyboardHookStruct.Flags, out ch);
            return(new KeyEventArgsExt(keyData, keyboardHookStruct.Time, isKeyDown, isKeyUp, ch));
        }
Пример #6
0
        internal static IEnumerable <KeyPressEventArgsExt> FromRawDataApp(CallbackData data, IKeyEventArgs arg)
        {
            var wParam = data.WParam;
            var lParam = data.LParam;

            //http://msdn.microsoft.com/en-us/library/ms644984(v=VS.85).aspx

            const uint maskKeydown  = 0x40000000; // for bit 30
            const uint maskKeyup    = 0x80000000; // for bit 31
            const uint maskScanCode = 0xff0000;   // for bit 23-16

            var flags = (uint)lParam.ToInt64();

            //bit 30 Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
            var wasKeyDown = (flags & maskKeydown) > 0;
            //bit 31 Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.
            var isKeyReleased = (flags & maskKeyup) > 0;

            if (!wasKeyDown && !isKeyReleased)
            {
                yield break;
            }

            var       virtualKeyCode = (int)wParam;
            var       scanCode       = checked ((int)(flags & maskScanCode));
            const int fuState        = 0;

            char[] chars;

            KeyboardNativeMethods.TryGetCharFromKeyboardState(virtualKeyCode, scanCode, fuState, out chars);
            if (chars == null)
            {
                yield break;
            }
            foreach (var ch in chars)
            {
                yield return(new KeyPressEventArgsExt(ch, arg));
            }
        }
Пример #7
0
        /// <summary>
        /// Creates <see cref="KeyEventArgsExt"/> from Windows Message parameters, based upon
        /// a local application hook.
        /// </summary>
        /// <param name="wParam">The first Windows Message parameter.</param>
        /// <param name="lParam">The second Windows Message parameter.</param>
        /// <returns>A new KeyEventArgsExt object.</returns>
        private static KeyEventArgsExt FromRawDataApp(int wParam, IntPtr lParam)
        {
            //http://msdn.microsoft.com/en-us/library/ms644984(v=VS.85).aspx

            const uint maskKeydown = 0x40000000; // for bit 30
            const uint maskKeyup   = 0x80000000; // for bit 31

            int timestamp = Environment.TickCount;

            uint flags = 0u;

#if IS_X64
            // both of these are ugly hacks. Is there a better way to convert a 64bit IntPtr to uint?

            // flags = uint.Parse(lParam.ToString());
            flags = Convert.ToUInt32(lParam.ToInt64());
#else
            //updated from ( uint )lParam, which threw an integer overflow exception in Unicode characters
            flags = ( uint )lParam.ToInt64();
#endif

            //bit 30 Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
            bool wasKeyDown = (flags & maskKeydown) > 0;
            //bit 31 Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.
            bool isKeyReleased = (flags & maskKeyup) > 0;

            Keys keyData = AppendModifierStates(( Keys )wParam);

            bool isKeyDown = !wasKeyDown && !isKeyReleased;
            bool isKeyUp   = wasKeyDown && isKeyReleased;

            char ch;

            //translated based on the active application's keyboard layout.
            KeyboardNativeMethods.TryGetCharFromKeyboardState(wParam, ( int )flags, out ch);
            return(new KeyEventArgsExt(keyData, timestamp, isKeyDown, isKeyUp, ch));
        }