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

[37d182]: KeySend.cpp Maximize Restore History

Download this file

KeySend.cpp    136 lines (101 with data), 3.8 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
#include "keysend.h"
#include <Winable.h>
CKeySend::CKeySend(void) : m_delay(10)
{
// 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)
{
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 ), KEYEVENTF_EXTENDEDKEY, 0);
}
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 |KEYEVENTF_EXTENDEDKEY, 0);
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;
}
::BlockInput(true);
::Sleep(m_delay);
::BlockInput(false);
}
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);
::BlockInput(true);
::Sleep(m_delay);
::BlockInput(false);
}
void CKeySend::SetDelay(int d){
m_delay=d;
}