Troudeloup
asked on
simulating keystroke - print a string.
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
void output ( string string1 );
int main ()
{
Sleep ( 5000 );
// input hello hoshen
string string1;
string1 = "hellohEEpeople";
output ( string1 );
return 0;
}
void output ( string string1 )
{
int size = string1.size();
for (int i = 0; i < size; ++i)
{
BYTE vk = LOBYTE(::MapVirtualKey(str ing1[i], 0));
keybd_event(string1[i],vk, 0,0);
keybd_event(string1[i],vk, KEYEVENTF_ KEYUP,0);
}
}
this actually would print gibbrish, how can it be fixed?
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
void output ( string string1 );
int main ()
{
Sleep ( 5000 );
// input hello hoshen
string string1;
string1 = "hellohEEpeople";
output ( string1 );
return 0;
}
void output ( string string1 )
{
int size = string1.size();
for (int i = 0; i < size; ++i)
{
BYTE vk = LOBYTE(::MapVirtualKey(str
keybd_event(string1[i],vk,
keybd_event(string1[i],vk,
}
}
this actually would print gibbrish, how can it be fixed?
ASKER
what if I need keys like enter and F1?
ASKER
actually, i think I prefer keybd_event() if it's ok.
can you correc the codes in the original post for me?
can you correc the codes in the original post for me?
#include <vector>
using namespace std;
struct KEYINPUT
{
BYTE virtualKey;
DWORD nFlags;
};
typedef vector<KEYINPUT> VKVECTOR;
void output ( const VKVECTOR& virtualKeys )
{
for( VKVECTOR::const_iterator it = virtualKeys.begin(); it != virtualKeys.end(); ++it )
{
BYTE vsc = LOBYTE(::MapVirtualKey(it->virtualKey, 0));
keybd_event(it->virtualKey,vsc,it->nFlags,0);
}
}
int main ()
{
Sleep ( 5000 );
KEYINPUT ki[12] = {
VK_SHIFT, 0,
0x48, 0, // H
0x48, KEYEVENTF_KEYUP,
VK_SHIFT, KEYEVENTF_KEYUP,
0x45, 0, // e
0x45, KEYEVENTF_KEYUP,
0x4C, 0, // l
0x4C, KEYEVENTF_KEYUP,
0x4C, 0, // l
0x4C, KEYEVENTF_KEYUP,
0x4F, 0, // o
0x4F, KEYEVENTF_KEYUP
};
VKVECTOR vInput;
for(int i = 0; i < 12; ++i)
{
vInput.push_back(ki[i]);
}
output( vInput );
return 0;
}
BTW, a list of virtual key codes (such as 0x48 for H in my code above) is at: http://msdn2.microsoft.com/en-us/library/ms645540.aspx
Just a small thing missing:
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
void output ( string string1 );
int main ()
{
Sleep ( 5000 );
// input hello hoshen
string string1;
string1 = "hellohEEpeople";
output ( string1 );
return 0;
}
void output ( string string1 )
{
int size = string1.size();
for (int i = 0; i < size; ++i)
{
BYTE vk = LOBYTE(::MapVirtualKey(str ing1[i], 0));
keybd_event(VkKeyScan(stri ng1[i]),vk ,0,0);
Sleep(100);
keybd_event(VkKeyScan(stri ng1[i]),vk ,KEYEVENTF _KEYUP,0);
}
}
'VkKeyScan()' is needed for the shift state.
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
void output ( string string1 );
int main ()
{
Sleep ( 5000 );
// input hello hoshen
string string1;
string1 = "hellohEEpeople";
output ( string1 );
return 0;
}
void output ( string string1 )
{
int size = string1.size();
for (int i = 0; i < size; ++i)
{
BYTE vk = LOBYTE(::MapVirtualKey(str
keybd_event(VkKeyScan(stri
Sleep(100);
keybd_event(VkKeyScan(stri
}
}
'VkKeyScan()' is needed for the shift state.
ASKER
how about special keys like F1 and enter?
For that you'd use 'VK_F1' and 'VK_RETURN', e.g.
string1 = "helloh EEpeople";
string1[6] = VK_RETURN;
At least, that works for mne here. For a more in-depth article about this issue, read http://www.codeproject.com/cpp/sendkeys_cpp_Article.asp ("SendKeys in C++")
string1 = "helloh EEpeople";
string1[6] = VK_RETURN;
At least, that works for mne here. For a more in-depth article about this issue, read http://www.codeproject.com/cpp/sendkeys_cpp_Article.asp ("SendKeys in C++")
ASKER
string1[6] = VK_RETURN;
what is this?
also, what is mne?
what is this?
also, what is mne?
ASKER
#include <iostream>
using namespace std;
int main ()
{
string string1 = "hellohoshen";
string1[6] = VK_RETURN;
cout << string1 << endl;
return 0;
}
test.cpp:8: error: `VK_RETURN' was not declared in this scope
using namespace std;
int main ()
{
string string1 = "hellohoshen";
string1[6] = VK_RETURN;
cout << string1 << endl;
return 0;
}
test.cpp:8: error: `VK_RETURN' was not declared in this scope
You forgot
#include <windows.h>
#include <windows.h>
>>also, what is mne?
'me' - seems to be my typo day ;o)
'me' - seems to be my typo day ;o)
ASKER
O_o
why not use sendkeys in the first place?
why not use sendkeys in the first place?
ASKER
oh nvm, it's script.
but then again this doesn't capitalize EE and it doesn't show decimal, am I using special keys correcty?
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
void output ( string string1 );
int main ()
{
Sleep ( 5000 );
// input hello hoshen
string string1;
string1 = "hellohEEpeople";
string1[14] = VK_DECIMAL;
Sleep ( 3000 );
output ( string1 );
return 0;
}
void output ( string string1 )
{
int size = string1.size();
for (int i = 0; i < size; ++i)
{
BYTE vk = LOBYTE(::MapVirtualKey(str ing1[i], 0));
keybd_event(VkKeyScan(stri ng1[i]),vk ,0,0);
Sleep(100);
keybd_event(VkKeyScan(stri ng1[i]),vk ,KEYEVENTF _KEYUP,0);
}
}
but then again this doesn't capitalize EE and it doesn't show decimal, am I using special keys correcty?
#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
void output ( string string1 );
int main ()
{
Sleep ( 5000 );
// input hello hoshen
string string1;
string1 = "hellohEEpeople";
string1[14] = VK_DECIMAL;
Sleep ( 3000 );
output ( string1 );
return 0;
}
void output ( string string1 )
{
int size = string1.size();
for (int i = 0; i < size; ++i)
{
BYTE vk = LOBYTE(::MapVirtualKey(str
keybd_event(VkKeyScan(stri
Sleep(100);
keybd_event(VkKeyScan(stri
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SendInput() lets you supply Unicode character values, so I used it instead.
Open in new window