HotKeys.cpp 8.0 KB

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