Accels.cpp 1.8 KB

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