KeyUp event not firing until I ALT+Tab away then back to my application
I am experiencing a very strange issue with my Windows Forms form. As stated in the title, the KeyUp event is not firing i开发者_运维问答n the application at all until I alt+tab away from the window, then back. Doesn't matter where I click in the application before switching away; nothing happens.
Also if I alt-tab away from the application, but click on the app's task bar to switch back, then I still don't get KeyUp events. Clicking away, then alt-tabbing back, makes the KeyUp event function properly again.
Seems I only get KeyUp events if I Alt-Tab TO the application - but not in any other case.
The KeyPreview property is true
. I'm not doing any other key processing in the application anywhere - just in the KeyUp event.
I really need this application to be able to respond to KeyUp events, especially just after startup - I don't want to instruct my users to alt-tab away then back just to have keyboard input function properly. Has anyone seen this issue and found a workaround?
Update: I've tinkered around with this a bit more, and I found a workaround - but it's definitely a kludge and I don't like it. In the OnShown
override for the form, I added a call to this.Focus();
. I also added an event handler for this.LostFocus
that also calls this.Focus();
. This is the only form in this application, so none of that is a problem - I just wish I understood why this is happening. =( Ah well.
I added this as an update instead of an answer because, well, this isn't an answer. Bleh.
Perhaps this helps: http://support.microsoft.com/kb/828964
I've overridden the form's ProcessCmdKey() method in order to 'receive' the accelerator keys in the form first.
I ended up giving up on using form events - I'm now registering a global Windows hotkey for the key sequence I want to trap, and that's working just fine. It doesn't matter in my case that the hotkey is global, as my application is designed to be the only one in use on this system.
精彩评论