1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using KeePass.App;
using KeePass.Forms;
using KeePass.Native;
using KeePass.Resources;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Utility;
using NativeLib = KeePassLib.Native.NativeLib;
namespace KeePass.Util
{
public static class HotKeyManager
{
private static Form g_fRecvWnd = null;
private static readonly Dictionary<int, Keys> g_dRegKeys = new Dictionary<int, Keys>();
// private static NativeMethods.BindKeyHandler g_hOnHotKey =
// new NativeMethods.BindKeyHandler(HotKeyManager.OnHotKey);
// public static Form ReceiverWindow
// {
// get { return g_fRecvWnd; }
// set { g_fRecvWnd = value; }
// }
public static bool Initialize(Form fRecvWnd)
{
g_fRecvWnd = fRecvWnd;
// if(NativeLib.IsUnix())
// {
// try { NativeMethods.tomboy_keybinder_init(); }
// catch(Exception) { Debug.Assert(false); return false; }
// }
return true;
}
public static bool RegisterHotKey(int nId, Keys kKey)
{
UnregisterHotKey(nId);
uint uMod = 0;
if((kKey & Keys.Shift) != Keys.None) uMod |= NativeMethods.MOD_SHIFT;
if((kKey & Keys.Alt) != Keys.None) uMod |= NativeMethods.MOD_ALT;
if((kKey & Keys.Control) != Keys.None) uMod |= NativeMethods.MOD_CONTROL;
uint vkCode = (uint)(kKey & Keys.KeyCode);
if(vkCode == (uint)Keys.None) return false; // Don't register mod keys only
try
{
if(!NativeLib.IsUnix())
{
if(NativeMethods.RegisterHotKey(g_fRecvWnd.Handle, nId, uMod, vkCode))
{
g_dRegKeys[nId] = kKey;
return true;
}
}
else // Unix
{
// NativeMethods.tomboy_keybinder_bind(EggAccKeysToString(kKey),
// g_hOnHotKey);
// g_dRegKeys[nId] = kKey;
// return true;
}
}
catch(Exception) { Debug.Assert(false); }
return false;
}
public static bool UnregisterHotKey(int nId)
{
if(g_dRegKeys.ContainsKey(nId))
{
// Keys k = g_dRegKeys[nId];
g_dRegKeys.Remove(nId);
try
{
bool bResult;
if(!NativeLib.IsUnix())
bResult = NativeMethods.UnregisterHotKey(g_fRecvWnd.Handle, nId);
else // Unix
{
// NativeMethods.tomboy_keybinder_unbind(EggAccKeysToString(k),
// g_hOnHotKey);
// bResult = true;
bResult = false;
}
// Debug.Assert(bResult);
return bResult;
}
catch(Exception) { Debug.Assert(false); }
}
return false;
}
public static void UnregisterAll()
{
List<int> vIDs = new List<int>(g_dRegKeys.Keys);
foreach(int nID in vIDs) UnregisterHotKey(nID);
Debug.Assert(g_dRegKeys.Count == 0);
}
public static bool IsHotKeyRegistered(Keys kKey, bool bGlobal)
{
if(g_dRegKeys.ContainsValue(kKey)) return true;
if(!bGlobal) return false;
int nID = AppDefs.GlobalHotKeyId.TempRegTest;
if(!RegisterHotKey(nID, kKey)) return true;
UnregisterHotKey(nID);
return false;
}
/* private static void OnHotKey(string strKey, IntPtr lpUserData)
{
if(string.IsNullOrEmpty(strKey)) return;
if(strKey.IndexOf(@"<Release>", StrUtil.CaseIgnoreCmp) >= 0) return;
if(g_fRecvWnd != null)
{
MainForm mf = (g_fRecvWnd as MainForm);
if(mf == null) { Debug.Assert(false); return; }
Keys k = EggAccStringToKeys(strKey);
foreach(KeyValuePair<int, Keys> kvp in g_dRegKeys)
{
if(kvp.Value == k) mf.HandleHotKey(kvp.Key);
}
}
else { Debug.Assert(false); }
}
private static Keys EggAccStringToKeys(string strKey)
{
if(string.IsNullOrEmpty(strKey)) return Keys.None;
Keys k = Keys.None;
if(strKey.IndexOf(@"<Alt>", StrUtil.CaseIgnoreCmp) >= 0)
k |= Keys.Alt;
if((strKey.IndexOf(@"<Ctl>", StrUtil.CaseIgnoreCmp) >= 0) ||
(strKey.IndexOf(@"<Ctrl>", StrUtil.CaseIgnoreCmp) >= 0) ||
(strKey.IndexOf(@"<Control>", StrUtil.CaseIgnoreCmp) >= 0))
k |= Keys.Control;
if((strKey.IndexOf(@"<Shft>", StrUtil.CaseIgnoreCmp) >= 0) ||
(strKey.IndexOf(@"<Shift>", StrUtil.CaseIgnoreCmp) >= 0))
k |= Keys.Shift;
string strKeyCode = strKey;
while(strKeyCode.IndexOf('<') >= 0)
{
int nStart = strKeyCode.IndexOf('<');
int nEnd = strKeyCode.IndexOf('>');
if((nStart < 0) || (nEnd < 0) || (nEnd <= nStart)) { Debug.Assert(false); break; }
strKeyCode = strKeyCode.Remove(nStart, nEnd - nStart + 1);
}
strKeyCode = strKeyCode.Trim();
try { k |= (Keys)Enum.Parse(typeof(Keys), strKeyCode, true); }
catch(Exception) { Debug.Assert(false); }
return k;
}
private static string EggAccKeysToString(Keys k)
{
StringBuilder sb = new StringBuilder();
if((k & Keys.Shift) != Keys.None) sb.Append(@"<Shift>");
if((k & Keys.Control) != Keys.None) sb.Append(@"<Control>");
if((k & Keys.Alt) != Keys.None) sb.Append(@"<Alt>");
sb.Append((k & Keys.KeyCode).ToString());
return sb.ToString();
} */
internal static void CheckCtrlAltA(Form fParent)
{
try
{
if(!Program.Config.Integration.CheckHotKeys) return;
if(NativeLib.IsUnix()) return;
// Check for a conflict only in the very specific case of
// Ctrl+Alt+A; in all other cases we assume that the user
// is aware of a possible conflict and intentionally wants
// to override any system key combination
if(Program.Config.Integration.HotKeyGlobalAutoType !=
(long)(Keys.Control | Keys.Alt | Keys.A)) return;
// Check for a conflict only on certain systems; other
// languages typically don't use Ctrl+Alt+A frequently
// and a conflict warning would just be confusing for
// most users
IntPtr hKL = NativeMethods.GetKeyboardLayout(0);
ushort uLangID = (ushort)(hKL.ToInt64() & 0xFFFFL);
ushort uPriLangID = NativeMethods.GetPrimaryLangID(uLangID);
string strKlid = NativeMethods.GetKeyboardLayoutNameEx();
if((uPriLangID != NativeMethods.LANG_POLISH) &&
(strKlid != NativeMethods.KLID_FRENCH_STD_AZERTY))
return;
const int vk = (int)Keys.A;
// We actually check for RAlt (which maps to Ctrl+Alt)
// instead of LCtrl+LAlt
byte[] pbState = new byte[256];
pbState[NativeMethods.VK_CONTROL] = 0x80;
pbState[NativeMethods.VK_MENU] = 0x80;
pbState[NativeMethods.VK_RMENU] = 0x80;
pbState[NativeMethods.VK_NUMLOCK] = 0x01; // Toggled
// pbState[vk] = 0x80;
string strUni = NativeMethods.ToUnicode3(vk, pbState, IntPtr.Zero);
if(string.IsNullOrEmpty(strUni)) return;
if(strUni.EndsWith("a") || strUni.EndsWith("A")) return;
if(char.IsControl(strUni, 0)) { Debug.Assert(false); strUni = "?"; }
string str = KPRes.CtrlAltAConflict.Replace("{PARAM}", strUni) +
MessageService.NewParagraph + KPRes.CtrlAltAConflictHint;
VistaTaskDialog dlg = new VistaTaskDialog();
dlg.AddButton((int)DialogResult.Cancel, KPRes.Ok, null);
dlg.CommandLinks = false;
dlg.Content = str;
dlg.DefaultButtonID = (int)DialogResult.Cancel;
dlg.EnableHyperlinks = true;
dlg.FooterText = VistaTaskDialog.CreateLink("h", KPRes.MoreInfo);
dlg.MainInstruction = KPRes.KeyboardKeyCtrl + "+" +
KPRes.KeyboardKeyAlt + "+A - " + KPRes.Warning;
dlg.SetIcon(VtdIcon.Warning);
dlg.SetFooterIcon(VtdIcon.Information);
dlg.VerificationText = UIUtil.GetDialogNoShowAgainText(null);
dlg.WindowTitle = PwDefs.ShortProductName;
dlg.LinkClicked += delegate(object sender, LinkClickedEventArgs e)
{
if((e != null) && (e.LinkText == "h"))
AppHelp.ShowHelp(AppDefs.HelpTopics.FaqTech,
AppDefs.HelpTopics.FaqTechHotKey);
};
if(dlg.ShowDialog(fParent))
{
if(dlg.ResultVerificationChecked)
Program.Config.Integration.CheckHotKeys = false;
}
// else MessageService.ShowWarning(str); // Check box is required
}
catch(Exception) { Debug.Assert(false); }
}
internal static bool HandleHotKeyIntoSelf(int wParam)
{
try
{
OptionsForm f = (GlobalWindowManager.TopWindow as OptionsForm);
if(f == null) return false;
IntPtr h = NativeMethods.GetForegroundWindowHandle();
if(h != f.Handle) return false;
HotKeyControlEx c = (UIUtil.GetActiveControl(f) as HotKeyControlEx);
if(c == null) return false;
Keys k;
if(!g_dRegKeys.TryGetValue(wParam, out k)) return false;
if(k == Keys.None) { Debug.Assert(false); return false; }
c.HotKey = k;
return true;
}
catch(Exception) { Debug.Assert(false); }
return false;
}
}
}
|