HotKeys.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. ctrl.SetHotKey(LOBYTE(m_Key), (WORD)lModifiers);
  150. if(lModifiers & HOTKEYF_EXT)
  151. {
  152. ::CheckDlgButton(hParent, nWindowsCBID, BST_CHECKED);
  153. }
  154. }
  155. bool CHotKey::Register()
  156. {
  157. if(m_Key)
  158. {
  159. if(m_bIsRegistered == false)
  160. {
  161. ASSERT(g_HotKeys.m_hWnd);
  162. m_bIsRegistered = ::RegisterHotKey(g_HotKeys.m_hWnd,
  163. m_Atom,
  164. GetModifier(),
  165. LOBYTE(m_Key) ) == TRUE;
  166. }
  167. }
  168. else
  169. {
  170. m_bIsRegistered = false;
  171. }
  172. return m_bIsRegistered;
  173. }
  174. bool CHotKey::Unregister(bool bOnShowingDitto)
  175. {
  176. if(!m_bIsRegistered)
  177. return true;
  178. if(bOnShowingDitto)
  179. {
  180. if(m_bUnRegisterOnShowDitto == false)
  181. return true;
  182. }
  183. if(m_Key)
  184. {
  185. ASSERT(g_HotKeys.m_hWnd);
  186. if(::UnregisterHotKey( g_HotKeys.m_hWnd, m_Atom))
  187. {
  188. m_bIsRegistered = false;
  189. return true;
  190. }
  191. else
  192. {
  193. Log(_T("Unregister FAILED!"));
  194. ASSERT(0);
  195. }
  196. }
  197. else
  198. {
  199. m_bIsRegistered = false;
  200. return true;
  201. }
  202. return false;
  203. }
  204. CHotKeys::CHotKeys() : m_hWnd(NULL)
  205. {
  206. }
  207. CHotKeys::~CHotKeys()
  208. {
  209. CHotKey* pHotKey;
  210. INT_PTR count = GetSize();
  211. for(int i=0; i < count; i++)
  212. {
  213. pHotKey = ElementAt(i);
  214. if(pHotKey)
  215. {
  216. delete pHotKey;
  217. }
  218. }
  219. }
  220. INT_PTR CHotKeys::Find(CHotKey* pHotKey)
  221. {
  222. INT_PTR count = GetSize();
  223. for(int i=0; i < count; i++)
  224. {
  225. if(pHotKey == ElementAt(i))
  226. {
  227. return i;
  228. }
  229. }
  230. return -1;
  231. }
  232. bool CHotKeys::Remove(CHotKey* pHotKey)
  233. {
  234. INT_PTR i = Find(pHotKey);
  235. if(i >= 0)
  236. {
  237. RemoveAt(i);
  238. return true;
  239. }
  240. return false;
  241. }
  242. bool CHotKeys::Remove(int clipId)
  243. {
  244. INT_PTR count = GetSize();
  245. for(int i=0; i < count; i++)
  246. {
  247. if(ElementAt(i) != NULL && ElementAt(i)->m_clipId == clipId)
  248. {
  249. CHotKey *pKey = ElementAt(i);
  250. RemoveAt(i);
  251. delete pKey;
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. BOOL CHotKeys::ValidateClip(int clipId, DWORD key, CString desc)
  258. {
  259. CHotKey *pKey = NULL;
  260. INT_PTR count = GetSize();
  261. for(int i=0; i < count; i++)
  262. {
  263. if(ElementAt(i) != NULL && ElementAt(i)->m_clipId == clipId)
  264. {
  265. pKey = ElementAt(i);
  266. break;
  267. }
  268. }
  269. if(pKey == NULL)
  270. {
  271. pKey = new CHotKey(desc, key, true);
  272. }
  273. BOOL ret = FALSE;
  274. if(pKey != NULL)
  275. {
  276. pKey->m_Key = key;
  277. pKey->m_Name = desc;
  278. pKey->m_clipId = clipId;
  279. ret = CHotKey::ValidateHotKey(key);
  280. }
  281. return ret;
  282. }
  283. void CHotKeys::LoadAllKeys()
  284. {
  285. INT_PTR count = GetSize();
  286. for(int i=0; i < count; i++)
  287. {
  288. ElementAt(i)->LoadKey();
  289. }
  290. }
  291. void CHotKeys::SaveAllKeys()
  292. {
  293. INT_PTR count = GetSize();
  294. for(int i=0; i < count; i++)
  295. {
  296. ElementAt(i)->SaveKey();
  297. }
  298. }
  299. void CHotKeys::RegisterAll(bool bMsgOnError)
  300. {
  301. CString str;
  302. CHotKey* pHotKey;
  303. INT_PTR count = GetSize();
  304. for(int i = 0; i < count; i++)
  305. {
  306. pHotKey = ElementAt(i);
  307. if(!pHotKey->Register() && pHotKey->m_Key > 0)
  308. {
  309. str = "Error Registering ";
  310. str += pHotKey->GetName();
  311. Log(str);
  312. if(bMsgOnError)
  313. AfxMessageBox(str);
  314. }
  315. }
  316. }
  317. void CHotKeys::UnregisterAll(bool bMsgOnError, bool bOnShowDitto)
  318. {
  319. CString str;
  320. CHotKey* pHotKey;
  321. INT_PTR count = GetSize();
  322. for(int i = 0; i < count; i++)
  323. {
  324. pHotKey = ElementAt(i);
  325. if(!pHotKey->Unregister(bOnShowDitto))
  326. {
  327. str = "Error Unregistering ";
  328. str += pHotKey->GetName();
  329. Log(str);
  330. if(bMsgOnError)
  331. AfxMessageBox(str);
  332. }
  333. }
  334. }
  335. void CHotKeys::GetKeys(ARRAY& keys)
  336. {
  337. INT_PTR count = GetSize();
  338. keys.SetSize(count);
  339. for(int i=0; i < count; i++)
  340. {
  341. keys[i] = ElementAt(i)->GetKey();
  342. }
  343. }
  344. // caution! this alters hotkeys based upon corresponding indexes
  345. void CHotKeys::SetKeys(ARRAY& keys, bool bSave)
  346. {
  347. INT_PTR count = GetSize();
  348. ASSERT(count == keys.GetSize());
  349. for(int i=0; i < count; i++)
  350. {
  351. ElementAt(i)->SetKey(keys[(INT)i], bSave);
  352. }
  353. }
  354. bool CHotKeys::FindFirstConflict(ARRAY& keys, INT_PTR* pX, INT_PTR* pY)
  355. {
  356. bool bConflict = false;
  357. INT_PTR i, j;
  358. INT_PTR count = keys.GetSize();
  359. DWORD key;
  360. for(i = 0; i < count && !bConflict; i++)
  361. {
  362. key = keys.ElementAt(i);
  363. // only check valid keys
  364. if(key == 0)
  365. continue;
  366. // scan the array for a duplicate
  367. for(j = i+1; j < count; j++ )
  368. {
  369. if(keys.ElementAt(j) == key)
  370. {
  371. bConflict = true;
  372. break;
  373. }
  374. }
  375. }
  376. if(bConflict)
  377. {
  378. if(pX)
  379. *pX = i-1;
  380. if(pY)
  381. *pY = j;
  382. }
  383. return bConflict;
  384. }
  385. // if true, pX and pY (if valid) are set to the indexes of the conflicting hotkeys.
  386. bool CHotKeys::FindFirstConflict(INT_PTR* pX, INT_PTR* pY)
  387. {
  388. ARRAY keys;
  389. GetKeys(keys);
  390. return FindFirstConflict(keys, pX, pY);
  391. }