public static int TranslateVirtualKey(int 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 (!NativeMethods.GetKeyboardState(keyboardState)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } keyboardState[NativeMethods.VK_CONTROL] = 0; keyboardState[NativeMethods.VK_LCONTROL] = 0; keyboardState[NativeMethods.VK_RCONTROL] = 0; keyboardState[NativeMethods.VK_MENU] = 0; keyboardState[NativeMethods.VK_LMENU] = 0; keyboardState[NativeMethods.VK_RMENU] = 0; keyboardState[NativeMethods.VK_LWIN] = 0; keyboardState[NativeMethods.VK_RWIN] = 0; var charResult = new byte[2]; var charCount = NativeMethods.ToAscii(virtualKey, NativeMethods.MapVirtualKey(virtualKey, 0), keyboardState, charResult, 0); // TODO: This could probably be handled better. For now, we'll just return the last character. return(charCount > 0 ? Convert.ToInt32(charResult[charCount - 1]) : virtualKey); }
private 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; }
private void LoadConfigurationAndRefreshComponents(Configuration configuration) { m_indentedFormattingModifierKeys = configuration.IndentedFormattingConversionHotKeyModifierKey; m_indentedFormattingMainKeys = configuration.IndentedFormattingConversionHotKeyMainKey; m_compactFormattingModifierKeys = configuration.CompactFormattingConversionHotKeyModifierKey; m_compactFormattingMainKeys = configuration.CompactFormattingConversionHotKeyMainKey; m_displaySuccessMessageEnabled = configuration.DisplaySuccessNotificationEnabled; m_waitWindowReadyMs = configuration.WaitWindowReadyMs; m_previousWaitWindowReadyMs = m_waitWindowReadyMs; IndentedFormattingModifierKeyBindingTextBox.Text = m_indentedFormattingModifierKeys.ToString(); IndentedFormattingMainKeyBindingTextBox.Text = m_indentedFormattingMainKeys.ToString(); CompactFormattingModifierKeyBindingTextBox.Text = m_compactFormattingModifierKeys.ToString(); CompactFormattingMainKeyBindingTextBox.Text = m_compactFormattingMainKeys.ToString(); DisplaySuccessNotificationEnabled.Checked = m_displaySuccessMessageEnabled; TimeWaitingMs.Text = m_waitWindowReadyMs.ToString(); }
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); }
private void UpdatingHook(ref KeyboardHook hook, EventHandler <KeyPressedEventArgs> handler, KeyboardHook.ModifierKeys modifierKeys, Keys mainKeys) { if (modifierKeys == KeyboardHook.ModifierKeys.None || mainKeys == Keys.None) { return; } if (hook != null) { hook.Dispose(); } hook = new KeyboardHook(); hook.RegisterHotKey(modifierKeys, mainKeys); hook.KeyPressed += handler; }
internal KeyPressedEventArgs(KeyboardHook.ModifierKeys modifier, Keys key) { _modifier = modifier; _key = key; }
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; }