Catching a moment when some specific "laggy" application window finishes
processing its keyboard input
I want to show a popup when I get some specific input I into any currently
running application A. Depending on popup's dialog result, I want to
modify the text into application A's current textbox by sending keystrokes
using SendKeys class. I'm currently using low level keyboard hook, which
means I could get keydown events before application A process it. In this
case my popup will be shown before last part of the inputted text appears
in the A's text box. Popup will get the focus and prevent this part of
text from reaching A.
For example, I'm searching for lol keyword. User opens Notepad and inputs
"lol". My application should show popup window "Press Enter to add
blahblahblah text" at this moment. Popup window became foreground. If user
presses Enter, application closes this dialog, makes Notepad foreground
back again and sends "blahblahblah" as a keystrokes to the system.
Everything works fine with the Notepad, but what if target
application/system works slowly and not responding sometimes? Considering
the next test app:
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Interval = 2000;
}
private void timer1_Tick(object sender, EventArgs e)
{
Sleep();
}
void Sleep()
{
System.Threading.Thread.CurrentThread.Join(1900);
}
This form contains a textbox and its UI thread is sleeping 95% of the
time. It processing keyboard input each 2 seconds. So if user inputs lol
in such a window, my application most likely will show the popup before
"lol" text appears in the textbox.
I do not want to show my popup before all the text reached the target app A.
Can you please suggest any method of getting the moment when some process
consumed all the keyboard input? SendKeys.Flush() is not working for me.
So does Process.WaitForInputIdle(), which is designed for other purpose, I
suppose. I've tried to send WM_ACTIVATE message via SendMessage to the
target app, because I thought it's exactly what I'm looking for:
The SendMessage function calls the window procedure for the specified
window and does not return until the window procedure has processed the
message.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx
User32.SendMessage(activeWindow, 6, 1, 0)
call returns 0, but not creating required pause and my popup appears
before the text.
Thanks in advance.
No comments:
Post a Comment