Accels.cpp 3.0 KB

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