-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking Issue: Sync key press/release with window focus #1272
Comments
This is a problem on Windows as well. I wouldn't be surprised if we had this issue everywhere. IMO the simplest solution is to automatically send a |
Is this really an issue winit needs to solve? I feel like applications should be expected to handle this, if appropriate, by responding to |
It could be opt in perhaps, by setting a flag or so, or opt-out? |
Automatically sending |
@Rua IMO, when a window loses focus, we should send a @murarth If we are expecting users to listen to the |
@goddessfreya I'm not suggesting that application developers should use On the other hand, I can't really think of a use case where an application would want the current behavior. And if there's only one "correct" way for applications to workaround this issue, it would make sense for winit to exhibit that behavior itself. Anyway, if the consensus is in favor of fixing this, I concede the point and will implement this for X11. |
@murarth I also can't think of a use case for the current behavior. If some applications don't want these artificial events and wish to maintain existing functionality, I think they should use I'm honestly not sure which applications would want the existing behavior, tho. I think most people assume, or at least I did, that when a window looses focus all the keys held will be released, and vice versa for when focus is regained. At the bare minimum, I imagine most people believe that for every key up they should receive a key down, and for every key down there will be a key up (if the user releases the key). I'm not sure what complications might result from us injecting the occasional key up/down here and there, but if that's a concern, we can mark some EDIT: As an alternative implementation, when a window regains focus, we could just issue all the necessary key ups/downs. This strikes me as slightly harder to implement on winit's side and less useful than my other proposed solution. |
Oh this is a big falsehood right there. Let's take key repeat, like holding a, you'll get a lot of keydown's but only a single key up (if even then). |
@OvermindDL1 Maybe I was not being clear, but this is consistent with what I said. If they held down |
Eh, that's not always the case either. The events from the hardware don't work that way, and thus neither do any window manager I've seen as well, there are always unexpected things. I personally do not want magical keydowns to be sent if focus is lost, I can handle that if I so wish, and in some cases I do, and in some cases I don't. I don't want fake input events. Opt-in maybe, but not opt-out. It should match the expected OS events by default, no fake events by default. |
I've submitted PR #1296 to address this issue on X11. The PR adds a field |
I've had some time to think on this and I think the three things most common things applications want with regards to keyboard input are the following: 1- I want the user to type in some text into something => use If we inject some artificial events into However, I think this still leaves a major question to be answered: should all keys be marked as released when a window looses focus or should the keys be updated when the window regains focus? Now, originally I was leaning towards the former, however, now that I've thought about it some more, I propose the following: 1- A Applications only interested in use case 2 can ignore all the synthetic events and maintain the current functionality. |
I don't see any significant advantages to reporting synthetic key events only on window focus and only for keys that have changed state since loss of focus. Plus, it would mean added complexity of tracking keyboard state on a per-window basis. Persistent keyboard state tracking, if it is done at all, I think, should be done on a global basis, as this is how the platform understands this state (at least on X11). |
@murarth I like the synthetic field, easy to match on so we can only get 3 from @goddessfreya seems impossible on some if not most systems (mobile, web, wayland I gather, etc...), you just won't be able to get key state like that, plus that's not how most interfaces work where you get events on change, not current 'state' (if that even makes sense in many contexts). Most OS's make it very very difficult to get keyboard information when not focused to prevent things like reading passwords (though that does bring to mention that many desktop OS's makes this particular security vulnerability very easy, but that's not the case everywhere). Events are the only thing that are guaranteed to work everywhere and that's what should be done as it is most capable as well (key polling is always a bad idea in my opinion). |
I don't know about other platforms, but on Wayland when you gain focus you are also notified of a list of keys that are currently pressed, so the typical behavior of a wayland app equate the loss of focus with an implicit release of all keys, and reload the pressed keys when it gains focus again. |
On MacOS this appears to be a non-issue. If a key is pressed, the window loses focus, and then the key is released, then the OS still sends a keyUp event. The OS must be doing something like tracking which view last received the corresponding keyDown event. In the opposite case, the OS also doesn't report "keyUp" events if the key was pressed outside the view. This appears to be true for mouse events as well. |
@kettle11 That's good to know, thanks! I've marked off macOS on the list. |
Be aware that I've removed this code from the X11 backend in #1932 for the following reasons:
|
For your info, I've just stumbled into this again with #2349: synthetic For what it's worth, I'd like to add my own experience on this issue, mostly from KAS:
Which brings up another issue: |
I still don't quite understand the original issue and why we need anything like that in winit(synthetic keys). Though in general we can probably approach it in a different way, like having We already have something like that for pointer, but more in a nature |
Well, uniform behaviour across platforms would be a reason: see above. But in that case, we shouldn't generate synthetic key-press events, and should not emit (non-synthetic) key-release events if no corresponding press-event was omitted (the latter is less important from my point of view). The principle of least surprise may be another reason: naively, people expect a key-release event after a key-press event. But if it's well documented that such events may be missed if the window loses focus then apps can also find their own way of handling this. On the whole, I don't find the idea of generating synthetic events bad, though synthetic key-down events can be a little unexpected, as seen in #2349. But this behaviour does comes with certain assumptions, namely that key-press events may be used to trigger actions while the state of a key and key-release actions are less important. |
EDIT: I didn't read this properly, this isn't about modifiers, it's about any key. As far as I know that's not even possible on Web as there isn't an API to query individual keys. We would have to track all keys in Winit to simulate that. |
macOS(Tracking Issue: Sync key press/release with window focus #1272 (comment))As mentioned in #1262 (review), however over the window, hold any key, move off the window release the key. You won't get the released key event. Ever.
https://gist.github.com/goddessfreya/39ae1f768cde5bdbc8c9aefa25984824
https://gist.github.com/goddessfreya/0192aa8d9920dba7c933c98190c42d90
cc @murarth
Related: #1077
The text was updated successfully, but these errors were encountered: