Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[42e688]: KeySend.cpp Maximize Restore History

Download this file

KeySend.cpp    207 lines (169 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright (c) 2003-2007 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
#include "keysend.h"
#include <Winable.h>
CKeySend::CKeySend(void) : m_delay(10)
{
// We want to use keybd_event (OldSendChar) for Win2K & older,
// SendInput (NewSendChar) for newer versions.
// For non-Unicode builds, only OldSendChar.
#ifdef UNICODE
OSVERSIONINFO os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&os) == FALSE) {
ASSERT(0);
}
m_isOldOS = ((os.dwMajorVersion == 4) ||
(os.dwMajorVersion == 5 && os.dwMinorVersion == 0));
#else
m_isOldOS = true;
#endif /* UNICODE */
// get the locale of the current thread.
// we are assuming that all window and threading in the
// current users desktop have the same locale.
m_hlocale = GetKeyboardLayout(0);
}
CKeySend::~CKeySend(void)
{
}
void CKeySend::SendString(const CMyString &data)
{
const int N = data.GetLength();
for(int n=0;n<N;n++){
SendChar(data[n]);
}
}
void CKeySend::SendChar(TCHAR c)
{
if (m_isOldOS)
OldSendChar(c);
else
NewSendChar(c);
}
void CKeySend::NewSendChar(TCHAR c)
{
UINT status;
INPUT input[2];
input[0].type = INPUT_KEYBOARD;
switch (c) {
case TCHAR('\t'):
case TCHAR('\r'):
input[0].ki.wVk = c == TCHAR('\t') ? VK_TAB : VK_RETURN;
input[0].ki.wScan = 0;
input[0].ki.dwFlags = 0;
break;
default:
input[0].ki.wVk = 0;
input[0].ki.wScan = c;
input[0].ki.dwFlags = KEYEVENTF_UNICODE;
break;
}
// add the key-up event
input[1].ki = input[0].ki;
input[1].ki.dwFlags |= KEYEVENTF_KEYUP;
status = ::SendInput(2, input, sizeof(INPUT));
if (status != 1)
TRACE(_T("CKeySend::SendChar: SendInput failed\n"));
::Sleep(m_delay);
}
void CKeySend::OldSendChar(TCHAR c)
{
BOOL shiftDown=false; //assume shift key is up at start.
BOOL ctrlDown=false;
BOOL altDown=false;
SHORT keyScanCode=VkKeyScanEx(c, m_hlocale );
// high order byte of keyscancode indicates if SHIFT, CTRL etc keys should be down
if(keyScanCode & 0x100){
shiftDown=true;
//send a shift down
keybd_event(VK_SHIFT, (BYTE) MapVirtualKeyEx(VK_SHIFT, 0, m_hlocale ), 0,3); //Fixes bug #1208955
}
if(keyScanCode & 0x200){
ctrlDown=true;
//send a ctrl down
keybd_event(VK_CONTROL, (BYTE) MapVirtualKeyEx(VK_CONTROL, 0, m_hlocale ), KEYEVENTF_EXTENDEDKEY, 0);
}
if(keyScanCode & 0x400){
altDown=true;
//send a alt down
keybd_event(VK_MENU, (BYTE) MapVirtualKeyEx(VK_MENU, 0, m_hlocale ), KEYEVENTF_EXTENDEDKEY, 0);
}
// the lower order byte has the key scan code we need.
keyScanCode =(SHORT)( keyScanCode & 0xFF);
keybd_event((BYTE)keyScanCode, (BYTE) MapVirtualKeyEx(keyScanCode, 0, m_hlocale ), 0, 0);
keybd_event((BYTE)keyScanCode, (BYTE) MapVirtualKeyEx(keyScanCode, 0, m_hlocale ), KEYEVENTF_KEYUP, 0);
if(shiftDown){
//send a shift up
keybd_event(VK_SHIFT, (BYTE) MapVirtualKeyEx(VK_SHIFT, 0, m_hlocale ), KEYEVENTF_KEYUP, 3); //Fixes bug #1208955
shiftDown=false;
}
if(ctrlDown){
//send a ctrl up
keybd_event(VK_CONTROL, (BYTE) MapVirtualKeyEx(VK_CONTROL, 0, m_hlocale ), KEYEVENTF_KEYUP |KEYEVENTF_EXTENDEDKEY, 0);
ctrlDown=false;
}
if(altDown){
//send a alt up
keybd_event(VK_MENU, (BYTE) MapVirtualKeyEx(VK_MENU, 0, m_hlocale ), KEYEVENTF_KEYUP |KEYEVENTF_EXTENDEDKEY, 0);
altDown=false;
}
::Sleep(m_delay);
}
void CKeySend::ResetKeyboardState()
{
// We need to make sure that the Control Key is still not down.
// It will be down while the user presses ctrl-T the shortcut for autotype.
BYTE keys[256];
GetKeyboardState((LPBYTE)&keys);
while((keys[VK_CONTROL] & 0x80)!=0){
// VK_CONTROL is down so send a key down and an key up...
keybd_event(VK_CONTROL, (BYTE)MapVirtualKeyEx(VK_CONTROL, 0, m_hlocale), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_CONTROL, (BYTE) MapVirtualKeyEx(VK_CONTROL, 0, m_hlocale), KEYEVENTF_KEYUP|KEYEVENTF_EXTENDEDKEY, 0);
//now we let the messages be processed by the applications to set the keyboard state
MSG msg;
//BOOL m_bCancel=false;
while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE) )
{
// so there is a message process it.
if (!AfxGetThread()->PumpMessage())
break;
}
Sleep(10);
memset((void*)&keys,0,256);
GetKeyboardState((LPBYTE)&keys);
}
}
// SetAndDelay allows users to put \d500\d10 in autotype and
// the it will cause a delay of half a second then subsequent
// key stokes will be delayed by 10 ms
// thedavecollins 2004-08-05
void CKeySend::SetAndDelay(int d){
SetDelay(d);
::Sleep(m_delay);
}
void CKeySend::SetDelay(int d){
m_delay=d;
}
void CKeySend::SetCapsLock(const bool bState)
{
BYTE keyState[256];
GetKeyboardState((LPBYTE)&keyState);
if((bState && !(keyState[VK_CAPITAL] & 1)) ||
(!bState && (keyState[VK_CAPITAL] & 1))) {
// Simulate a key press
keybd_event(VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
// Simulate a key release
keybd_event(VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
MSG msg;
while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) {
// so there is a message process it.
if (!AfxGetThread()->PumpMessage())
break;
}
}