Ejemplo n.º 1
0
 protected override bool ProcessKeyEventArgs(ref Message m)
 {
     return(HandleKeyboardEvent(m.Msg, m.WParam.ToInt32(), KeyboardHook.GetModifierKeyState()));
 }
Ejemplo n.º 2
0
        protected bool HandleKeyboardEvent(Int32 msg, Int32 virtualKey, KeyboardHook.ModifierKeys modifierKeys)
        {
            if (DesignMode || !IsConnected)
                return false;

            if (modifierKeys != PreviousModifierKeyState)
                SyncModifierKeyState(modifierKeys);

            if (IsModifierKey(virtualKey)) return true;

            Boolean pressed;
            switch (msg)
            {
                case Win32.WM_KEYDOWN:
                case Win32.WM_SYSKEYDOWN:
                    pressed = true;
                    break;
                case Win32.WM_KEYUP:
                case Win32.WM_SYSKEYUP:
                    pressed = false;
                    break;
                default:
                    return false;
            }

            vnc.WriteKeyboardEvent(Convert.ToUInt32(TranslateVirtualKey(virtualKey, modifierKeys)), pressed);

            return true;
        }
Ejemplo n.º 3
0
        protected void SyncModifierKeyState(KeyboardHook.ModifierKeys modifierKeys)
        {
            if ((PreviousModifierKeyState & KeyboardHook.ModifierKeys.LeftShift) !=
                (modifierKeys & KeyboardHook.ModifierKeys.LeftShift))
                vnc.WriteKeyboardEvent(RfbProtocol.XK_Shift_L, (modifierKeys & KeyboardHook.ModifierKeys.LeftShift) != 0);
            if ((PreviousModifierKeyState & KeyboardHook.ModifierKeys.RightShift) !=
                (modifierKeys & KeyboardHook.ModifierKeys.RightShift))
                vnc.WriteKeyboardEvent(RfbProtocol.XK_Shift_R, (modifierKeys & KeyboardHook.ModifierKeys.RightShift) != 0);

            if ((PreviousModifierKeyState & KeyboardHook.ModifierKeys.LeftControl) !=
                (modifierKeys & KeyboardHook.ModifierKeys.LeftControl))
                vnc.WriteKeyboardEvent(RfbProtocol.XK_Control_L, (modifierKeys & KeyboardHook.ModifierKeys.LeftControl) != 0);
            if ((PreviousModifierKeyState & KeyboardHook.ModifierKeys.RightControl) !=
                (modifierKeys & KeyboardHook.ModifierKeys.RightControl))
                vnc.WriteKeyboardEvent(RfbProtocol.XK_Control_R, (modifierKeys & KeyboardHook.ModifierKeys.RightControl) != 0);

            if ((PreviousModifierKeyState & KeyboardHook.ModifierKeys.LeftAlt) !=
                (modifierKeys & KeyboardHook.ModifierKeys.LeftAlt))
                vnc.WriteKeyboardEvent(RfbProtocol.XK_Alt_L, (modifierKeys & KeyboardHook.ModifierKeys.LeftAlt) != 0);
            if ((PreviousModifierKeyState & KeyboardHook.ModifierKeys.RightAlt) !=
                (modifierKeys & KeyboardHook.ModifierKeys.RightAlt))
                vnc.WriteKeyboardEvent(RfbProtocol.XK_Alt_R, (modifierKeys & KeyboardHook.ModifierKeys.RightAlt) != 0);

            if ((PreviousModifierKeyState & KeyboardHook.ModifierKeys.LeftWin) !=
                (modifierKeys & KeyboardHook.ModifierKeys.LeftWin))
                vnc.WriteKeyboardEvent(RfbProtocol.XK_Super_L, (modifierKeys & KeyboardHook.ModifierKeys.LeftWin) != 0);
            if ((PreviousModifierKeyState & KeyboardHook.ModifierKeys.RightWin) !=
                (modifierKeys & KeyboardHook.ModifierKeys.RightWin))
                vnc.WriteKeyboardEvent(RfbProtocol.XK_Super_R, (modifierKeys & KeyboardHook.ModifierKeys.RightWin) != 0);

            PreviousModifierKeyState = modifierKeys;
        }
Ejemplo n.º 4
0
        public static Int32 TranslateVirtualKey(Int32 virtualKey, KeyboardHook.ModifierKeys modifierKeys)
        {
            if (KeyTranslationTable.ContainsKey(virtualKey))
                return KeyTranslationTable[virtualKey];

            // Windows sends the uppercase letter when the user presses a hotkey
            // like Ctrl-A. ToAscii takes into effect the keyboard layout and
            // state of the modifier keys. This will give us the lowercase letter
            // unless the user is also pressing Shift.
            var keyboardState = new byte[256];
            if (!Win32.GetKeyboardState(keyboardState))
                throw new Win32Exception(Marshal.GetLastWin32Error());

            keyboardState[Win32.VK_CONTROL] = 0;
            keyboardState[Win32.VK_LCONTROL] = 0;
            keyboardState[Win32.VK_RCONTROL] = 0;
            keyboardState[Win32.VK_MENU] = 0;
            keyboardState[Win32.VK_LMENU] = 0;
            keyboardState[Win32.VK_RMENU] = 0;
            keyboardState[Win32.VK_LWIN] = 0;
            keyboardState[Win32.VK_RWIN] = 0;

            var charResult = new byte[2];
            var charCount = Win32.ToAscii(virtualKey, Win32.MapVirtualKey(virtualKey, 0), keyboardState, charResult, 0);

            // TODO: This could probably be handled better. For now, we'll just return the last character.
            if (charCount > 0) return Convert.ToInt32(charResult[charCount - 1]);

            return virtualKey;
        }