Skip to content
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

Keyboard and mouse button states are lost when love.window.setMode() is called (11.5 on Windows) #2017

Closed
AverniteDF opened this issue Feb 8, 2024 · 4 comments
Labels
library dependency Related to a library used by LÖVE Windows

Comments

@AverniteDF
Copy link

I've been noticing some buggy keyboard and mouse behavior occurring after love.window.setMode() is called (this is with 11.5 on Windows). What happens is that Love2D 'forgets' all of the keys and mouse buttons that were being held down at the time setMode() was called. I know that the window gets a new Windows handle when setMode() is called--could that be the cause? It's problematic because Love2D invokes love.keyreleased() for all the keys that were being held down (even though they're still being held down). This issue also causes problems with the mouse buttons, Love2D gets confused because it doesn't know the correct state of the buttons and it doesn't invoke the pressed/released callbacks as it should. Mouse setGrabbed() also fails to confine the pointer to the window client area if you hold down any of the mouse buttons during setMode(). Again, could the change of the window's hWnd be the root cause of all these issues? I know a lot of Win32 API calls rely on that handle.

@slime73
Copy link
Member

slime73 commented Feb 8, 2024

You'll run into a bunch of issues if you recreate the window inside an input press event. I recommend doing it on release instead.

@slime73 slime73 added the library dependency Related to a library used by LÖVE label Feb 8, 2024
@HDPLocust
Copy link

HDPLocust commented Feb 12, 2024

It might also be a good idea to use an event bus, for example:

function love.changescreenmode(width, height)
  love.window.setMode(width, height)
end

function love.keypressed(key)
  if key == "f" and love.keyboard.isDown("lctrl") then
    love.event.push("changescreenmode", 800, 600)
  end
end

The event execution will be delayed until the next frame, so you can collect everything what you need in current.

@AverniteDF
Copy link
Author

I think SDL assumes that all keys/buttons are in an unpressed state when it creates a window because any keypress that happened prior to the window being created is considered to be outside the scope or "jurisdiction" of the window. The input event system ignores keypresses and mouse clicks that happen outside the window and the isDown() states need to correspond with the events received. However, in the case of subsequent windows being created (such as toggling fullscreen) maybe the handover from one window to another could be handled better because even though it's a new window there is (or should be) continuity in the application.

Something that might not be too difficult to fix: When you hold down a mouse button in relative mode (with grabbed pointer) during a love.window.setMode() call the grab stops functioning and the pointer can leave the confines of the newly created window. I think this can be solved by re-grabbing the new window. I tried this using Win32 API calls and it worked. I'm not sure if this is an issue for Love2D or SDL.

@slime73
Copy link
Member

slime73 commented May 15, 2024

love 12 doesn't destroy and recreate the window when setMode is called, so this shouldn't be an issue anymore with that version.

@slime73 slime73 closed this as completed May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library dependency Related to a library used by LÖVE Windows
Projects
None yet
Development

No branches or pull requests

3 participants