soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SAccelerator.cpp
1#include "souistd.h"
2#include "core/SAccelerator.h"
3
4SNSBEGIN
5
6SAccelerator::SAccelerator(WORD vKey, bool bCtrl, bool bAlt, bool bShift)
7 : m_wVK(vKey)
8 , m_wModifier(0)
9{
10 if (bCtrl)
11 m_wModifier |= Mod_Ctrl;
12 if (bAlt)
13 m_wModifier |= Mod_Alt;
14 if (bShift)
15 m_wModifier |= Mod_Shift;
16}
17
19 : m_wVK(LOWORD(dwAccel))
20 , m_wModifier(HIWORD(dwAccel))
21{
22}
23
27
29{
30 return FormatAccelKey(MAKELONG(m_wVK, m_wModifier));
31}
32
33WORD SAccelerator::VkFromString(LPCTSTR pszKey)
34{
35 WORD wKey = 0;
36 if (_tcscmp(pszKey, _T("esc")) == 0)
37 wKey = VK_ESCAPE;
38 else if (_tcscmp(pszKey, _T("enter")) == 0)
39 wKey = VK_RETURN;
40 else if (_tcscmp(pszKey, _T("up")) == 0)
41 wKey = VK_UP;
42 else if (_tcscmp(pszKey, _T("down")) == 0)
43 wKey = VK_DOWN;
44 else if (_tcscmp(pszKey, _T("left")) == 0)
45 wKey = VK_LEFT;
46 else if (_tcscmp(pszKey, _T("right")) == 0)
47 wKey = VK_RIGHT;
48 else if (_tcscmp(pszKey, _T("home")) == 0)
49 wKey = VK_HOME;
50 else if (_tcscmp(pszKey, _T("pageup")) == 0)
51 wKey = VK_PRIOR;
52 else if (_tcscmp(pszKey, _T("pagedown")) == 0)
53 wKey = VK_NEXT;
54 else if (_tcscmp(pszKey, _T("insert")) == 0)
55 wKey = VK_INSERT;
56 else if (_tcscmp(pszKey, _T("space")) == 0)
57 wKey = VK_SPACE;
58 else if (_tcscmp(pszKey, _T("delete")) == 0)
59 wKey = VK_DELETE;
60 else if (_tcscmp(pszKey, _T("print")) == 0)
61 wKey = VK_PRINT;
62 else if (_tcslen(pszKey) > 1)
63 {
64 if (pszKey[0] == _T('f'))
65 { // F1-F12
66 wKey = VK_F1 + _ttoi(pszKey + 1) - 1;
67 }
68 else if (_tcsnicmp(pszKey, _T("num "), 4) == 0)
69 { // Num 0 - Num 9 || Num Del
70 if (_tcscmp(pszKey + 4, _T("del")) == 0)
71 wKey = VK_DECIMAL;
72 else if (_tcscmp(pszKey + 4, _T("*")) == 0)
73 wKey = VK_MULTIPLY;
74 else if (_tcscmp(pszKey + 4, _T("+")) == 0)
75 wKey = VK_ADD;
76 else
77 wKey = VK_NUMPAD0 + _ttoi(pszKey + 4);
78 }
79 }
80 else // if( _tcslen(pszKey)==1)
81 {
82 if (pszKey[0] >= _T('a') && pszKey[0] <= _T('z')) // a-z
83 wKey = pszKey[0] - 0x20;
84 else if (pszKey[0] >= 'A' && pszKey[0] <= 'Z')
85 wKey = pszKey[0];
86 else if (pszKey[0] >= _T('0') && pszKey[0] <= _T('9'))
87 wKey = pszKey[0];
88 else if (pszKey[0] == _T('-'))
89 wKey = VK_OEM_MINUS;
90 else if (pszKey[0] == _T('='))
91 wKey = VK_OEM_PLUS;
92 else if (pszKey[0] == _T(','))
93 wKey = VK_OEM_COMMA;
94 else if (pszKey[0] == _T('.'))
95 wKey = VK_OEM_PERIOD;
96 else if (pszKey[0] == _T(';'))
97 wKey = VK_OEM_1;
98 else if (pszKey[0] == _T('/'))
99 wKey = VK_OEM_2;
100 else if (pszKey[0] == _T('`'))
101 wKey = VK_OEM_3;
102 else if (pszKey[0] == _T('['))
103 wKey = VK_OEM_4;
104 else if (pszKey[0] == _T('\\'))
105 wKey = VK_OEM_5;
106 else if (pszKey[0] == _T(']'))
107 wKey = VK_OEM_6;
108 else if (pszKey[0] == _T('\''))
109 wKey = VK_OEM_7;
110 }
111 return wKey;
112}
113
115{
116 SStringT str;
117 switch (vk)
118 {
119 case VK_ESCAPE:
120 str = _T("ESC");
121 break;
122 case VK_RETURN:
123 str = _T("Enter");
124 break;
125 case VK_UP:
126 str = _T("Up");
127 break;
128 case VK_DOWN:
129 str = _T("Down");
130 break;
131 case VK_LEFT:
132 str = _T("Left");
133 break;
134 case VK_RIGHT:
135 str = _T("Right");
136 break;
137 case VK_HOME:
138 str = _T("Home");
139 break;
140 case VK_END:
141 str = _T("End");
142 break;
143 case VK_PRIOR:
144 str = _T("PageUp");
145 break;
146 case VK_NEXT:
147 str = _T("PageDown");
148 break;
149 case VK_INSERT:
150 str = _T("Insert");
151 break;
152 case VK_SPACE:
153 str = _T("Space");
154 break;
155 case VK_DELETE:
156 str = _T("Delete");
157 break;
158 case VK_PRINT:
159 str = _T("Print");
160 break;
161 default:
162 if ((vk >= '0' && vk <= '9') || (vk >= 'A' && vk <= 'Z'))
163 str = (TCHAR)vk;
164 else if (vk >= VK_NUMPAD0 && vk <= VK_NUMPAD9)
165 str.Format(_T("Num %d"), vk - VK_NUMPAD0);
166 else if (vk == VK_MULTIPLY)
167 str = _T("Num *");
168 else if (vk == VK_ADD)
169 str = _T("Num +");
170 else if (vk == VK_DECIMAL)
171 str = _T("Num Del");
172 else if (vk >= VK_F1 && vk <= VK_F12)
173 str.Format(_T("F%d"), vk - VK_F1 + 1);
174 else
175 {
176 TCHAR c = MapVirtualKey(vk, 2);
177 switch (c)
178 {
179 case '-':
180 case '=':
181 case '[':
182 case ']':
183 case '\\':
184 case ';':
185 case '\'':
186 case ',':
187 case '.':
188 case '/':
189 case '`':
190 str += TCHAR(c);
191 break;
192 }
193 }
194 break;
195 }
196 return str;
197}
198
199SStringT SAccelerator::FormatAccelKey(DWORD dwAccel)
200{
201 WORD wModifier = HIWORD(dwAccel);
202 WORD wVk = LOWORD(dwAccel);
203
204 if (wModifier == 0 && wVk == 0)
205 return _T("无");
206 SStringT str;
207 if (wModifier & Mod_Ctrl)
208 str = _T("Ctrl+");
209 if (wModifier & Mod_Shift)
210 str += _T("Shift+");
211 if (wModifier & Mod_Alt)
212 str += _T("Alt+");
213
214 str += GetKeyName(wVk);
215 return str;
216}
217
218//将字符串翻译为加速键
219DWORD SAccelerator::TranslateAccelKey(LPCTSTR pszAccelKey)
220{
221 TCHAR szBuf[101] = { 0 }; //保证字符串结束有两个结束符
222 WORD wModifier = Mod_None;
223 WORD wKey = 0;
224 int nKeyLen = (int)_tcslen(pszAccelKey);
225 if (nKeyLen >= 100)
226 return 0;
227 _tcscpy(szBuf, pszAccelKey);
228 CharLowerBuff(szBuf, nKeyLen);
229
230 LPTSTR pszBuf = szBuf;
231 LPTSTR pszKey = _tcstok(pszBuf, _T("+"));
232 while (pszKey)
233 {
234 if (_tcscmp(pszKey, _T("ctrl")) == 0)
235 {
236 wModifier |= Mod_Ctrl;
237 }
238 else if (_tcscmp(pszKey, _T("alt")) == 0)
239 {
240 wModifier |= Mod_Alt;
241 }
242 else if (_tcscmp(pszKey, _T("shift")) == 0)
243 {
244 wModifier |= Mod_Shift;
245 }
246 else
247 {
248 wKey = VkFromString(pszKey);
249 break;
250 }
251 pszBuf += _tcslen(pszKey) + 1;
252 pszKey = _tcstok(pszBuf, _T("+"));
253 }
254 return MAKELONG(wKey, wModifier);
255}
256
258{
259 return m_wModifier;
260}
261
263{
264 return m_wVK;
265}
266
268{
269 return MAKELONG(m_wVK, m_wModifier);
270}
271
272SNSEND
Accelerator management module.
WORD GetModifier() SCONST OVERRIDE
Gets the modifier keys.
static SStringT FormatAccelKey(DWORD dwAccel)
Formats an accelerator key as a string.
SAccelerator(DWORD dwAccel)
Constructor.
~SAccelerator(void)
Destructor.
static DWORD TranslateAccelKey(LPCTSTR pszKeyName)
Translates a string to an accelerator key value.
DWORD GetAcc() SCONST OVERRIDE
Gets the accelerator key value.
static WORD VkFromString(LPCTSTR pszKey)
Converts a string to a virtual key code.
static SStringT GetKeyName(WORD vk)
Converts a virtual key code to its string representation.
SStringT FormatHotkey()
Formats the accelerator key as a string.
WORD GetKey() SCONST OVERRIDE
Gets the key value.