Detecting Input Without Window Focus (Listen For Input In The Background)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Baggef
Prole
Posts: 20
Joined: Fri Aug 23, 2019 7:30 pm
Location: USA
Contact:

Detecting Input Without Window Focus (Listen For Input In The Background)

Post by Baggef »

Is it possible to detect input while the window is not focused?

I've tried using love.window.hasFocus(), and it did update while the window did not have focus.

Code: Select all

function love.update(dt)
	if love.window.hasFocus() == false then
		print("No Focus")
	end
end
But detecting input while in the background doesn't seem to work with hasFocus()

Code: Select all

function love.update(dt)
	if love.window.hasFocus() == false and love.keyboard.isDown('k') then
		print('k')
	end
end
Any help is greatly appreciated.
gaeem defelpmint is maiy pashin
Itch: Freakman
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Detecting Input Without Window Focus (Listen For Input In The Background)

Post by zorg »

No, it's not possible through regular means.
(Edit: It's not completely impossible, but achieving it requires os-specific lower level workarounds)
Last edited by zorg on Tue Sep 17, 2019 5:07 am, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Detecting Input Without Window Focus (Listen For Input In The Background)

Post by raidho36 »

SDL is a gaming-centric framework and games are not meant to capture input while in background. Implementation-wise, SDL polls input from a window, if it loses focus then there's no input events.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Detecting Input Without Window Focus (Listen For Input In The Background)

Post by pgimeno »

The whole concept of focus is about where to send input events, isn't it?

Imagine you are typing in an editor, then change window to a web form, and what you type in the web form is typed in the editor. [EDIT: Or that you press F1 for help in one application, and every other open application opens the help.]

It is possible for certain applications to receive certain inputs when not focused, for example a music player in the background can receive multimedia keys, but as raidho implied, SDL is not prepared for that.
Last edited by pgimeno on Tue Sep 17, 2019 11:41 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Detecting Input Without Window Focus (Listen For Input In The Background)

Post by zorg »

I faintly remember someone saying that you can still detect joystick/gamepad stuff even when unfocused though, if not keyboard/mouse.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Detecting Input Without Window Focus (Listen For Input In The Background)

Post by pgimeno »

Probably this:
https://love2d.org/forums/viewtopic.php?f=3&t=85417

The OP asked about key events specifically, though.
Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests