Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.2 KiB

  1. // AccelContainer.cpp: implementation of the CAccelContainer class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "AccelContainer.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. typedef struct _VKEYS {
  10. LPCTSTR pKeyName;
  11. WORD virtKey;
  12. } VKEYS;
  13. VKEYS vkeys[] = {
  14. TEXT("BkSp"), VK_BACK,
  15. TEXT("PgUp"), VK_PRIOR,
  16. TEXT("PgDn"), VK_NEXT,
  17. TEXT("End"), VK_END,
  18. TEXT("Home"), VK_HOME,
  19. TEXT("Left"), VK_LEFT,
  20. TEXT("Up"), VK_UP,
  21. TEXT("Right"),VK_RIGHT,
  22. TEXT("Down"), VK_DOWN,
  23. TEXT("Ins"), VK_INSERT,
  24. TEXT("Del"), VK_DELETE,
  25. TEXT("Mult"), VK_MULTIPLY,
  26. TEXT("Add"), VK_ADD,
  27. TEXT("Sub"), VK_SUBTRACT,
  28. TEXT("DecPt"),VK_DECIMAL,
  29. TEXT("Div"), VK_DIVIDE,
  30. TEXT("F2"), VK_F2,
  31. TEXT("F3"), VK_F3,
  32. TEXT("F5"), VK_F5,
  33. TEXT("F6"), VK_F6,
  34. TEXT("F7"), VK_F7,
  35. TEXT("F8"), VK_F8,
  36. TEXT("F9"), VK_F9,
  37. TEXT("F10"), VK_F10,
  38. TEXT("F11"), VK_F11,
  39. TEXT("F12"), VK_F12
  40. };
  41. wstring CAccelContainer::GetKeyFromAccel(const ACCEL& accel)
  42. {
  43. int i;
  44. wstring strAccel((LPCTSTR)&accel.key, 1);
  45. for (i = 0; i < sizeof(vkeys)/sizeof(vkeys[0]); ++i) {
  46. if (vkeys[i].virtKey == accel.key) {
  47. strAccel = vkeys[i].pKeyName;
  48. break;
  49. }
  50. }
  51. return strAccel;
  52. }
  53. BOOL CAccelContainer::IsAccelKey(LPMSG pMsg, WORD* pCmd)
  54. {
  55. ACCELVECTOR::iterator iter;
  56. WORD fVirtKey = 0;
  57. //
  58. //
  59. if (NULL == pMsg) {
  60. fVirtKey |= (GetKeyState(VK_MENU) & 0x8000) ? FALT : 0;
  61. fVirtKey |= (GetKeyState(VK_CONTROL) & 0x8000) ? FCONTROL : 0;
  62. fVirtKey |= (GetKeyState(VK_SHIFT) & 0x8000) ? FSHIFT : 0;
  63. fVirtKey |= FVIRTKEY;
  64. for (iter = m_Accel.begin(); iter != m_Accel.end(); ++iter) {
  65. const ACCEL& accl = *iter;
  66. if (GetKeyState(accl.key) & 0x8000) {
  67. //
  68. // pressed! see if we have a match
  69. //
  70. if (fVirtKey == accl.fVirt) {
  71. //
  72. // we do have a match
  73. //
  74. if (pCmd) {
  75. *pCmd = accl.cmd;
  76. }
  77. return TRUE;
  78. }
  79. }
  80. }
  81. } else {
  82. // one of the nasty messages ?
  83. if (pMsg->message == WM_SYSKEYDOWN || pMsg->message == WM_SYSKEYUP) {
  84. fVirtKey = (pMsg->lParam & 0x20000000) ? FALT : 0;
  85. fVirtKey |= FVIRTKEY; // always a virtkey code
  86. for (iter = m_Accel.begin(); iter != m_Accel.end(); ++iter) {
  87. const ACCEL& accl = *iter;
  88. if (pMsg->wParam == accl.key) {
  89. //
  90. // pressed! see if we have a match
  91. //
  92. if (fVirtKey == accl.fVirt) {
  93. //
  94. // we do have a match
  95. //
  96. if (pCmd) {
  97. *pCmd = accl.cmd;
  98. }
  99. return TRUE;
  100. }
  101. }
  102. }
  103. }
  104. }
  105. return FALSE;
  106. }
  107. VOID CAccelContainer::ParseAccelString(LPCTSTR lpszStr, WORD wCmd)
  108. {
  109. LPCTSTR pch = lpszStr;
  110. LPCTSTR pchEnd, pchb;
  111. ACCEL accl;
  112. //
  113. // nuke all
  114. //
  115. while (*pch) {
  116. //
  117. // skip whitespace
  118. //
  119. pch += _tcsspn(pch, TEXT(" \t"));
  120. //
  121. // see what kind of key this is
  122. //
  123. if (*pch == TEXT('{')) {
  124. // some special key
  125. ++pch;
  126. pch += _tcsspn(pch, TEXT(" \t"));
  127. int i;
  128. for (i = 0; i < sizeof(vkeys)/sizeof(vkeys[0]); ++i) {
  129. int nLen = _tcslen(vkeys[i].pKeyName);
  130. if (!_tcsnicmp(pch, vkeys[i].pKeyName, nLen)) {
  131. // aha -- we have a match
  132. //
  133. accl.cmd = wCmd;
  134. accl.fVirt = FALT|FVIRTKEY;
  135. accl.key = vkeys[i].virtKey;
  136. m_Accel.push_back(accl);
  137. pch += nLen;
  138. pch += _tcsspn(pch, TEXT(" \t"));
  139. break;
  140. }
  141. }
  142. pchEnd = _tcschr(pch, '}');
  143. pchb = _tcschr(pch, '{');
  144. if (pchEnd != NULL && (pchb == NULL || pchEnd < pchb)) {
  145. pch = pchEnd + 1; // one past the bracket
  146. }
  147. // what if we have not succeeded - and no closing bracket ?
  148. // we skip the bracket and go ahead as character
  149. } else if (_istalnum(*pch)) { // normal key
  150. TCHAR ch = _totupper(*pch);
  151. accl.cmd = wCmd;
  152. accl.fVirt = FALT|FVIRTKEY;
  153. accl.key = ch;
  154. m_Accel.push_back(accl);
  155. ++pch;
  156. } else {
  157. ++pch; // skip the char, we can't recognize it
  158. }
  159. }
  160. }