Link to home
Start Free TrialLog in
Avatar of CosmoNut
CosmoNut

asked on

Alternatives to using SendKey

Hello,  I am building an app which requires sending key strokes to a seperate application.  I am having some issues using sendkeys and wodering if there are any other options to complete this task? what I need and how Ive done it...

AppActivate "Other App"
 SendKeys ("t"), True
 SendKeys ("Hi!), True
 SendKeys ("{ENTER}"), True

#1)It seems I must set the focus to the other app inorder to use sendkeys, Id like to avoid this if possiable, as it is very bothersome to have your windows switched on you while working.

#2) If I include a "DoEvents" the sendkeys is interrupted by any user interaction, if I dont then all other user functions are dead until this completes.

Is there a better way to do this? No not writing nasty code! Am trying to send a text message in game to a game server that will be displayed on a timer, the above procedure works fine if the server is dedicated and not used for anything else.  I would like to be able to still do this if the user decides to play and host with minimal interuption. I have the memory address that enables the text box and also the address that contains the char's that are typed into it, this would work except I cant write the enter key press into memory to actually send it.
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phallout
phallout

Here you go...  Here is an "API Spy"  It is good to find all of the window information, handle, class, parent handle, parent class, etc..  It will even write code for you, although you should learn yourself.  There is a link to a tutorial on how the use this program on the same page.  http://www.patorjk.com/progapispy.htm

You can also check out the site by itself, a little programming information there but it's mostly aimed towards AOL but still useful.  http://www.patorjk.com
Avatar of CosmoNut

ASKER

Hi and thank you so far for your comments. It isnt a true text box per say, is an ingame text area.  Am trying the spying but dont seem to be having much luck.
Hmm... I made a program to send commands (cheat codes) to AOE.  I never could get API to work for some reason.  I had to use sendkeys.  Since AOE ran fullscreen, I used a form that would dock at the top of the screen & stay above all other windows (stayontop in a timer).  When you put the mouse over each button, it would send specific commands to the currently active window (in this case AOE)

sendkeys "{enter}", true   'to bring up AOE chat window
sendkeys "lumberjack", true  'cheat for wood
sendkeys "{enter}", true 'close chat window

I used that code in a loop until the mouse was moved from the button.

That worked fine for me.

The problem on why API would not work was because the game does not actually have "forms" or "objects" for the mostpart.  It is all just images drawn to the screen.  If the text box is an actual object with a handle (vs. graphics made to act like a text box) then you should be able to use API on it (in theory)


it doesn't have to be a textbox... when you run spy++ and you do search->findwindow (check 'hide spy++) and drag the target icon over the textarea or whatever you want the class name of... does it work?
Ok well as the text area is actually built into the game(and it does not run windowed only full screen) I must alt-tab out of it to get the spy++. And ironically phallout this is for an anti-cheat that runs server side and the text I wish to display is a message to players so they know its running!  

I did also try writing a CR at the end of my text to the memory address which stores the text data, that did not work either.
Thank you both very much for your help with this. I have found the answer I was looking for using postmessage works great......https://www.experts-exchange.com/questions/20431023/PostMessage-API.html?query=postmessage&searchType=topic.  Bobbit the points are yours as you seemed to be heading that direction, or atleast away from sendkeys! Before I close out would you mind very much explaining the pro's and con's or atleast the difference between using the sendmessage and postmessage commands? I will gladly add an extra 50pts.

Thank you both again!
from http://www.developerfusion.com/show/34/4/

Both require same parameters but there’s a slight difference. When a message is sent to a window with SendMessage, the window procedure is called and the calling program (or thread) waits for the message to be processed and replied back, and until then the calling program does not resume its processing. One thing is wrong with this approach however, that is if the program that is busy carrying out long instructions or a program that has been hung and hence no time to respond to the message will in turn hang your program too because your program will be waiting for a reply that may never arrive. The solution to this is to use PostMessage instead of SendMessage. PostMessage on the otherhand returns to the calling program immediately without waiting for the thread to process the message, hence saving your program from hanging. Which of them you have to use depends on your requirement.
That would explain why I needed a slight delay between Sending the "t" and waiting for the box to appear and then sending the text line. Thank you very much!!!!!!
Thanks again and have a great day!