view src/win32/KeyboardEdit.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 ////////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 1998 by Thierry Maurel
3 // All rights reserved
4 //
5 // Distribute freely, except: don't remove my name from the source or
6 // documentation (don't take credit for my work), mark your changes (don't
7 // get me blamed for your possible bugs), don't alter or remove this
8 // notice.
9 // No warrantee of any kind, express or implied, is included with this
10 // software; use at your own risk, responsibility for damages (if any) to
11 // anyone resulting from the use of this software rests entirely with the
12 // user.
13 //
14 // Send bug reports, bug fixes, enhancements, requests, flames, etc., and
15 // I'll try to keep a version up to date. I can be reached as follows:
16 // tmaurel@caramail.com (or tmaurel@hol.fr)
17 //
18 ////////////////////////////////////////////////////////////////////////////////
19 // File : KeyboardEdit.cpp
20 // Project : AccelsEditor
21 ////////////////////////////////////////////////////////////////////////////////
22 // Version : 1.0 * Authors : A.Lebatard + T.Maurel
23 // Date : 17.08.98
24 //
25 // Remarks : implementation file
26 //
28 ////////////////////////////////////////////////////////////////////////////////
29 // modified by aquanull
31 #include "stdafx.h"
32 #include "KeyboardEdit.h"
34 extern TCHAR *mapVirtKeysStringFromWORD(WORD wKey);
36 IMPLEMENT_DYNAMIC(CKeyboardEdit, CEdit)
38 /////////////////////////////////////////////////////////////////////////////
39 // CKeyboardEdit
41 CKeyboardEdit::CKeyboardEdit()
42 {
43 ResetKey();
44 }
46 CKeyboardEdit::~CKeyboardEdit()
47 {}
49 BEGIN_MESSAGE_MAP(CKeyboardEdit, CEdit)
50 //{{AFX_MSG_MAP(CKeyboardEdit)
51 ON_CONTROL_REFLECT_EX(EN_CHANGE, &CKeyboardEdit::OnEnChange)
52 ON_CONTROL_REFLECT_EX(EN_SETFOCUS, &CKeyboardEdit::OnEnSetfocus)
53 ON_CONTROL_REFLECT_EX(EN_KILLFOCUS, &CKeyboardEdit::OnEnKillfocus)
54 //}}AFX_MSG_MAP
55 END_MESSAGE_MAP()
57 /////////////////////////////////////////////////////////////////////////////
58 // CKeyboardEdit message handlers
59 BOOL CKeyboardEdit::PreTranslateMessage(MSG *pMsg)
60 {
61 if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN)
62 {
63 int key = pMsg->wParam;
64 m_keys[key] = true;
65 switch (key)
66 {
67 case VK_CONTROL:
68 case VK_MENU:
69 case VK_SHIFT:
70 m_bCtrlPressed = m_keys[VK_CONTROL];
71 m_bAltPressed = m_keys[VK_MENU];;
72 m_bShiftPressed = m_keys[VK_SHIFT];
73 if (!m_keys[m_wVirtKey])
74 {
75 if (m_keys[m_wJamKey])
76 {
77 m_wVirtKey = m_wJamKey;
78 m_wJamKey = 0;
79 }
80 else
81 m_wVirtKey = 0;
82 }
83 if (!m_keys[m_wJamKey])
84 m_wJamKey = 0;
85 break;
86 default:
87 m_bCtrlPressed = m_keys[VK_CONTROL];
88 m_bAltPressed = m_keys[VK_MENU];
89 m_bShiftPressed = m_keys[VK_SHIFT];
90 if (m_wVirtKey != key)
91 {
92 if (m_keys[m_wVirtKey])
93 m_wJamKey = m_wVirtKey;
94 else
95 m_wJamKey = 0;
96 m_wVirtKey = key;
97 }
98 else if (!m_keys[m_wJamKey])
99 m_wJamKey = 0;
100 break;
101 }
102 DisplayKeyboardString();
103 return TRUE;
104 }
105 else if (pMsg->message == WM_KEYUP || pMsg->message == WM_SYSKEYUP)
106 {
107 int key = pMsg->wParam;
108 m_keys[key] = false;
109 switch (key)
110 {
111 case VK_CONTROL:
112 case VK_MENU:
113 case VK_SHIFT:
114 break;
115 default:
116 m_bCtrlPressed = m_keys[VK_CONTROL];
117 m_bAltPressed = m_keys[VK_MENU];
118 m_bShiftPressed = m_keys[VK_SHIFT];
119 if (m_wJamKey)
120 {
121 if (!m_keys[m_wVirtKey])
122 {
123 m_wVirtKey = m_wJamKey;
124 m_wJamKey = 0;
125 }
126 if (!m_keys[m_wJamKey])
127 m_wJamKey = 0;
128 }
129 break;
130 }
131 DisplayKeyboardString();
132 return TRUE;
133 }
135 return CEdit::PreTranslateMessage(pMsg);
136 }
138 BOOL CKeyboardEdit::OnEnChange()
139 {
140 return FALSE;
141 }
143 BOOL CKeyboardEdit::OnEnSetfocus()
144 {
145 //SetSel(0, -1, TRUE); // mouse click makes this in vain, so we use the method below instead
146 PostMessage(EM_SETSEL, 0, -1);
147 m_bForceUpdate = true;
148 return FALSE;
149 }
151 BOOL CKeyboardEdit::OnEnKillfocus()
152 {
153 AllKeyUp();
154 return FALSE;
155 }
157 ////////////////////////////////////////////////////////////////////////
158 //
159 void CKeyboardEdit::DisplayKeyboardString()
160 {
161 CString strKbd;
163 // modifiers
164 if (m_bCtrlPressed)
165 strKbd = "Ctrl";
167 if (m_bAltPressed)
168 {
169 if (strKbd.GetLength() > 0)
170 strKbd += '+';
171 strKbd += "Alt";
172 }
173 if (m_bShiftPressed)
174 {
175 if (strKbd.GetLength() > 0)
176 strKbd += '+';
177 strKbd += "Shift";
178 }
179 // virtual key
180 LPCTSTR szVirtKey = mapVirtKeysStringFromWORD(m_wVirtKey);
181 if (szVirtKey != NULL)
182 {
183 if (strKbd.GetLength() > 0)
184 strKbd += '+';
185 strKbd += szVirtKey;
186 }
187 // jammed key
188 LPCTSTR szJamKey = mapVirtKeysStringFromWORD(m_wJamKey);
189 if (szJamKey != NULL)
190 {
191 strKbd += '(';
192 strKbd += szJamKey;
193 strKbd += ')';
194 }
196 if (m_bForceUpdate)
197 {
198 m_bForceUpdate = false;
199 SetWindowText(strKbd);
200 }
201 else
202 {
203 CString oldString;
204 GetWindowText(oldString);
205 if (oldString.Compare(strKbd))
206 SetWindowText(strKbd);
207 }
208 }
210 ////////////////////////////////////////////////////////////////////////
211 //
212 void CKeyboardEdit::ResetKey()
213 {
214 AllKeyUp();
216 m_bForceUpdate = true;
217 m_bCtrlPressed = false;
218 m_bAltPressed = false;
219 m_bShiftPressed = false;
220 m_wVirtKey = 0;
221 m_wJamKey = 0;
223 if (m_hWnd != NULL)
224 {
225 CString oldString;
226 GetWindowText(oldString);
227 if (!oldString.IsEmpty())
228 SetWindowText(_T(""));
229 }
230 }
232 void CKeyboardEdit::AllKeyUp()
233 {
234 for (int i = 0; i < 256; ++i)
235 m_keys[i] = 0;
236 }
238 ////////////////////////////////////////////////////////////////////////
239 //
240 bool CKeyboardEdit::GetAccelKey(WORD &wVirtKey, bool &bCtrl, bool &bAlt, bool &bShift) const
241 {
242 if (!IsDefined())
243 return false;
245 wVirtKey = m_wVirtKey;
246 bAlt = m_bAltPressed;
247 bCtrl = m_bCtrlPressed;
248 bShift = m_bShiftPressed;
249 return true;
250 }
252 bool CKeyboardEdit::GetJamKey(WORD &wJamKey) const
253 {
254 if (m_wJamKey != 0)
255 wJamKey = m_wJamKey;
256 return m_wJamKey != 0;
257 }
259 bool CKeyboardEdit::IsDefined() const
260 {
261 return bool(m_wVirtKey || m_bAltPressed || m_bCtrlPressed || m_bShiftPressed);
262 }
264 bool CKeyboardEdit::IsFinished() const
265 {
266 bool finished = true;
267 for (int i = 0; i < 256; ++i)
268 if (m_keys[i])
269 finished = false;
270 return finished;
271 }