简体   繁体   中英

How to give a text box focus from a Windows HotKey event

I have a WPF app which has a very small window. It sits on a desktop but is really small. I have set up through the RegisterHotKey Win32 API a hot key of AltGr+A or Ctrl+Alt+A.

The problem I have is that when I am in another program and use my shortcut the hotkey works and my window comes alive through the winProc and I give a text box on the window focus I also use Keybord.Focus(textBox) .

My intention is that the user can then start typing immediately. Unfortunately unless I am really very quick at letting the the key-combination go, the window/textbox looses focus.

I tried to stop the text box from loosing focus but this does not stop my window or whole app from losing focus which is what is happening.

How am I suppose to do this?

I found that it depends on the other programs that are currently running and that have focus/are active. Effectively when the hotkey is pressed, my App is trying to steal focus which is not something that is really wanted. I have found that Microsoft's own office programs really hate having focus being stolen.

My solution was to use the Win32 API to set my app as the Active one. But as I said this will not always work. There are many articles on this but here is one on the topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM