annotate src/win32/KeyboardEdit.cpp @ 7:c0a590a394c3

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