HotKeys.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #include "stdafx.h"
  2. #include "HotKeys.h"
  3. #include "Options.h"
  4. #include "Misc.h"
  5. #include "SendKeys.h"
  6. #include "Accels.h"
  7. CHotKeys g_HotKeys;
  8. int CHotKey::m_nextId = 0;
  9. CHotKey::CHotKey(CString name, DWORD defKey, bool bUnregOnShowDitto, HotKeyType hkType, CString description)
  10. : m_Name(name),
  11. m_description(description),
  12. m_bIsRegistered(false),
  13. m_bUnRegisterOnShowDitto(bUnregOnShowDitto),
  14. m_clipId(0)
  15. {
  16. m_Atom = ::GlobalAddAtom(StrF(_T("%s_%d"), m_Name, hkType));
  17. ASSERT(m_Atom);
  18. m_Key = (DWORD)g_Opt.GetProfileLong(m_Name, (long) defKey);
  19. m_globalId = m_nextId;
  20. m_nextId++;
  21. m_hkType = hkType;
  22. g_HotKeys.Add(this);
  23. }
  24. CHotKey::~CHotKey()
  25. {
  26. Unregister();
  27. ::GlobalDeleteAtom(m_Atom);
  28. }
  29. CString CHotKey::GetHotKeyDisplay()
  30. {
  31. return GetHotKeyDisplayStatic(m_Key);
  32. }
  33. CString CHotKey::GetHotKeyDisplayStatic(DWORD dwHotKey)
  34. {
  35. WORD vk = LOBYTE(dwHotKey);
  36. CString keyDisplay;
  37. UINT modifiers = GetModifier(HIBYTE(dwHotKey));
  38. if(modifiers & MOD_SHIFT)
  39. {
  40. keyDisplay += _T("Shift + ");
  41. }
  42. if(modifiers & MOD_CONTROL)
  43. {
  44. keyDisplay += _T("Ctrl + ");
  45. }
  46. if(modifiers & MOD_ALT)
  47. {
  48. keyDisplay += _T("Alt + ");
  49. }
  50. if(modifiers & MOD_WIN)
  51. {
  52. keyDisplay += _T("Win + ");
  53. }
  54. switch (vk)
  55. {
  56. case VK_MOUSE_CLICK:
  57. keyDisplay += "Click";
  58. break;
  59. case VK_MOUSE_DOUBLE_CLICK:
  60. keyDisplay += "Double Click";
  61. break;
  62. case VK_MOUSE_RIGHT_CLICK:
  63. keyDisplay += "Right Click";
  64. break;
  65. case VK_MOUSE_MIDDLE_CLICK:
  66. keyDisplay += "Middle Click";
  67. break;
  68. default:
  69. keyDisplay += GetVirKeyName(vk);
  70. break;
  71. }
  72. return keyDisplay;
  73. }
  74. //http://www.ffuts.org/blog/mapvirtualkey-getkeynametext-and-a-story-of-how-to/
  75. CString CHotKey::GetVirKeyName(unsigned int virtualKey)
  76. {
  77. unsigned int scanCode = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);
  78. // because MapVirtualKey strips the extended bit for some keys
  79. switch (virtualKey)
  80. {
  81. case VK_LEFT:
  82. case VK_UP:
  83. case VK_RIGHT:
  84. case VK_DOWN: // arrow keys
  85. case VK_PRIOR:
  86. case VK_NEXT: // page up and page down
  87. case VK_END:
  88. case VK_HOME:
  89. case VK_INSERT:
  90. case VK_DELETE:
  91. case VK_DIVIDE: // numpad slash
  92. case VK_NUMLOCK:
  93. {
  94. scanCode |= 0x100; // set extended bit
  95. break;
  96. }
  97. }
  98. wchar_t keyName[50];
  99. if (GetKeyNameText(scanCode << 16, keyName, sizeof(keyName)) != 0)
  100. {
  101. return keyName;
  102. }
  103. else
  104. {
  105. return "[Error]";
  106. }
  107. }
  108. UINT CHotKey::GetModifier(DWORD dwHotKey)
  109. {
  110. UINT uMod = 0;
  111. if(dwHotKey & HOTKEYF_SHIFT)
  112. uMod |= MOD_SHIFT;
  113. if(dwHotKey & HOTKEYF_CONTROL)
  114. uMod |= MOD_CONTROL;
  115. if(dwHotKey & HOTKEYF_ALT)
  116. uMod |= MOD_ALT;
  117. if(dwHotKey & HOTKEYF_EXT)
  118. uMod |= MOD_WIN;
  119. return uMod;
  120. }
  121. void CHotKey::SetKey( DWORD key, bool bSave )
  122. {
  123. if(m_Key == key)
  124. {
  125. return;
  126. }
  127. if(m_bIsRegistered)
  128. Unregister();
  129. m_Key = key;
  130. if(bSave)
  131. SaveKey();
  132. }
  133. void CHotKey::LoadKey()
  134. {
  135. SetKey((DWORD) g_Opt.GetProfileLong(m_Name, 0));
  136. }
  137. bool CHotKey::SaveKey()
  138. {
  139. if(m_clipId <= 0)
  140. {
  141. return g_Opt.SetProfileLong( m_Name, (long) m_Key ) != FALSE;
  142. }
  143. return false;
  144. }
  145. BOOL CHotKey::ValidateHotKey(DWORD dwHotKey)
  146. {
  147. ATOM id = ::GlobalAddAtom(_T("HK_VALIDATE"));
  148. BOOL bResult = ::RegisterHotKey( g_HotKeys.m_hWnd,
  149. id,
  150. GetModifier(HIBYTE(dwHotKey)),
  151. LOBYTE(dwHotKey) );
  152. if(bResult)
  153. ::UnregisterHotKey(g_HotKeys.m_hWnd, id);
  154. ::GlobalDeleteAtom(id);
  155. return bResult;
  156. }
  157. void CHotKey::CopyFromCtrl(CHotKeyCtrl& ctrl, HWND hParent, int nWindowsCBID)
  158. {
  159. long lHotKey = ctrl.GetHotKey();
  160. short sKeyKode = LOBYTE(lHotKey);
  161. short sModifers = ((HIBYTE(lHotKey)) & ~HOTKEYF_EXT);
  162. if(lHotKey && ::IsDlgButtonChecked(hParent, nWindowsCBID))
  163. {
  164. sModifers |= HOTKEYF_EXT;
  165. }
  166. SetKey(MAKEWORD(sKeyKode, sModifers));
  167. }
  168. void CHotKey::CopyToCtrl(CHotKeyCtrl& ctrl, HWND hParent, int nWindowsCBID)
  169. {
  170. DWORD shortcut = ACCEL_MAKEKEY(LOBYTE(m_Key), ((HIBYTE(m_Key)) &~HOTKEYF_EXT));
  171. switch (LOBYTE(shortcut))
  172. {
  173. case VK_LEFT:
  174. case VK_UP:
  175. case VK_RIGHT:
  176. case VK_DOWN: // arrow keys
  177. case VK_PRIOR:
  178. case VK_NEXT: // page up and page down
  179. case VK_END:
  180. case VK_HOME:
  181. case VK_INSERT:
  182. case VK_DELETE:
  183. case VK_DIVIDE: // numpad slash
  184. case VK_NUMLOCK:
  185. {
  186. shortcut = ACCEL_MAKEKEY(LOBYTE(shortcut), (HIBYTE(shortcut) | HOTKEYF_EXT));
  187. }
  188. break;
  189. }
  190. long lModifiers = HIBYTE(shortcut);
  191. long keys = LOBYTE(shortcut);
  192. ctrl.SetHotKey((WORD)keys, (WORD)lModifiers);
  193. long originalModifiers = HIBYTE(m_Key);
  194. if(originalModifiers & HOTKEYF_EXT)
  195. {
  196. ::CheckDlgButton(hParent, nWindowsCBID, BST_CHECKED);
  197. }
  198. }
  199. bool CHotKey::Register()
  200. {
  201. if(m_Key)
  202. {
  203. if(m_bIsRegistered == false)
  204. {
  205. ASSERT(g_HotKeys.m_hWnd);
  206. m_bIsRegistered = ::RegisterHotKey(g_HotKeys.m_hWnd,
  207. m_Atom,
  208. GetModifier(),
  209. LOBYTE(m_Key) ) == TRUE;
  210. }
  211. }
  212. else
  213. {
  214. m_bIsRegistered = false;
  215. }
  216. return m_bIsRegistered;
  217. }
  218. bool CHotKey::Unregister(bool bOnShowingDitto)
  219. {
  220. if(!m_bIsRegistered)
  221. return true;
  222. if(bOnShowingDitto)
  223. {
  224. if(m_bUnRegisterOnShowDitto == false)
  225. return true;
  226. }
  227. if(m_Key)
  228. {
  229. ASSERT(g_HotKeys.m_hWnd);
  230. if(::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom))
  231. {
  232. m_bIsRegistered = false;
  233. return true;
  234. }
  235. else
  236. {
  237. Log(_T("Unregister FAILED!"));
  238. ASSERT(0);
  239. }
  240. }
  241. else
  242. {
  243. m_bIsRegistered = false;
  244. return true;
  245. }
  246. return false;
  247. }
  248. CHotKeys::CHotKeys() : m_hWnd(NULL)
  249. {
  250. }
  251. CHotKeys::~CHotKeys()
  252. {
  253. CHotKey* pHotKey;
  254. INT_PTR count = GetSize();
  255. for(int i=0; i < count; i++)
  256. {
  257. pHotKey = ElementAt(i);
  258. if(pHotKey)
  259. {
  260. delete pHotKey;
  261. }
  262. }
  263. }
  264. INT_PTR CHotKeys::Find(CHotKey* pHotKey)
  265. {
  266. INT_PTR count = GetSize();
  267. for(int i=0; i < count; i++)
  268. {
  269. if(pHotKey == ElementAt(i))
  270. {
  271. return i;
  272. }
  273. }
  274. return -1;
  275. }
  276. bool CHotKeys::Remove(CHotKey* pHotKey)
  277. {
  278. INT_PTR i = Find(pHotKey);
  279. if(i >= 0)
  280. {
  281. RemoveAt(i);
  282. return true;
  283. }
  284. return false;
  285. }
  286. bool CHotKeys::Remove(int clipId, CHotKey::HotKeyType hkType)
  287. {
  288. INT_PTR count = GetSize();
  289. for(int i=0; i < count; i++)
  290. {
  291. if(ElementAt(i) != NULL &&
  292. ElementAt(i)->m_clipId == clipId &&
  293. ElementAt(i)->m_hkType == hkType)
  294. {
  295. CHotKey *pKey = ElementAt(i);
  296. RemoveAt(i);
  297. delete pKey;
  298. return true;
  299. }
  300. }
  301. return false;
  302. }
  303. BOOL CHotKeys::ValidateClip(int clipId, DWORD key, CString desc, CHotKey::HotKeyType hkType)
  304. {
  305. CHotKey *pKey = NULL;
  306. INT_PTR count = GetSize();
  307. for(int i=0; i < count; i++)
  308. {
  309. if(ElementAt(i) != NULL &&
  310. ElementAt(i)->m_clipId == clipId &&
  311. ElementAt(i)->m_hkType == hkType)
  312. {
  313. pKey = ElementAt(i);
  314. break;
  315. }
  316. }
  317. if(pKey == NULL)
  318. {
  319. pKey = new CHotKey(desc, key, true, hkType);
  320. }
  321. BOOL ret = FALSE;
  322. if(pKey != NULL)
  323. {
  324. pKey->m_Key = key;
  325. pKey->m_Name = desc;
  326. pKey->m_clipId = clipId;
  327. ret = CHotKey::ValidateHotKey(key);
  328. }
  329. return ret;
  330. }
  331. void CHotKeys::LoadAllKeys()
  332. {
  333. INT_PTR count = GetSize();
  334. for(int i=0; i < count; i++)
  335. {
  336. ElementAt(i)->LoadKey();
  337. }
  338. }
  339. void CHotKeys::SaveAllKeys()
  340. {
  341. INT_PTR count = GetSize();
  342. for(int i=0; i < count; i++)
  343. {
  344. ElementAt(i)->SaveKey();
  345. }
  346. }
  347. void CHotKeys::RegisterAll(bool bMsgOnError)
  348. {
  349. CString str;
  350. CHotKey* pHotKey;
  351. INT_PTR count = GetSize();
  352. for(int i = 0; i < count; i++)
  353. {
  354. pHotKey = ElementAt(i);
  355. if(!pHotKey->Register() && pHotKey->m_Key > 0)
  356. {
  357. str = "Error Registering ";
  358. str += pHotKey->GetName();
  359. Log(str);
  360. if(bMsgOnError)
  361. AfxMessageBox(str);
  362. }
  363. }
  364. }
  365. void CHotKeys::UnregisterAll(bool bMsgOnError, bool bOnShowDitto)
  366. {
  367. CString str;
  368. CHotKey* pHotKey;
  369. INT_PTR count = GetSize();
  370. for(int i = 0; i < count; i++)
  371. {
  372. pHotKey = ElementAt(i);
  373. if(!pHotKey->Unregister(bOnShowDitto))
  374. {
  375. str = "Error Unregistering ";
  376. str += pHotKey->GetName();
  377. Log(str);
  378. if(bMsgOnError)
  379. AfxMessageBox(str);
  380. }
  381. }
  382. }
  383. void CHotKeys::GetKeys(ARRAY& keys)
  384. {
  385. INT_PTR count = GetSize();
  386. keys.SetSize(count);
  387. for(int i=0; i < count; i++)
  388. {
  389. keys[i] = ElementAt(i)->GetKey();
  390. }
  391. }
  392. // caution! this alters hotkeys based upon corresponding indexes
  393. void CHotKeys::SetKeys(ARRAY& keys, bool bSave)
  394. {
  395. INT_PTR count = GetSize();
  396. ASSERT(count == keys.GetSize());
  397. for(int i=0; i < count; i++)
  398. {
  399. ElementAt(i)->SetKey(keys[(INT)i], bSave);
  400. }
  401. }
  402. bool CHotKeys::FindFirstConflict(ARRAY& keys, INT_PTR* pX, INT_PTR* pY)
  403. {
  404. bool bConflict = false;
  405. INT_PTR i, j;
  406. INT_PTR count = keys.GetSize();
  407. DWORD key;
  408. for(i = 0; i < count && !bConflict; i++)
  409. {
  410. key = keys.ElementAt(i);
  411. // only check valid keys
  412. if(key == 0)
  413. continue;
  414. // scan the array for a duplicate
  415. for(j = i+1; j < count; j++ )
  416. {
  417. if(keys.ElementAt(j) == key)
  418. {
  419. bConflict = true;
  420. break;
  421. }
  422. }
  423. }
  424. if(bConflict)
  425. {
  426. if(pX)
  427. *pX = i-1;
  428. if(pY)
  429. *pY = j;
  430. }
  431. return bConflict;
  432. }
  433. // if true, pX and pY (if valid) are set to the indexes of the conflicting hotkeys.
  434. bool CHotKeys::FindFirstConflict(INT_PTR* pX, INT_PTR* pY)
  435. {
  436. ARRAY keys;
  437. GetKeys(keys);
  438. return FindFirstConflict(keys, pX, pY);
  439. }