Mercurial > vba-clojure
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/KeyboardEdit.cpp Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,271 @@ 1.4 +//////////////////////////////////////////////////////////////////////////////// 1.5 +// Copyright (C) 1998 by Thierry Maurel 1.6 +// All rights reserved 1.7 +// 1.8 +// Distribute freely, except: don't remove my name from the source or 1.9 +// documentation (don't take credit for my work), mark your changes (don't 1.10 +// get me blamed for your possible bugs), don't alter or remove this 1.11 +// notice. 1.12 +// No warrantee of any kind, express or implied, is included with this 1.13 +// software; use at your own risk, responsibility for damages (if any) to 1.14 +// anyone resulting from the use of this software rests entirely with the 1.15 +// user. 1.16 +// 1.17 +// Send bug reports, bug fixes, enhancements, requests, flames, etc., and 1.18 +// I'll try to keep a version up to date. I can be reached as follows: 1.19 +// tmaurel@caramail.com (or tmaurel@hol.fr) 1.20 +// 1.21 +//////////////////////////////////////////////////////////////////////////////// 1.22 +// File : KeyboardEdit.cpp 1.23 +// Project : AccelsEditor 1.24 +//////////////////////////////////////////////////////////////////////////////// 1.25 +// Version : 1.0 * Authors : A.Lebatard + T.Maurel 1.26 +// Date : 17.08.98 1.27 +// 1.28 +// Remarks : implementation file 1.29 +// 1.30 + 1.31 +//////////////////////////////////////////////////////////////////////////////// 1.32 +// modified by aquanull 1.33 + 1.34 +#include "stdafx.h" 1.35 +#include "KeyboardEdit.h" 1.36 + 1.37 +extern TCHAR *mapVirtKeysStringFromWORD(WORD wKey); 1.38 + 1.39 +IMPLEMENT_DYNAMIC(CKeyboardEdit, CEdit) 1.40 + 1.41 +///////////////////////////////////////////////////////////////////////////// 1.42 +// CKeyboardEdit 1.43 + 1.44 +CKeyboardEdit::CKeyboardEdit() 1.45 +{ 1.46 + ResetKey(); 1.47 +} 1.48 + 1.49 +CKeyboardEdit::~CKeyboardEdit() 1.50 +{} 1.51 + 1.52 +BEGIN_MESSAGE_MAP(CKeyboardEdit, CEdit) 1.53 +//{{AFX_MSG_MAP(CKeyboardEdit) 1.54 +ON_CONTROL_REFLECT_EX(EN_CHANGE, &CKeyboardEdit::OnEnChange) 1.55 +ON_CONTROL_REFLECT_EX(EN_SETFOCUS, &CKeyboardEdit::OnEnSetfocus) 1.56 +ON_CONTROL_REFLECT_EX(EN_KILLFOCUS, &CKeyboardEdit::OnEnKillfocus) 1.57 +//}}AFX_MSG_MAP 1.58 +END_MESSAGE_MAP() 1.59 + 1.60 +///////////////////////////////////////////////////////////////////////////// 1.61 +// CKeyboardEdit message handlers 1.62 +BOOL CKeyboardEdit::PreTranslateMessage(MSG *pMsg) 1.63 +{ 1.64 + if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN) 1.65 + { 1.66 + int key = pMsg->wParam; 1.67 + m_keys[key] = true; 1.68 + switch (key) 1.69 + { 1.70 + case VK_CONTROL: 1.71 + case VK_MENU: 1.72 + case VK_SHIFT: 1.73 + m_bCtrlPressed = m_keys[VK_CONTROL]; 1.74 + m_bAltPressed = m_keys[VK_MENU];; 1.75 + m_bShiftPressed = m_keys[VK_SHIFT]; 1.76 + if (!m_keys[m_wVirtKey]) 1.77 + { 1.78 + if (m_keys[m_wJamKey]) 1.79 + { 1.80 + m_wVirtKey = m_wJamKey; 1.81 + m_wJamKey = 0; 1.82 + } 1.83 + else 1.84 + m_wVirtKey = 0; 1.85 + } 1.86 + if (!m_keys[m_wJamKey]) 1.87 + m_wJamKey = 0; 1.88 + break; 1.89 + default: 1.90 + m_bCtrlPressed = m_keys[VK_CONTROL]; 1.91 + m_bAltPressed = m_keys[VK_MENU]; 1.92 + m_bShiftPressed = m_keys[VK_SHIFT]; 1.93 + if (m_wVirtKey != key) 1.94 + { 1.95 + if (m_keys[m_wVirtKey]) 1.96 + m_wJamKey = m_wVirtKey; 1.97 + else 1.98 + m_wJamKey = 0; 1.99 + m_wVirtKey = key; 1.100 + } 1.101 + else if (!m_keys[m_wJamKey]) 1.102 + m_wJamKey = 0; 1.103 + break; 1.104 + } 1.105 + DisplayKeyboardString(); 1.106 + return TRUE; 1.107 + } 1.108 + else if (pMsg->message == WM_KEYUP || pMsg->message == WM_SYSKEYUP) 1.109 + { 1.110 + int key = pMsg->wParam; 1.111 + m_keys[key] = false; 1.112 + switch (key) 1.113 + { 1.114 + case VK_CONTROL: 1.115 + case VK_MENU: 1.116 + case VK_SHIFT: 1.117 + break; 1.118 + default: 1.119 + m_bCtrlPressed = m_keys[VK_CONTROL]; 1.120 + m_bAltPressed = m_keys[VK_MENU]; 1.121 + m_bShiftPressed = m_keys[VK_SHIFT]; 1.122 + if (m_wJamKey) 1.123 + { 1.124 + if (!m_keys[m_wVirtKey]) 1.125 + { 1.126 + m_wVirtKey = m_wJamKey; 1.127 + m_wJamKey = 0; 1.128 + } 1.129 + if (!m_keys[m_wJamKey]) 1.130 + m_wJamKey = 0; 1.131 + } 1.132 + break; 1.133 + } 1.134 + DisplayKeyboardString(); 1.135 + return TRUE; 1.136 + } 1.137 + 1.138 + return CEdit::PreTranslateMessage(pMsg); 1.139 +} 1.140 + 1.141 +BOOL CKeyboardEdit::OnEnChange() 1.142 +{ 1.143 + return FALSE; 1.144 +} 1.145 + 1.146 +BOOL CKeyboardEdit::OnEnSetfocus() 1.147 +{ 1.148 + //SetSel(0, -1, TRUE); // mouse click makes this in vain, so we use the method below instead 1.149 + PostMessage(EM_SETSEL, 0, -1); 1.150 + m_bForceUpdate = true; 1.151 + return FALSE; 1.152 +} 1.153 + 1.154 +BOOL CKeyboardEdit::OnEnKillfocus() 1.155 +{ 1.156 + AllKeyUp(); 1.157 + return FALSE; 1.158 +} 1.159 + 1.160 +//////////////////////////////////////////////////////////////////////// 1.161 +// 1.162 +void CKeyboardEdit::DisplayKeyboardString() 1.163 +{ 1.164 + CString strKbd; 1.165 + 1.166 + // modifiers 1.167 + if (m_bCtrlPressed) 1.168 + strKbd = "Ctrl"; 1.169 + 1.170 + if (m_bAltPressed) 1.171 + { 1.172 + if (strKbd.GetLength() > 0) 1.173 + strKbd += '+'; 1.174 + strKbd += "Alt"; 1.175 + } 1.176 + if (m_bShiftPressed) 1.177 + { 1.178 + if (strKbd.GetLength() > 0) 1.179 + strKbd += '+'; 1.180 + strKbd += "Shift"; 1.181 + } 1.182 + // virtual key 1.183 + LPCTSTR szVirtKey = mapVirtKeysStringFromWORD(m_wVirtKey); 1.184 + if (szVirtKey != NULL) 1.185 + { 1.186 + if (strKbd.GetLength() > 0) 1.187 + strKbd += '+'; 1.188 + strKbd += szVirtKey; 1.189 + } 1.190 + // jammed key 1.191 + LPCTSTR szJamKey = mapVirtKeysStringFromWORD(m_wJamKey); 1.192 + if (szJamKey != NULL) 1.193 + { 1.194 + strKbd += '('; 1.195 + strKbd += szJamKey; 1.196 + strKbd += ')'; 1.197 + } 1.198 + 1.199 + if (m_bForceUpdate) 1.200 + { 1.201 + m_bForceUpdate = false; 1.202 + SetWindowText(strKbd); 1.203 + } 1.204 + else 1.205 + { 1.206 + CString oldString; 1.207 + GetWindowText(oldString); 1.208 + if (oldString.Compare(strKbd)) 1.209 + SetWindowText(strKbd); 1.210 + } 1.211 +} 1.212 + 1.213 +//////////////////////////////////////////////////////////////////////// 1.214 +// 1.215 +void CKeyboardEdit::ResetKey() 1.216 +{ 1.217 + AllKeyUp(); 1.218 + 1.219 + m_bForceUpdate = true; 1.220 + m_bCtrlPressed = false; 1.221 + m_bAltPressed = false; 1.222 + m_bShiftPressed = false; 1.223 + m_wVirtKey = 0; 1.224 + m_wJamKey = 0; 1.225 + 1.226 + if (m_hWnd != NULL) 1.227 + { 1.228 + CString oldString; 1.229 + GetWindowText(oldString); 1.230 + if (!oldString.IsEmpty()) 1.231 + SetWindowText(_T("")); 1.232 + } 1.233 +} 1.234 + 1.235 +void CKeyboardEdit::AllKeyUp() 1.236 +{ 1.237 + for (int i = 0; i < 256; ++i) 1.238 + m_keys[i] = 0; 1.239 +} 1.240 + 1.241 +//////////////////////////////////////////////////////////////////////// 1.242 +// 1.243 +bool CKeyboardEdit::GetAccelKey(WORD &wVirtKey, bool &bCtrl, bool &bAlt, bool &bShift) const 1.244 +{ 1.245 + if (!IsDefined()) 1.246 + return false; 1.247 + 1.248 + wVirtKey = m_wVirtKey; 1.249 + bAlt = m_bAltPressed; 1.250 + bCtrl = m_bCtrlPressed; 1.251 + bShift = m_bShiftPressed; 1.252 + return true; 1.253 +} 1.254 + 1.255 +bool CKeyboardEdit::GetJamKey(WORD &wJamKey) const 1.256 +{ 1.257 + if (m_wJamKey != 0) 1.258 + wJamKey = m_wJamKey; 1.259 + return m_wJamKey != 0; 1.260 +} 1.261 + 1.262 +bool CKeyboardEdit::IsDefined() const 1.263 +{ 1.264 + return bool(m_wVirtKey || m_bAltPressed || m_bCtrlPressed || m_bShiftPressed); 1.265 +} 1.266 + 1.267 +bool CKeyboardEdit::IsFinished() const 1.268 +{ 1.269 + bool finished = true; 1.270 + for (int i = 0; i < 256; ++i) 1.271 + if (m_keys[i]) 1.272 + finished = false; 1.273 + return finished; 1.274 +}