Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I use SendKeys api to send keystokes to some other app. I want to stop this when app loses focus . Is there posibility to check if other app has focus or even better to make app can't lose focus ? Thanks in advance.
Posted
Updated 5-Mar-11 7:08am
v3
Comments
Sandeep Mewara 5-Mar-11 12:46pm    
even better to make app can't lose focus
Avoid it, users would get furious if you do it

To make a window focus use the windows api function setForgroundWindow.

MSDN decumentation about it here.

http://msdn.microsoft.com/en-us/library/ms633539%28v=vs.85%29.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Mar-11 19:04pm    
Unfortunately, this will not solve the problem (and you did not ask how to find out if a form is "focused" (in fact, activated, but this does not matter). More importantly, it won't solve the problem: it is a bit more complex. Please see my Answer.
The real problem is this: as you click on Virtual Keyboard, target Window's focus is lost.
Sorry, only 4 this time.
--SA
For forms, a Form's "focus" is actually it's activation; you can use Form.Activate. If this is some Windows which is not a form, you can use the Answer by Albin. To know if a Window is in foreground you should use Windows API GetForegroundWindow which returns HWND and compare with HWND of the Window in question. You need to use P/Invoke as well as for SetForegroundWindow.

This along will not solve your problem. For full resolution, please see these two Answers:

Application focus getting and losing[^]
Programming on BACKSPACE button[^]

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900