HotKeys.cpp 7.4 KB

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