VirtualKeyFromKey() public static method

Convert our Key enum into a Win32 VirtualKey.
public static VirtualKeyFromKey ( Key key ) : int
key Key
return int
Example #1
0
        private bool TextServicesKeystroke(TextServicesContext context, KeyEventArgs keyArgs, bool test)
        {
            TextServicesContext.KeyOp keyop;
            int wParam;
            int lParam;
            int scancode;

            // Cicero's Keystroke Manager and TIP does not recognize VK_RSHIFT or VK_LSHIFT.
            // We need to pass VK_SHIFT and the proper scancode.
            //
            switch (keyArgs.RealKey)
            {
            case Key.RightShift:
                wParam   = NativeMethods.VK_SHIFT;
                scancode = 0x36;
                break;

            case Key.LeftShift:
                wParam   = NativeMethods.VK_SHIFT;
                scancode = 0x2A;
                break;

            default:
                wParam   = KeyInterop.VirtualKeyFromKey(keyArgs.RealKey);
                scancode = 0;
                break;
            }

            lParam = (int)(((uint)scancode << 16) | 1);

            if (keyArgs.RoutedEvent == Keyboard.PreviewKeyDownEvent /*keyArgs.IsDown*/)
            {
                keyop = test ? TextServicesContext.KeyOp.TestDown : TextServicesContext.KeyOp.Down;
            }
            else
            {
                // Previous key state and transition state always 1 for WM_KEYUP.
                lParam |= (1 << 31) | (1 << 30);

                keyop = test ? TextServicesContext.KeyOp.TestUp : TextServicesContext.KeyOp.Up;
            }

            return(context.Keystroke(wParam, lParam, keyop));
        }
Example #2
0
        /// <summary>
        ///     Gets the current state of the specified key from the device from the underlying system
        /// </summary>
        /// <param name="key">
        ///     Key to get the state of
        /// </param>
        /// <returns>
        ///     The state of the specified key
        /// </returns>
        protected override KeyStates GetKeyStatesFromSystem(Key key)
        {
            KeyStates keyStates = KeyStates.None;

            int virtualKeyCode = KeyInterop.VirtualKeyFromKey(key);
            int nativeKeyState = UnsafeNativeMethods.GetKeyState(virtualKeyCode);

            if ((nativeKeyState & 0x00008000) == 0x00008000)
            {
                keyStates |= KeyStates.Down;
            }

            if ((nativeKeyState & 0x00000001) == 0x00000001)
            {
                keyStates |= KeyStates.Toggled;
            }

            return(keyStates);
        }
Example #3
0
 public static KeyUpNotification ToKeyUpNotification(this KeyEventArgs eventArgs)
 {
     return(new KeyUpNotification((Forms.Keys)KeyInterop.VirtualKeyFromKey(eventArgs.Key), null));
 }
Example #4
0
        /// <summary>
        ///     Gets the current state of the specified key from the device from the underlying system
        /// </summary>
        /// <param name="key">
        ///     Key to get the state of
        /// </param>
        /// <returns>
        ///     The state of the specified key
        /// </returns>
        protected override KeyStates GetKeyStatesFromSystem(Key key)
        {
            KeyStates keyStates = KeyStates.None;

            bool getKeyStatesFromSystem = false;

            if (IsActive)
            {
                // Our keyboard device is only active if some WPF window in
                // this AppDomain has focus.  It is always safe to return
                // the state of keys.
                getKeyStatesFromSystem = true;
            }
            else if (SecurityHelper.AppDomainGrantedUnrestrictedUIPermission)
            {
                // This is a trusted AppDomain, so we are willing to expose
                // the state of keys regardless of whether or not a WPF
                // window has focus.  This is important for child HWND
                // hosting scenarios.
                getKeyStatesFromSystem = true;
            }
            else
            {
                // Security Mitigation:
                // No WPF window has focus in this AppDomain, and this is a
                // partially-trusted AppDomain, so we do not generally want
                // to expose the state of keys.  However, we make an exception
                // for modifier keys, as they are considered safe.
                switch (key)
                {
                case Key.LeftAlt:
                case Key.RightAlt:
                case Key.LeftCtrl:
                case Key.RightCtrl:
                case Key.LeftShift:
                case Key.RightShift:
                    getKeyStatesFromSystem = true;
                    break;
                }
            }

            if (getKeyStatesFromSystem)
            {
                int virtualKeyCode = KeyInterop.VirtualKeyFromKey(key);
                int nativeKeyState;

                nativeKeyState = UnsafeNativeMethods.GetKeyState(virtualKeyCode);

                if ((nativeKeyState & 0x00008000) == 0x00008000)
                {
                    keyStates |= KeyStates.Down;
                }

                if ((nativeKeyState & 0x00000001) == 0x00000001)
                {
                    keyStates |= KeyStates.Toggled;
                }
            }

            return(keyStates);
        }