By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Can't send keystrokes to application

Hi all,
I'm very familiar with TCC/LE, which I think is a wonderful thing. As I want to send keystrokes to a specific window, I installed TCC (trial) - and if it works, I'm quite willing to purchase it. But my script does not work. I wrote a BTM script which launches an application (Capture4ME.exe) after a specific time. I want then to send keystrokes to it, in order to activate an input channel and start a recording. After a certain amount of time I want to send keystrokes to this application again, to stop the recording and exit the application.
The application starts allright, but it does not receive the keystrokes I send to it. When it starts straight away, it works well. But when it starts after a certain time (let's say 10-15 minutes) the application starts but does not receive the keystrokes.
What is wrong? Can anyone shine a light on this?
OS is Windows 7 Pro 64b. There are more applications running on this pc.
Attached files are the BTM file (scheduler.btm) and the file with the start time and length of recording the BTM file reads (schedule.txt).
Thanks very much in advance for your help. I'm very curious to your comments.
With kind regards,
Fokke Nauta


From your BTM:
keystack /w10 /l "Capture4ME" tab [8] enter /w250 enter

What does this do?
/l "Capture4ME"

The application starts allright, but it does not receive the keystrokes I send to it. When it starts straight away, it works well. But when it starts after a certain time (let's say 10-15 minutes) the application starts but does not receive the keystrokes.

Does that mean when it starts right away it does receive the keystrokes?
I read in the help files that you can specify a window where to send the keystrokes to.

/I Wait for an input idle or the specified number of milliseconds.
/I=pid,milliseconds Look for the specified process ID
/I"Title",milliseconds Look for the specified window title

I tried the 2nd entry (Look for the specified window title) and specified the amount of milliseconds, but that did not make sense. I left the amount of milliseconds out.
Should I write /l "Capture4ME", 1000 here? As an example?
That option uses /I or /i. Your BTM uses the lowercase version of /L. And it's purpose is to wait until the app/window has been idle for a while. I don't think you need that at all. You have already made "Capture4ME" the foreground window with ACTIVATE, and keystrokes automatically go to the foreground window.
Okay, that makes sense. I thought it was the lowercase version of /L, but should use the /i instead.
I thought I needed that, as the key strokes did not go to the window "Capture4ME" I specified! That didn't happen! I don't understand why. I mentioned topmost with ACTIVATE.
activate "Capture4ME" topmost
keystack /w10 /i "Capture4ME",1000 tab [8] enter /w250 enter

Didn't make a change. Did not work.
You can try to debug from the command line. Start simple.

If Capture4Me is the foreground app when it starts you don't need to activate it. Try

start /pgm "C:\Program Files (x86)\Capture4ME\Capture4Me.exe" & delay 3 & keystack F4

If not try
start /pgm "C:\Program Files (x86)\Capture4ME\Capture4Me.exe" & activate "Capture4Me" & delay 3 & keystack F4

If that srarts it and then closes it, it's getting keystrokes. Build from there.
Hey Vince, you'll need an Alt along with the F4 if you want to close something. :)

Thanks! ... I know ... was just about to call attention to my error.

I downloaded a ZIP version of Capture3Me. This worked for me.
start Capture4ME.exe & delay 10 & keystack Alt-F4

Note the 10 second delay. 3 seconds (as in your BTM) was not enough. Here, Capture4Me takes a little over 5 seconds before it appears and accepts input.

This little BTM also works.
pushd "c:\users\vefatica\desktop\capture4me\capture4me"
start Capture4Me.exe
delay 10
keystack Tab [5] Enter

I don't know what your "Tab [8]" does. For me, "Tab [4]" gets me to "Start Device" and "Tab [5]" gets me to "Record".
I tried this:

start /pgm "C:\Program Files (x86)\Capture4ME\Capture4Me.exe" & delay 3 & keystack tab [8] enter /w250 enter

When I start it straight away, it works. 8 times tab and Enter brings me to "Start Device", and the 250 clock ticks are enough to get the HDMI signal in. The last Enter starts the recording.
But when I wait for 15 minutes, like this in the beginning of the btm file:

do until %@instr[0,5,%_time] == %@line[schedule.txt,0]
delay 10

It won't work. That's why I used Activate, but inspite of that the application does not receive the key strokes.
Right now I'm trying this:

start /pgm "C:\Program Files (x86)\Capture4ME\Capture4Me.exe" & activate "Capture4Me" & delay 3 & keystack tab [8] enter /w250 enter

With a 15 minutes waiting time before this line starts.
Doesn't work either.
When I start it immediately, it works. When I build in a delay of - let's say - 15 minutes before the application starts, the application won't receive the key strokes. Even inspite of the fact that I used Activate with topmost, and in the keystack the option /i (now correct) with the name of the window of the application,
This was my original solution:

pushd "C:\Program Files (x86)\Capture4ME\"
start Capture4ME.exe
delay 3
activate "Capture4ME" topmost
keystack /w10 /i "Capture4ME",1000 tab [8] enter /w250 enter

Once again, when I start it immediately, it works well. After a delay of 15 minutes the application does not receive the key strokes. And I don't understand why.
The option /i "Capture4ME",1000 in the keystack command does not work. Leaving this out let keystack apply the keystrokes, but then again as there is no delay at the beginning.
I have given up. I brought the script back to the essential:

pushd "C:\Program Files (x86)\Capture4ME"
start Capture4Me.exe
delay 3
activate "Capture4ME"
keystack /w10 tab [8] enter /w250 enter
delay 60
activate "Capture4ME"
keystack /w10 enter /w20 alt-f4

This works, as long as the delay in between isn't longer than a few minutes. And if there is no delay before :on.

So I think I have to find a different solution.

Thanks for your help.

As for the delays, you may be fighting with Windows itself. A process cannot arbitrarily set the foreground window (the window which will receive keystrokes). I have seen this in my testing. Regardless of the delay before starting Capture4Me, if I do nothing, leaving TCC in the foreground, it works OK. But if I'm doing something else (like browsing these forums) when the time comes then TCC cannot make Capture4Me come to the foreground. MSDN has this to say.

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.
Thanks, Vince. I have used the keystack command in many situations to send key strokes to an application and it never failed. In the current situation it is simply not reliable. Sometimes it works, many times not. I guess you are right, that other processes on this pc have their influences.
I am now investigating how to solve my problem with RoboTask. Sending key strokes to the window of Capture4ME works well. Now I have to study on how to create expressions and how to read a file. I'm familiar with TCC/LE, but RoboTask is completely new to me. Interesting stuff, it is ...
Thanks for your help. Pity that TCC couldn't solve my problem.
With regards,
Below is an interesting experiment. It shows that you can send keystrokes to (at least some) apps, even if they are not in the foreground ... even if they're minimized! After getting the HWND of the Capture4Me window, this posts the WM_KEYDOWN message (256) four times with tab (VK_TAB = 9). That gets me to the "Record" button. Then I post WM_KEYDOWN with Enter (VK_ENTER = 13) to press the button. 10 seconds later I post the WM_CLOSE message (16) and that closes the window and terminates the app.

pushd "c:\users\vefatica\desktop\capture4me\capture4me"

start Capture4Me.exe

do while not iswindow "Capture4Me" ( delay /m 500 )

set hwnd=%@winapi[user32,FindWindowEx,0,0,0,Capture4Me]
set visible=%@winapi[user32,IsWindowVisible,%hwnd]

do while %visible == 0
    delay /m 500
    set visible=%@winapi[user32,IsWindowVisible,%hwnd]

:: Put TCC in the foreground (for emphasis)
window restore

:: minimize Capture4Me (for emphasis)
activate "Capture4Me" min

:: four tabs - WM_KEYDOWN/VK_TAB
do i=1 to 4
    delay /m 300
    echo %@winapi[user32,PostMessage,%hwnd,256,9,0] > nul

delay /m 300
echo %@winapi[user32,PostMessage,%hwnd,256,13,0] > nul

delay 10
echo %@winapi[user32,PostMessage,%hwnd,16,0,0] > nul

Hi Vince,
Thanks for your help. Interesting, this goes ways beyond my knowledge.
I modified your file a bit - added some delay here and there. And I need 8 tabs for the start device button, not 4. For the rest it's the same, but it won't work here.
Please see the attached file.
With regards,


What doesn't work?

I cannot really test fully because I have no channels ... no devices.
Many times - not always - the window of Capture4ME does not receive the key strokes. When I start it straight away, it works many times. However, the key strokes after the delay are not always received. When I start with a delaay, it hardly ever works. TCC runs, key strokes are sent, but Capture4ME has not received them.
Right now I'm trying RoboTask. I'm getting some succes,
Use a program that is intended directly for this kind of operations.
You can send keystrokes directly to the window handle, but you must target it specifically, not just any random supposedly-foreground window.
I thought I would be able to activate a window and then send key strokes to it. Obviously not. I now use RoboTask for my purpose, and this works allright. With RoboTask I can specify the window where I want to send key strokes to.
I think AutoHotKey might be a very good candidate for this purpose. Free tool, open source, extremely powerful.
[FOX] Ultimate Translator