Accels.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "stdafx.h"
  2. #include "Accels.h"
  3. #include "HotKeys.h"
  4. CAccels::CAccels(){
  5. }
  6. void CAccels::AddAccel(CAccel a)
  7. {
  8. m_Map.SetAt(a.Key, a);
  9. }
  10. void CAccels::AddAccel(DWORD cmd, DWORD key)
  11. {
  12. CAccel a;
  13. a.Cmd = cmd;
  14. a.Key = key;
  15. m_Map.SetAt(key, a);
  16. }
  17. void CAccels::RemoveAll()
  18. {
  19. m_Map.RemoveAll();
  20. }
  21. CString CAccels::GetCmdKeyText(DWORD cmd)
  22. {
  23. CString cmdShortcutText = _T("");
  24. POSITION pos = m_Map.GetStartPosition();
  25. DWORD mapShortcut;
  26. CAccel a;
  27. while (pos != NULL)
  28. {
  29. m_Map.GetNextAssoc(pos, mapShortcut, a);
  30. if(a.Cmd == cmd)
  31. {
  32. cmdShortcutText = CHotKey::GetHotKeyDisplayStatic(mapShortcut);
  33. break;
  34. }
  35. }
  36. return cmdShortcutText;
  37. }
  38. bool CAccels::OnMsg(MSG *pMsg, CAccel &a)
  39. {
  40. // bit 30 (0x40000000) is 1 if this is NOT the first msg of the key
  41. // i.e. auto-repeat may cause multiple msgs of the same key
  42. if((pMsg->lParam &0x40000000) || (pMsg->message != WM_KEYDOWN && pMsg->message != WM_SYSKEYDOWN))
  43. {
  44. return NULL;
  45. }
  46. if(!pMsg || m_Map.GetCount() <= 0)
  47. {
  48. return NULL;
  49. }
  50. BYTE vkey = LOBYTE(pMsg->wParam);
  51. BYTE mod = GetKeyStateModifiers();
  52. DWORD key = ACCEL_MAKEKEY(vkey, mod);
  53. CString cs;
  54. cs.Format(_T("Key: %d, Mod: %d, vkey: %d"), key, mod, vkey);
  55. OutputDebugString(cs);
  56. if(m_Map.Lookup(key, a))
  57. {
  58. return true;
  59. }
  60. return false;
  61. }
  62. BYTE CAccels::GetKeyStateModifiers()
  63. {
  64. BYTE m = 0;
  65. if(GetKeyState(VK_SHIFT) &0x8000)
  66. {
  67. m |= HOTKEYF_SHIFT;
  68. }
  69. if(GetKeyState(VK_CONTROL) &0x8000)
  70. {
  71. m |= HOTKEYF_CONTROL;
  72. }
  73. if(GetKeyState(VK_MENU) &0x8000)
  74. {
  75. m |= HOTKEYF_ALT;
  76. }
  77. if(GetKeyState(VK_LWIN) &0x8000)
  78. {
  79. m |= HOTKEYF_EXT;
  80. }
  81. if(GetKeyState(VK_RWIN) &0x8000)
  82. {
  83. m |= HOTKEYF_EXT;
  84. }
  85. return m;
  86. }