Accels.cpp 3.4 KB

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