HotKeys.cpp 7.6 KB

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