Open beauwest opened 9 years ago
To clarify, I'm not sure that this is something that can, or should be fixed without radically changing the behavior of Keypress.js. Someone much smarter than me will have to think it through.
If you take a look at the source file, you'll see I had something similar for handling the user alt+tabbing:
@blur_event = attach_handler window, "blur", =>
# Assume all keys are released when we can't catch key events
# This prevents alt+tab conflicts
for key in @_keys_down
@_key_up key, {}
@_keys_down = []
I wrote that back in the 1.0 version before you could bind to any object, so I think if I just change that 'window' to the element we are bound to, it should work. It's slightly buggy though since you could still have that key held down when coming back, but I think it will handle most use cases.
Thanks for catching this!
Yeah, I did see that, and I'm manually calling that function blur_event()
when the element gets blurred. However, would that work in every case? In the same web app, I have an instance where I do something similar to alt+tabbing inside the browser window. It cycles through elements. And when each element is cycled to, it gains focus.
If blur_event
was added to the element your listener is bound to, bubbling wouldn't work as expected. At least I think it wouldn't.
I really think that this is a pretty obscure corner case, I mean, I'd love to see it fixed, but I think a lot of other scenarios would break if blur_event was called whenever the element we are bound to is blurred.
I could be crazy though.
So I have a bit of a weird issue that took me forever to track down. The use case is this:
newElement.focus()
)This works great, except in one circumstance. If the new element takes focus before the
keyup
event happens, the Keypress._keys_down array still shows that there is a key pressed! This causes issues for any other hotkeys that haveis_solitary
set totrue
.Clear as mud?
The solution that I have implemented is, when the original element gets a regular blur event. I call
listener.blur_event()
which clears the array for when I come back to it.If you need me to clarify further, or if there are any questions, please let me know!