About Store Forum Documentation Contact



Post Reply 
Simulating keystrokes
Author Message
Kiekos Offline
Member

Post: #1
Simulating keystrokes
Hey folks!

This question is not strictly game-connected but it's EE-connected (at least somehow). I was asked by a friend of mine to create a simple "always-on-top" app that would only have several buttons as its UI. Button - once pressed - would send a specific sequence of chars to currently active bar/window (for example to browser's address bar).

So let's say I have that address bar active and I click "eBay" button in the app. It should send "www.ebay.com" string to that bar. I hope you get the point.

I wanted to ask if any of you guys know which method I should use to achieve that. I'd like to use EE for the GUI as it's gonna be fairly easy but the coding is a tricky part. I don't think SendKeys or SendInput will work in this case (those are the two most common methods as far as I know). Maybe EE has something that will help even a little bit?

Cheers,
Kiekos

PS Sorry if it's too much OT.
04-25-2014 07:19 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Simulating keystrokes
Something like that is too intrusive IMO.
I'd rather go with
opening browser with specific link using OSLaunch
or get/set clipboard with ClipSet/ClipGet

I wouldn't trust an app that manipulates input
04-25-2014 09:25 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #3
RE: Simulating keystrokes
I mean, I just want to simulate a sequence of keystrokes. How is that intrusive?

I'm not going to inject anything. It's just like instead of writing this answer which I'm writing right now I would simply click a button in my app when this text box is active (blinking cursor).
04-25-2014 10:35 PM
Find all posts by this user Quote this message in a reply
PsychoBoy Offline
Member

Post: #4
RE: Simulating keystrokes
Use SendMessage/PostMessage, for eg:
Code:
SendMessage(FindWindow(NULL, "Browser"), WM_CHAR, VkKeyScan('a'), true);
04-25-2014 10:55 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #5
RE: Simulating keystrokes
@PsychoBoy, I don't really want to search for a specific window - I'd like to send input to currently active box/bar (if that's possible). So it's like *click on a bar*, *click a button* and boom - text is sent there. Browser/address bar was just an example smile

I just came up with something like this, but will that work along with EE?

Code:
void GenerateKey(BYTE vk) //this function is taken from an online tutorial
{
    INPUT Input;
    ZeroMemory(&Input, sizeof(Input));
    Input.type = INPUT_KEYBOARD;
    Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
    Input.ki.wVk = vk;
    SendInput(1, &Input, sizeof(INPUT));

    return;
}

void  SendText(char *Text)
{
      for(int i=0; i<strlen(Text); i++)
          GenerateKey( (UCHAR) VkKeyScan( Text[i] ) );
}

//and then simply
SendText("Sample text...");

Is it already going to send it to currently active box or do I have to find and activate that box (in code) in order for the input to be sent?

Btw, I've read something about:
Code:
keyboard_event(VirtualKey, 0, flag, 0);
but I don't really know how to use it in this particular case.
04-25-2014 11:17 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #6
RE: Simulating keystrokes
I do realize it's not necessary, but it was the first thing that came through my mind as creating a UI like that is a matter of minutes. I'll see what does the trick!

Thank you for the link. Though, I'm wondering... All these tutorials show apps that send strings/chars to other apps such as Notepad (they find active Notepad window? or create a txt file with sent input?).

None shows sending to currently active element on screen. I've been going through MSDN and WinAPI documentations and couldn't find anything apart from activating windows...



EDIT: Yeah... things got complicated. I found out that even if the app is always on top, still every time I click the button it deactivates the other window / bar / text area that I clicked before.

So either way I have to send input to something that I find and activate through code and that's something I simply don't know how to do. For example I'd have to search for Chrome process, then for a particular tab and eventually for a specific text area on the website...
(This post was last modified: 04-26-2014 05:31 PM by Kiekos.)
04-26-2014 11:41 AM
Find all posts by this user Quote this message in a reply
Post Reply