Click here to Skip to main content
15,513,441 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I ensure that my C++/MFC dialog based application keeps keyboard focus when it is running? I have tried making the main window topmost using SetWindowPos() but although it stays on top of other windows, it still looses keyboard focus if someone tries to activate another application. I have to click on my application's title bar to regain keyboard focus.

Thanks

Tony
Posted
Comments
PIEBALDconsult 1-Apr-15 15:27pm    
What does the _user_ want? What you describe seems user-hostile.
softwaremonkey 3-Apr-15 10:07am    
The application is controlling machinery via function keys on the keyboard (customer's requirement, not mine!). The user wants (expects) the machine to respond correctly when he presses the keys at all times. I know Windows isn't the right OS for this kind of thing but again, not my choice ;o)

1 solution

You are supposed to loose focus of someone activates another window, this is the only valid behavior. You need to understand that there is only one focused window (child window, a control) per desktop.

However, if you think that the focus should get back to the previously focused control when a window is activated, you are absolutely right.

High-level UI libraries always take care of that, but MFC is not one of them. You can achieve this effect by handling windows message WM_ACTIVATE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646274%28v=vs.85%29.aspx[^].

With MFC, you can use CWin:OnActivate: https://msdn.microsoft.com/en-us/library/1f31c334.aspx[^].


Congratulations with April 1st! :-)

I would like to use the occasion to invite your (and everyone else, of course) to see my new 1 of April publication and have some fun:
Some Programming Approaches to "Neuro-Linguistic Programming".
Participation in this game in Comments and Discussions is especially encouraged.

Thank you.
—SA
 
Share this answer
 
Comments
softwaremonkey 1-Apr-15 15:05pm    
Although it goes against the Windows concept, my app is the ONLY app that is running and should always have all key presses sent to it when it is running. I have a keyboard hook to prevent some keypress combinations which can upset things (ALT-TAB etc) but every now and again something causes my app to loose keyboard focus. Would system modal do the trick maybe?
Sergey Alexandrovich Kryukov 2-Apr-15 0:14am    
No. You should not assume your application is only one; this is not even about the concept, this is about sanity. My advice will work for you. And I don't know a particular reason for loosing focus. If your windows looses activation (but does it? when? you will detect it, anyway, by handling the event), it will loose focus unless you don't care about this, as I advise. Use my advice, that's it.

Also, understand this: there are 3 different things, related only indirectly: the order of window in Z-order, being it top or not; 2) active state of the window; 3) focus. Understanding of this will lead you to the solutions in all other cases.

—SA

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