HotKeys.cpp 8.5 KB

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