Accels.cpp 3.5 KB

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