views:

185

answers:

3

This is a subjective question, but I need opinions.

I have a WinForms C# application whose window hides itself after a specific keystroke (Enter or Escape), with possible modifiers (e.g. Ctrl-Enter). When hiding on KeyDown or KeyPress, the other application that becomes active after my window hides itself receives the KeyUp event for that keystroke. Normally, it shouldn't affect that other application, but some of them out there react on KeyUp. For example, TweetDeck sends the message currently being edited on "Enter" KeyUp, even if it did not receive KeyDown/KeyPress.

So I thought, fine, I'll be a good citizen, I'll hide on KeyUp. But this doesn't feel right. If I only look for keys up, I'm doing what I blame others of doing! If I try to create an history of matching KeyDown/KeyUp, I'm over-complicating my code (modifiers generate their own key up).

What should I do? What should a well-implemented application do?

+2  A: 

This is a hack, but you can set the state of your program to "pending hide" when receive the key down. And then when you get the key up for that sequence, reset the "pending state" and then hide.

Alternatively, can you just "eat" the key up off the message queue after you receive the key down?

I would not worry too much about applications handling key up rather than key down - like you point out - the only reason this is an issue is because your app changes active windows in the middle of a key down key up sequence. It is your responsibility (IMO) to also "eat" the key up messages. You can probably just handle the key up instead of key down with no adverse side effects.

EDIT

Thinking about this further - when doing alt-tab to go to a new window - the action does not happen until the key up. In the meantime it shows a window of possible apps to change to. You can do similar action and the behavior has a precedent.

So: On key down: Display window that indicates app will hide. on key up: hide window

This is "stateful" - you can only go into hiding if you received the key down and the key up - at least that is what I would do. 99.9999% (guess) not handling key down would be ok.

Tim
But, what if the user presses the key and holds it down for a second? He'll be confused if it doesn't hide on KeyDown. And what are you going to eat of the queue, if the KeyUp isn't there yet?
James Curran
You just do nothing then. Other solutions to this won't take action until key up either...
Tim
Your solution has a similar same issue... the app hides, but nothing happens until the key is released. As I put in my answer - it is a hack.
Tim
In order to handle KeyUp, the window must still be there, thus it means "hide on key up". And I do feel it's awkward to not react on keydown/press for the example given by James.
Martin Plante
A: 

Well, I'd say "Don't worry about it, until it becomes a problem", but I guess it is a problem now....

In that case, I would hide on KeyPress (the expected user experience), but grab the focus until you get a KeyUp (or until a short timeout).

James Curran
"[...] but grab the focus until you get a KeyUp"I can't do that if the only window is hidden. I either stay visible until KeyUp, or live with keyups bubbling up to other apps.
Martin Plante
A: 

I can't think of any program that implements keyboard shortcuts on the KeyUp event. That standard was set a long time ago, with the Windows TranslateAccelerator() API function. It translates WM_KEYDOWN. Windows Forms implements the same behavior with ProcessCmdKey().

Sounds like you found a doozy. Does it handle Alt+F4 correctly?

Hans Passant
The issue is he is doing something outside of normal windows behavior. This s similar to alt-tab behavior. Alt-tab does nothing until the key up.
Tim
TweetDeck is not closing on Alt-F4 up, thank God! It seems only "Enter" is handled in a special way on KeyUp. I'm really faced with "How can I make a bug in another app less annoying to my users?".Tim: I'm not doing anything outside of normal behavior. e.g. Alt-F4 closes any app on key down.
Martin Plante
I meant abnormal in that most apps don't have their own private mechanism for hiding their window... - like alt-tab or minimize or shutting it down. This IS NOT standard behavior. NTTAWWT.
Tim
I've decided there is no clean way to overcome this bad behavior in that other application. Thank you all for your suggestions. This is the best answer in my mind.
Martin Plante