C++ Learning Community Forum
August 01, 2010, 03:02:43 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: I am looking for C++ program that does simple notepad automation  (Read 396 times)
rain-13
N00b!!1
*
Posts: 2


View Profile
« on: March 16, 2009, 07:42:57 PM »

Hello!

OOps, wrong place

1) I need to run notepad (done)
2) I want wait until notepad's window is active <--need help with this
3) I want send some keystokes (done, but not working as i Need)<--need help with this
4) I want program to move window to given location <--need help with this

I have already made some source (from other examples).

I am thankful if you add functions for 2nd and 4th   requests, and if you help me improve 3rd thing. I mean I want send string "AbCd123!&\/+-.," as it is, current function sends all characters in upper- or lowercase, it dont send special characters.Also I want change it to send key combination alt + F4.

If I am unclear, ask  more questions

Code:
#include <iostream>
#include <windows.h>
#include <winable.h> /* Dev-C++ specific */
using namespace std;


/* This is a function to simplify usage of sending keys */
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;
}

void TypeText(char String[]) {

    /* You could filter text in here if you wanted :) */
    for (int x = 0; String[x] != 0; x++) { GenerateKey(String[x], FALSE); }
}

void Run(char *app) {
   std::string command = "start ";
   command += app;
   system(command.c_str());
}

int main() {
    /*Run("notepad.exe");*/
    system("start c:\\Windows\\System32\\notepad.exe");
    /* HWND = "Window Handle" */
    HWND GameWindow = FindWindow(0, "Untitled - Notepad");


    /*
       SetForegroundWindow will give the window focus for the
       keyboard/mouse! In other words, you don't have to have
       the game opened upfront in order to emulate key/mouse
       presses, it's very helpful if it's a game that runs
       in fullscreen mode, like StarCraft: Brood War does
    */

    SetForegroundWindow(GameWindow);
    for ( int x = 0; x < 10; x++ ) {
    GenerateKey(VK_CAPITAL, TRUE);
    GenerateKey('K', FALSE);
    GenerateKey(VK_CAPITAL, FALSE);
    TypeText("LAHVI VAJUTUSED GENEREERITAKSE PROGRAMMIPOOLT NING SAADETAKSE SUVALISSE AKNASSE");

    GenerateKey(0x3A, FALSE); /* period key */
    GenerateKey(0x0D, FALSE); /* enter key */
    }
    return 0;
}
« Last Edit: March 16, 2009, 11:01:17 PM by rain-13 » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!