/// <summary> /// Create a new EventArg for the key /// </summary> /// <param name="keyCode"></param> public KeyboardHookEventArgs(Keys keyCode) { Key = keyCode; try { // we have to use Windows Form to get the modifiers as won't work if no focus Alt = (System.Windows.Forms.Control.ModifierKeys & Keys.Alt) != 0; Control = (System.Windows.Forms.Control.ModifierKeys & Keys.Control) != 0; Shift = (System.Windows.Forms.Control.ModifierKeys & Keys.Shift) != 0; // combine the modifiers Modifiers = WinAPI.KeyModifiers.None; if (Alt) { Modifiers |= WinAPI.KeyModifiers.Alt; } if (Control) { Modifiers |= WinAPI.KeyModifiers.Control; } if (Shift) { Modifiers |= WinAPI.KeyModifiers.Shift; } } catch (Exception) { } }
/// <summary> /// Create a new KeyboardHookEventArgs for a key and modifier /// </summary> /// <param name="key"></param> /// <param name="modifier"></param> public KeyboardHookEventArgs(Keys key, WinAPI.KeyModifiers modifier) { Key = key; Modifiers = modifier; Alt = (modifier & WinAPI.KeyModifiers.Alt) != 0; Control = (modifier & WinAPI.KeyModifiers.Control) != 0; Shift = (modifier & WinAPI.KeyModifiers.Shift) != 0; }
/// <summary> /// Check if a specified Hotkey is available /// </summary> /// <param name="form">owning form</param> /// <param name="key">hot key Key</param> /// <param name="modifier">hoy key Modifier</param> /// <returns>true if available</returns> public static bool IsHotkeyAvailable(Form form, Keys key, WinAPI.KeyModifiers modifier) { var available = WinAPI.RegisterHotKey(form.Handle, 0, modifier, key); if (available) { WinAPI.UnregisterHotKey(form.Handle, 0); } return(available); }
/// <summary> /// Read the saved Xml to load the hotkey /// </summary> /// <param name="reader">XmlReader</param> public void ReadXml(XmlReader reader) { reader.MoveToContent(); if (reader.IsEmptyElement) { reader.Read(); return; } reader.Read(); while (reader.EOF == false) { if (reader.IsStartElement()) { switch (reader.Name) { case "modifiers": Modifiers = (WinAPI.KeyModifiers)BitConverter.ToInt32( Authenticator.StringToByteArray(reader.ReadElementContentAsString()), 0); break; case "key": Key = (WinAPI.VirtualKeyCode)BitConverter.ToUInt16( Authenticator.StringToByteArray(reader.ReadElementContentAsString()), 0); break; case "action": Action = (HotKeyActions)Enum.Parse(typeof(HotKeyActions), reader.ReadElementContentAsString(), true); break; case "window": Window = reader.ReadElementContentAsString(); break; case "advanced": Advanced = reader.ReadElementContentAsString(); break; default: reader.Skip(); break; } } else { reader.Read(); break; } } }
/// <summary> /// Set up the hook into Windows /// </summary> protected void Hook() { for (int i = 0; i < m_hooked.Count; i++) { WinAuthAuthenticator auth = m_hooked[i]; Keys key = (Keys)auth.HotKey.Key; WinAPI.KeyModifiers modifier = auth.HotKey.Modifiers; if (WinAPI.RegisterHotKey(m_form.Handle, i + 1, modifier | WinAPI.KeyModifiers.NoRepeat, key) == false) { // the MOD_NOREPEAT flag is not support in XP or 2003 and we should get a fail, so we call again without it WinAPI.RegisterHotKey(m_form.Handle, i + 1, modifier, key); } } }
/// <summary> /// Create a new HotKeySequence from a loaded string /// </summary> /// <param name="data"></param> public HoyKeySequence(string data) { if (string.IsNullOrEmpty(data) == false) { Match match = Regex.Match(data, @"([0-9a-fA-F]{8})([0-9a-fA-F]{4})\t([^\t]*)\t(Y|N)(.*)", RegexOptions.Multiline); if (match.Success == true) { Modifiers = (WinAPI.KeyModifiers)BitConverter.ToInt32(Authenticator.StringToByteArray(match.Groups[1].Value), 0); HotKey = (WinAPI.VirtualKeyCode)BitConverter.ToUInt16(Authenticator.StringToByteArray(match.Groups[2].Value), 0); WindowTitle = match.Groups[3].Value; Advanced = (match.Groups[4].Value == "Y"); if (Advanced == true) { AdvancedScript = match.Groups[5].Value; } } } }
/// <summary> /// Click the OK button to save the keys /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void okButton_Click(object sender, EventArgs e) { WinAPI.VirtualKeyCode key = (keyCombo.SelectedItem as KeyItem != null ? ((KeyItem)keyCombo.SelectedItem).Key : default(WinAPI.VirtualKeyCode)); if (key == 0) { this.Hotkey = null; } else if (this.Hotkey == null) { this.Hotkey = new HotKey(); } if (this.Hotkey != null) { WinAPI.KeyModifiers modifiers = WinAPI.KeyModifiers.None; if (shiftToggle.Checked) { modifiers |= WinAPI.KeyModifiers.Shift; } if (ctrlToggle.Checked) { modifiers |= WinAPI.KeyModifiers.Control; } if (altToggle.Checked) { modifiers |= WinAPI.KeyModifiers.Alt; } // check it is available if this is a different hotkey if ((key != this.Hotkey.Key || modifiers != this.Hotkey.Modifiers) && KeyboardHook.IsHotkeyAvailable(this, (Keys)key, modifiers) == false) { WinAuthForm.ErrorDialog(this, strings.HotKeyNotAvailable); this.DialogResult = System.Windows.Forms.DialogResult.None; return; } this.Hotkey.Key = key; this.Hotkey.Modifiers = modifiers; if (notifyRadioButton.Checked == true) { this.Hotkey.Action = HotKey.HotKeyActions.Notify; this.Hotkey.Window = null; this.Hotkey.Advanced = null; } else if (injectRadioButton.Checked == true) { this.Hotkey.Action = HotKey.HotKeyActions.Inject; this.Hotkey.Window = this.injectTextbox.Text; this.Hotkey.Advanced = null; } else if (pasteRadioButton.Checked == true) { this.Hotkey.Action = HotKey.HotKeyActions.Copy; this.Hotkey.Window = null; this.Hotkey.Advanced = null; } else if (advancedRadioButton.Checked == true) { this.Hotkey.Action = HotKey.HotKeyActions.Advanced; this.Hotkey.Window = null; this.Hotkey.Advanced = this.advancedTextbox.Text; } } }
/// <summary> /// Create a new EventArg for the key /// </summary> /// <param name="keyCode"></param> public KeyboardHookEventArgs(Keys keyCode) { this.Key = (Keys)keyCode; try { // we have to use Windows Form to get the modifiers as won't work if no focus this.Alt = (System.Windows.Forms.Control.ModifierKeys & Keys.Alt) != 0; this.Control = (System.Windows.Forms.Control.ModifierKeys & Keys.Control) != 0; this.Shift = (System.Windows.Forms.Control.ModifierKeys & Keys.Shift) != 0; // combine the modifiers Modifiers = WinAPI.KeyModifiers.None; if (this.Alt) { Modifiers |= WinAPI.KeyModifiers.Alt; } if (this.Control) { Modifiers |= WinAPI.KeyModifiers.Control; } if (this.Shift) { Modifiers |= WinAPI.KeyModifiers.Shift; } } catch (Exception) { } }
/// <summary> /// Create a new HotKeySequence from a loaded string /// </summary> /// <param name="data">XmlNode from config</param> public HoyKeySequence(XmlNode autoLoginNode, string password, decimal version) { bool boolVal = false; XmlNode node = autoLoginNode.SelectSingleNode("modifiers"); if (node != null && node.InnerText.Length != 0) { Modifiers = (WinAPI.KeyModifiers)BitConverter.ToInt32(Authenticator.StringToByteArray(node.InnerText), 0); } node = autoLoginNode.SelectSingleNode("hotkey"); if (node != null && node.InnerText.Length != 0) { HotKey = (WinAPI.VirtualKeyCode)BitConverter.ToUInt16(Authenticator.StringToByteArray(node.InnerText), 0); } node = autoLoginNode.SelectSingleNode("windowtitle"); if (node != null && node.InnerText.Length != 0) { WindowTitle = node.InnerText; } node = autoLoginNode.SelectSingleNode("windowtitleregex"); if (node != null && bool.TryParse(node.InnerText, out boolVal) == true) { WindowTitleRegex = boolVal; } node = autoLoginNode.SelectSingleNode("processname"); if (node != null && node.InnerText.Length != 0) { ProcessName = node.InnerText; } node = autoLoginNode.SelectSingleNode("advanced"); if (node != null && bool.TryParse(node.InnerText, out boolVal) == true) { Advanced = boolVal; } node = autoLoginNode.SelectSingleNode("script"); if (node != null && node.InnerText.Length != 0) { string data = node.InnerText; XmlAttribute attr = node.Attributes["encrypted"]; if (attr != null && attr.InnerText.Length != 0) { char[] encTypes = attr.InnerText.ToCharArray(); // we read the string in reverse order (the order they were encrypted) for (int i = encTypes.Length - 1; i >= 0; i--) { char encryptedType = encTypes[i]; switch (encryptedType) { case 'u': { // we are going to decrypt with the Windows User account key byte[] cipher = Authenticator.StringToByteArray(data); byte[] plain = ProtectedData.Unprotect(cipher, null, DataProtectionScope.CurrentUser); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } case 'm': { // we are going to decrypt with the Windows local machine key byte[] cipher = Authenticator.StringToByteArray(data); byte[] plain = ProtectedData.Unprotect(cipher, null, DataProtectionScope.LocalMachine); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } case 'y': { // we use an explicit password to encrypt data if (string.IsNullOrEmpty(password) == true) { throw new EncrpytedSecretDataException(); } data = Authenticator.Decrypt(data, password, (version >= (decimal)1.7)); // changed encrypted in 1.7 byte[] plain = Authenticator.StringToByteArray(data); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } default: break; } } } AdvancedScript = data; } }
/// <summary> /// Create a new KeyboardHookEventArgs for a key and modifier /// </summary> /// <param name="key"></param> /// <param name="modifier"></param> public KeyboardHookEventArgs(Keys key, WinAPI.KeyModifiers modifier) { this.Key = key; this.Modifiers = modifier; this.Alt = (modifier & WinAPI.KeyModifiers.Alt) != 0; this.Control = (modifier & WinAPI.KeyModifiers.Control) != 0; this.Shift = (modifier & WinAPI.KeyModifiers.Shift) != 0; }
public void HoyKeySequence_Load() { // all possible modifiers WinAPI.KeyModifiers[] modifiers = new WinAPI.KeyModifiers[] { WinAPI.KeyModifiers.None, WinAPI.KeyModifiers.Alt, WinAPI.KeyModifiers.Control, WinAPI.KeyModifiers.Shift, WinAPI.KeyModifiers.Alt | WinAPI.KeyModifiers.Control, WinAPI.KeyModifiers.Alt | WinAPI.KeyModifiers.Shift, WinAPI.KeyModifiers.Control | WinAPI.KeyModifiers.Shift, WinAPI.KeyModifiers.Alt | WinAPI.KeyModifiers.Control | WinAPI.KeyModifiers.Shift }; // loop through each VKey foreach (WinAPI.KeyModifiers modifier in modifiers) { HoyKeySequence hk1 = new HoyKeySequence(null); hk1.Modifiers = modifier; hk1.Advanced = false; foreach (WinAPI.VirtualKeyCode vk in Enum.GetValues(typeof(WinAPI.VirtualKeyCode))) { hk1.HotKey = vk; string s1 = hk1.ToString(); HoyKeySequence hk2 = new HoyKeySequence(s1); string s2 = hk2.ToString(); Console.Out.WriteLine(Convert.ToSingle(modifier) + ":" + Convert.ToString(vk)); Assert.AreEqual(s1, s2); } } }
/// <summary> /// Read the saved Xml to load the hotkey /// </summary> /// <param name="reader">XmlReader</param> public void ReadXml(XmlReader reader) { reader.MoveToContent(); if (reader.IsEmptyElement) { reader.Read(); return; } reader.Read(); while (reader.EOF == false) { if (reader.IsStartElement()) { switch (reader.Name) { case "modifiers": Modifiers = (WinAPI.KeyModifiers)BitConverter.ToInt32(Authenticator.StringToByteArray(reader.ReadElementContentAsString()), 0); break; case "key": Key = (WinAPI.VirtualKeyCode)BitConverter.ToUInt16(Authenticator.StringToByteArray(reader.ReadElementContentAsString()), 0); break; case "action": Action = (HotKeyActions)Enum.Parse(typeof(HotKeyActions), reader.ReadElementContentAsString(), true); break; case "window": Window = reader.ReadElementContentAsString(); break; case "advanced": Advanced = reader.ReadElementContentAsString(); break; default: reader.Skip(); break; } } else { reader.Read(); break; } } }
public void ReadXml(XmlReader reader, string password = null) { reader.MoveToContent(); if (reader.IsEmptyElement) { reader.Read(); return; } reader.Read(); while (reader.EOF == false) { if (reader.IsStartElement()) { switch (reader.Name) { case "modifiers": Modifiers = (WinAPI.KeyModifiers)BitConverter.ToInt32(Authenticator.StringToByteArray(reader.ReadElementContentAsString()), 0); break; case "hotkey": HotKey = (WinAPI.VirtualKeyCode)BitConverter.ToUInt16(Authenticator.StringToByteArray(reader.ReadElementContentAsString()), 0); break; case "windowtitle": WindowTitle = reader.ReadElementContentAsString(); break; case "windowtitleregex": WindowTitleRegex = reader.ReadElementContentAsBoolean(); break; case "processname": ProcessName = reader.ReadElementContentAsString(); break; case "advanced": Advanced = reader.ReadElementContentAsBoolean(); break; case "script": string encrypted = reader.GetAttribute("encrypted"); string data = reader.ReadElementContentAsString(); if (string.IsNullOrEmpty(encrypted) == false) { Authenticator.PasswordTypes passwordType = Authenticator.DecodePasswordTypes(encrypted); data = Authenticator.DecryptSequence(data, passwordType, password, true); //byte[] plain = Authenticator.StringToByteArray(data); //data = Encoding.UTF8.GetString(plain, 0, plain.Length); /* * char[] encTypes = encrypted.ToCharArray(); * // we read the string in reverse order (the order they were encrypted) * for (int i = encTypes.Length - 1; i >= 0; i--) * { * char encryptedType = encTypes[i]; * switch (encryptedType) * { * case 'u': * { * // we are going to decrypt with the Windows User account key * byte[] cipher = Authenticator.StringToByteArray(data); * byte[] plain = ProtectedData.Unprotect(cipher, null, DataProtectionScope.CurrentUser); * data = Encoding.UTF8.GetString(plain, 0, plain.Length); * break; * } * case 'm': * { * // we are going to decrypt with the Windows local machine key * byte[] cipher = Authenticator.StringToByteArray(data); * byte[] plain = ProtectedData.Unprotect(cipher, null, DataProtectionScope.LocalMachine); * data = Encoding.UTF8.GetString(plain, 0, plain.Length); * break; * } * case 'y': * { * // we use an explicit password to encrypt data * if (string.IsNullOrEmpty(password) == true) * { * throw new EncryptedSecretDataException(); * } * data = Authenticator.Decrypt(data, password, true); * byte[] plain = Authenticator.StringToByteArray(data); * data = Encoding.UTF8.GetString(plain, 0, plain.Length); * break; * } * default: * break; * } * } */ } AdvancedScript = data; break; default: reader.Skip(); break; } } else { reader.Read(); break; } } }
/// <summary> /// Create a new HotKeySequence from a loaded string /// </summary> /// <param name="data">XmlNode from config</param> public HoyKeySequence(XmlNode autoLoginNode, string password, decimal version) { bool boolVal = false; XmlNode node = autoLoginNode.SelectSingleNode("modifiers"); if (node != null && node.InnerText.Length != 0) { Modifiers = (WinAPI.KeyModifiers)BitConverter.ToInt32(Authenticator.StringToByteArray(node.InnerText), 0); } node = autoLoginNode.SelectSingleNode("hotkey"); if (node != null && node.InnerText.Length != 0) { HotKey = (WinAPI.VirtualKeyCode)BitConverter.ToUInt16(Authenticator.StringToByteArray(node.InnerText), 0); } node = autoLoginNode.SelectSingleNode("windowtitle"); if (node != null && node.InnerText.Length != 0) { WindowTitle = node.InnerText; } node = autoLoginNode.SelectSingleNode("windowtitleregex"); if (node != null && bool.TryParse(node.InnerText, out boolVal) == true) { WindowTitleRegex = boolVal; } node = autoLoginNode.SelectSingleNode("processname"); if (node != null && node.InnerText.Length != 0) { ProcessName = node.InnerText; } node = autoLoginNode.SelectSingleNode("advanced"); if (node != null && bool.TryParse(node.InnerText, out boolVal) == true) { Advanced = boolVal; } node = autoLoginNode.SelectSingleNode("script"); if (node != null && node.InnerText.Length != 0) { string data = node.InnerText; XmlAttribute attr = node.Attributes["encrypted"]; if (attr != null && attr.InnerText.Length != 0) { char[] encTypes = attr.InnerText.ToCharArray(); // we read the string in reverse order (the order they were encrypted) for (int i = encTypes.Length - 1; i >= 0; i--) { char encryptedType = encTypes[i]; switch (encryptedType) { case 'u': { // we are going to decrypt with the Windows User account key byte[] cipher = Authenticator.StringToByteArray(data); byte[] plain = ProtectedData.Unprotect(cipher, null, DataProtectionScope.CurrentUser); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } case 'm': { // we are going to decrypt with the Windows local machine key byte[] cipher = Authenticator.StringToByteArray(data); byte[] plain = ProtectedData.Unprotect(cipher, null, DataProtectionScope.LocalMachine); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } case 'y': { // we use an explicit password to encrypt data if (string.IsNullOrEmpty(password) == true) { throw new EncryptedSecretDataException(); } data = Authenticator.Decrypt(data, password, (version >= (decimal)1.7)); // changed encrypted in 1.7 byte[] plain = Authenticator.StringToByteArray(data); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } default: break; } } } AdvancedScript = data; } }
public void ReadXml(XmlReader reader, string password = null) { reader.MoveToContent(); if (reader.IsEmptyElement) { reader.Read(); return; } reader.Read(); while (reader.EOF == false) { if (reader.IsStartElement()) { switch (reader.Name) { case "modifiers": Modifiers = (WinAPI.KeyModifiers)BitConverter.ToInt32(Authenticator.StringToByteArray(reader.ReadElementContentAsString()), 0); break; case "hotkey": HotKey = (WinAPI.VirtualKeyCode)BitConverter.ToUInt16(Authenticator.StringToByteArray(reader.ReadElementContentAsString()), 0); break; case "windowtitle": WindowTitle = reader.ReadElementContentAsString(); break; case "windowtitleregex": WindowTitleRegex = reader.ReadElementContentAsBoolean(); break; case "processname": ProcessName = reader.ReadElementContentAsString(); break; case "advanced": Advanced = reader.ReadElementContentAsBoolean(); break; case "script": string encrypted = reader.GetAttribute("encrypted"); string data = reader.ReadElementContentAsString(); if (string.IsNullOrEmpty(encrypted) == false) { Authenticator.PasswordTypes passwordType = Authenticator.DecodePasswordTypes(encrypted); data = Authenticator.DecryptSequence(data, passwordType, password, null, true); //byte[] plain = Authenticator.StringToByteArray(data); //data = Encoding.UTF8.GetString(plain, 0, plain.Length); /* char[] encTypes = encrypted.ToCharArray(); // we read the string in reverse order (the order they were encrypted) for (int i = encTypes.Length - 1; i >= 0; i--) { char encryptedType = encTypes[i]; switch (encryptedType) { case 'u': { // we are going to decrypt with the Windows User account key byte[] cipher = Authenticator.StringToByteArray(data); byte[] plain = ProtectedData.Unprotect(cipher, null, DataProtectionScope.CurrentUser); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } case 'm': { // we are going to decrypt with the Windows local machine key byte[] cipher = Authenticator.StringToByteArray(data); byte[] plain = ProtectedData.Unprotect(cipher, null, DataProtectionScope.LocalMachine); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } case 'y': { // we use an explicit password to encrypt data if (string.IsNullOrEmpty(password) == true) { throw new EncrpytedSecretDataException(); } data = Authenticator.Decrypt(data, password, true); byte[] plain = Authenticator.StringToByteArray(data); data = Encoding.UTF8.GetString(plain, 0, plain.Length); break; } default: break; } } */ } AdvancedScript = data; break; default: reader.Skip(); break; } } else { reader.Read(); break; } } }