need to send keys to any in focus app

Jan 20, 2012 at 8:51 AM

Hi

I can get the keys pressed. OK works good.

I now need to send keys to that same active window/control. like I want to force bla bla bla to be typed irrespective of any key typed (dont wory, i'm not making a virus!)

I have a c++ version working. can you please help me to integrate that in your library?  How can I do that? Please guide me.

#include <windows.h>
#include "stdafx.h"
//#include "aa.h"
//
//#include "resource.h"

//function prototypes
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);

//variables
HINSTANCE hInst;
int i = 0;          //current position in "replacement" string
int endi = 0;       //current position in "endstring" string
char* endstring = "IDDQD";  //endstring - sequence of characters to type to break the curse (OBLIGATORY CAPS)
char* replacement = "BLA ";     //string to replace most things typed - it cycles (OBLIGATORY CAPS)



//windows main function
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    HINSTANCE app = GetModuleHandle(NULL);  //get the handle
    HHOOK hook = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,app,0);   //set the hook
    MSG msg;

    //message loop
    while (GetMessage(&msg,NULL,0,0)>0) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    UnhookWindowsHookEx(hook);  //remove the hook
    return 0;   //exit
}

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    KBDLLHOOKSTRUCT *kb = (KBDLLHOOKSTRUCT *)lParam;

    if (kb->dwExtraInfo==111)   //extra info 111 means, that keystroke was generated artificially in previous cycle
        return CallNextHookEx(NULL, nCode, wParam, lParam); //so just don't do anything

    int eatKey = 0; //if current key is to be eaten - or ommited if you prefer

    BYTE vk = kb->vkCode;   //just a shortcut
    //we have to capture al "KEYDOWNS" - not "KEYUPS" on specific keys - alphanumerical and OEM-specials
    if (wParam==WM_KEYDOWN && ((vk>=0x30 && vk<=0x39) || (vk>=0x41 && vk<=0x5A) || (vk>=0x60 && vk<=0x69) || vk==0xBA || vk==0xBF || vk==0xC0 || vk==0xDB || vk==0xDC || vk==0xDD || vk==0xDE || vk==0xDF || vk==0x20))
    {
        //bit of code to walk through or reset endstring iterator "endi"
        if (endstring[endi] == vk)
            endi++;
        else
            endi = 0;
        if (endi >= strlen(endstring))  //if iterator reaches the end,
            PostQuitMessage(0);         //exit gracefully

        eatKey = 1;     //note to ommit the currently pressed key
        INPUT input[2]; //fill the two input structures (for keydown and keyup) - to generate artificial down and up events
        input[0].type = INPUT_KEYBOARD;
        input[0].ki.wVk = (WORD)replacement[i]; input[0].ki.dwFlags = 0;
        input[0].ki.time = kb->time;
        input[0].ki.wScan = 0;
        input[0].ki.dwExtraInfo = 111;  //give them extra info 111, so they can be recognized and not eaten recursively
        input[1].type = INPUT_KEYBOARD;
        input[1].ki.wVk = (WORD)replacement[i++]; input[1].ki.dwFlags = KEYEVENTF_KEYUP;
        input[1].ki.time = kb->time+1;
        input[1].ki.wScan = 0;
        input[1].ki.dwExtraInfo = 111;
        SendInput(2, input, sizeof(INPUT)); //do virtual down and up keystrokes
    }

    if(i >= strlen(replacement)) i = 0;   //if i got to the end of replacement string - reset it to beginning
    if (eatKey)
        return 1;   //to "eat" the key, return fail
    else
        return CallNextHookEx(NULL, nCode, wParam, lParam); //to let it through, return result of the next hook
}

 

Regards

pradvk