Пример #1
0
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------

        #region Internal Methods

        //
        // Inject keyboard input into the system
        //
        internal static void SendKeyboardInput(Key key, bool press)
        {
            UnsafeNativeMethods.INPUT ki = new UnsafeNativeMethods.INPUT();
            ki.type = UnsafeNativeMethods.INPUT_KEYBOARD;
            ki.union.keyboardInput.wVk   = (short)KeyInterop.VirtualKeyFromKey(key);
            ki.union.keyboardInput.wScan = (short)UnsafeNativeMethods.MapVirtualKey(ki.union.keyboardInput.wVk, 0);
            int dwFlags = 0;

            if (ki.union.keyboardInput.wScan > 0)
            {
                dwFlags |= UnsafeNativeMethods.KEYEVENTF_SCANCODE;
            }
            if (!press)
            {
                dwFlags |= UnsafeNativeMethods.KEYEVENTF_KEYUP;
            }
            ki.union.keyboardInput.dwFlags = dwFlags;
            if (IsExtendedKey(key))
            {
                ki.union.keyboardInput.dwFlags |= UnsafeNativeMethods.KEYEVENTF_EXTENDEDKEY;
            }
            ki.union.keyboardInput.time        = 0;
            ki.union.keyboardInput.dwExtraInfo = new IntPtr(0);

            Misc.SendInput(1, ref ki, Marshal.SizeOf(ki));
        }
Пример #2
0
        // Used internally by the HWND SetFocus code - it sends a hotkey to
        // itself - because it uses a VK that's not on the keyboard, it needs
        // to send the VK directly, not the scan code, which regular
        // SendKeyboardInput does.
        internal static void SendKeyboardInputVK(byte vk, bool press)
        {
            UnsafeNativeMethods.INPUT ki = new UnsafeNativeMethods.INPUT();
            ki.type = UnsafeNativeMethods.INPUT_KEYBOARD;
            ki.union.keyboardInput.wVk         = vk;
            ki.union.keyboardInput.wScan       = 0;
            ki.union.keyboardInput.dwFlags     = press ? 0 : UnsafeNativeMethods.KEYEVENTF_KEYUP;
            ki.union.keyboardInput.time        = 0;
            ki.union.keyboardInput.dwExtraInfo = new IntPtr(0);

            Misc.SendInput(1, ref ki, Marshal.SizeOf(ki));
        }