Accels.cpp 2.7 KB

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