Here's what I'm working with.

#include <windows.h>
    #include "iostream"
    void SendText(char* message, int size)
    {
    int lc=0;
    do{
    keybd_event(VkKeyScan(message[lc]),0,KEYEVENTF_EXTENDEDKEY,0);
    keybd_event(VkKeyScan(message[lc]),0,KEYEVENTF_KEYUP,0);
    lc=lc+1;
    }

    while(lc<size);

    keybd_event(VK_RETURN,0,KEYEVENTF_EXTENDEDKEY,0); //Presses Return
    keybd_event(VK_RETURN,0,KEYEVENTF_KEYUP,0); //Presses Return
    }

    int main()
    {

    Sleep(5000);

    char a=98;

    SendText("a", strlen("a"));


    }

I think this is a noobish question but, how would I get it to type out whatever is stored in the variable "a" when using the function SendText. I know it has something to do with inserting the variable into the parameters but I can't seem to figure out what I need to do to get it to spit out the ASCII character stored in "a" (98=b in this case)...

Everything I try to do gives me issues because the ASCII codes don't match up at all with virtual key codes

What I'm trying to do is start the program, pause so I can select an input box in another program, then cycle through a bunch of ASCII characters and press enter after each one. But I can't really start working on the cycling through characters part untill I figure out how to send them properly in the first place. Maybe there's a better way, I'm open to suggestions.

my example output would be, for instance:
Start program
during 5 second wait I select the input box in a different window
5 seconds ends and the program enters:
1
Enter
2
Enter
3
Enter
...
999
Enter

and so on

That's actually a good question because you've stumbled on a subtle behavior of keybd_event(). What it does is queue up key presses to the program with focus. In this case that program is itself (confused yet?). The problem is that this program isn't listening for input, and doesn't ever listen for input before terminating. Therefore the key presses will never be echoed.

The act of requesting input from stdin is what will cause the queued keys to echo, and you can also extract them at the same time:

#include <windows.h>

void SendKeyboardLine(const char *message)
{
    int lc = 0;

    while (*message) {
        keybd_event(VkKeyScan(*message), 0, 0, 0);
        keybd_event(VkKeyScan(*message), 0, KEYEVENTF_KEYUP, 0);
        ++message;
    }

    keybd_event(VK_RETURN, 0, KEYEVENTF_EXTENDEDKEY, 0);
    keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
}

#include <iostream>
#include <string>

int main()
{
    std::string line;

    std::cout << "Sending keyboard input . . .\n";
    SendKeyboardLine("This is a test");
    std::cout << "Reading keyboard input . . .\n";

    if (getline(std::cin, line))
        std::cout << "You typed '" << line << "'\n";
}

Hmm, didn't know any of that.

It works fine when I'm testing with notepad, but when I clicked into the box I was trying to work with it doesn't do anything at all.

Thanks for the info, though I'm not exactly any closer to my goal lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.