Alfred
2010-05-22 10:44:10 UTC
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
using namespace std;
bool isLower (char ch);
bool isCaps (char ch);
char convert (char ch);
void otherChars (char ch);
/* This is a function to simplify usage of sending keys */
void GenerateKey(int vk, BOOL bExtended);
int main() {
ifstream ins;
string fileName;
string Window;
char i;
cout << "Enter the name of the window: ";
getline(cin, Window);
cout << endl;
cout << "Enter filename: ";
cin >> fileName;
/* HWND = "Window Handle" */
HWND UserWindow = FindWindowA(0, Window.c_str());
SetForegroundWindow(UserWindow);
ins.open( fileName.c_str() );
while (!ins.eof())
{
ins.get(i);
if (isCaps(i))
{
GenerateKey(VK_CAPITAL, TRUE);
GenerateKey(i, FALSE);
GenerateKey(VK_CAPITAL, TRUE);
}
else if (isLower(i))
{
GenerateKey(convert(i), FALSE);
}
//If it's a new line, press enter
else if (i == '\n')
{
GenerateKey(0x0D, FALSE);
}
else if (i == ' ')
{
GenerateKey(' ', FALSE);
}
else
{
otherChars(i);
}
}
return 0;
}
void GenerateKey(int vk, BOOL bExtended) {
KEYBDINPUT kb = {0};
INPUT Input = {0};
//Generate a "key down"
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
// Generate a "key up"
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
return;
}
bool isCaps(char ch)
{
if ('A' <= ch && ch <='Z')
return true;
else
return false;
}
bool isLower(char ch)
{
if ('a' <= ch && ch <='z')
return true;
else
return false;
}
char convert (char ch)
{
ch = ch - 32;
return ch;
}
void otherChars (char ch)
{
if (ch == '.')
{
GenerateKey(ch, FALSE);
}
}