Accels.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "stdafx.h"
  2. #include "Accels.h"
  3. #include "HotKeys.h"
  4. #include "Options.h"
  5. CAccels::CAccels()
  6. {
  7. m_handleRepeatKeys = false;
  8. m_firstMapTick = 0;
  9. m_activeFirstKey = 0;
  10. m_checkModifierKeys = true;
  11. }
  12. void CAccels::AddAccel(CAccel a)
  13. {
  14. m_multiMap.insert(pair<DWORD, CAccel>(a.Key, a));
  15. }
  16. void CAccels::AddAccel(DWORD cmd, DWORD key, DWORD key2, CString refData)
  17. {
  18. if ((int)key2 <= 0)
  19. {
  20. key2 = 0;
  21. }
  22. CAccel a(key, cmd, key2, refData);
  23. m_multiMap.insert(pair<DWORD, CAccel>(key, a));
  24. }
  25. void CAccels::RemoveAll()
  26. {
  27. m_multiMap.clear();
  28. }
  29. CString CAccels::GetCmdKeyText(DWORD cmd, CString refData)
  30. {
  31. CString cmdShortcutText;
  32. for (multimap<DWORD, CAccel>::iterator it = m_multiMap.begin(); it != m_multiMap.end(); ++it)
  33. {
  34. if (it->second.Cmd == cmd &&
  35. (refData == _T("") || it->second.RefData == refData))
  36. {
  37. if (it->second.Key != 0)
  38. {
  39. cmdShortcutText = CHotKey::GetHotKeyDisplayStatic(it->second.Key);
  40. if (it->second.Key2 != 0)
  41. {
  42. CString cmdShortcutText2 = CHotKey::GetHotKeyDisplayStatic(it->second.Key2);
  43. if (cmdShortcutText2.GetLength() > 0)
  44. {
  45. cmdShortcutText += _T(" - ");
  46. cmdShortcutText += cmdShortcutText2;
  47. }
  48. }
  49. }
  50. break;
  51. }
  52. }
  53. return cmdShortcutText;
  54. }
  55. bool CAccels::OnMsg(MSG *pMsg, CAccel &a)
  56. {
  57. if((pMsg->message != WM_KEYDOWN && pMsg->message != WM_SYSKEYDOWN))
  58. {
  59. return NULL;
  60. }
  61. // bit 30 (0x40000000) is 1 if this is NOT the first msg of the key
  62. // i.e. auto-repeat may cause multiple msgs of the same key
  63. if((pMsg->lParam &0x40000000) && m_handleRepeatKeys == false)
  64. {
  65. return NULL;
  66. }
  67. m_handleRepeatKeys = false;
  68. if(!pMsg)
  69. {
  70. return NULL;
  71. }
  72. BYTE vkey = LOBYTE(pMsg->wParam);
  73. BYTE mod = 0;
  74. if (m_checkModifierKeys)
  75. {
  76. mod = GetKeyStateModifiers();
  77. }
  78. DWORD key = ACCEL_MAKEKEY(vkey, mod);
  79. //CString cs;
  80. //cs.Format(_T("Key: %d, Mod: %d, vkey: %d, diff: %d\r\n"), key, mod, vkey, (GetTickCount() - m_firstMapTick));
  81. //OutputDebugString(cs);
  82. if (m_firstMapTick != 0 &&
  83. (GetTickCount() - m_firstMapTick) < CGetSetOptions::m_doubleKeyStrokeTimeout)
  84. {
  85. pair<multimap<DWORD, CAccel>::iterator, multimap<DWORD, CAccel>::iterator> ppp;
  86. ppp = m_multiMap.equal_range(m_activeFirstKey);
  87. for (multimap<DWORD, CAccel>::iterator it2 = ppp.first; it2 != ppp.second; ++it2)
  88. {
  89. if (key == it2->second.Key2)
  90. {
  91. a = (*it2).second;
  92. m_firstMapTick = 0;
  93. m_activeFirstKey = 0;
  94. return true;
  95. }
  96. }
  97. }
  98. else
  99. {
  100. m_firstMapTick = 0;
  101. m_activeFirstKey = 0;
  102. pair<multimap<DWORD, CAccel>::iterator, multimap<DWORD, CAccel>::iterator> ppp;
  103. ppp = m_multiMap.equal_range(key);
  104. for (multimap<DWORD, CAccel>::iterator it2 = ppp.first; it2 != ppp.second; ++it2)
  105. {
  106. if (it2->second.Key2 == 0)
  107. {
  108. a = (*it2).second;
  109. //return now as a another command could have a second key defined
  110. //if they don't press the second key this will be handled by a timer on the outside
  111. }
  112. else
  113. {
  114. m_activeFirstKey = key;
  115. m_firstMapTick = GetTickCount();
  116. break;
  117. }
  118. }
  119. if (a.Cmd > 0 && m_activeFirstKey == 0)
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. bool CAccels::ContainsKey(int vKey)
  127. {
  128. CString cmdShortcutText;
  129. for (multimap<DWORD, CAccel>::iterator it = m_multiMap.begin(); it != m_multiMap.end(); ++it)
  130. {
  131. if (LOBYTE(it->second.Key) == vKey || LOBYTE(it->second.Key2) == vKey)
  132. {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. BYTE CAccels::GetKeyStateModifiers()
  139. {
  140. BYTE m = 0;
  141. if(GetKeyState(VK_SHIFT) &0x8000)
  142. {
  143. m |= HOTKEYF_SHIFT;
  144. }
  145. if(GetKeyState(VK_CONTROL) &0x8000)
  146. {
  147. m |= HOTKEYF_CONTROL;
  148. }
  149. if(GetKeyState(VK_MENU) &0x8000)
  150. {
  151. m |= HOTKEYF_ALT;
  152. }
  153. if(GetKeyState(VK_LWIN) &0x8000)
  154. {
  155. m |= HOTKEYF_EXT;
  156. }
  157. if(GetKeyState(VK_RWIN) &0x8000)
  158. {
  159. m |= HOTKEYF_EXT;
  160. }
  161. return m;
  162. }