Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting input while out of focus

I've never actual asked a question on Stack Overflow, though just about every programming question I've ever had I found an answer here. Sadly today is not one of those days.

I've spent all day Googling, browsing the win32 api, and trying all sorts of things, but I just can't find the damn answer I'm looking for.

Here's the situation: I've got a Unity project I've created, it uses input from a joystick, there are other components to this project outside of unity, but that's irrelevant, the base of my problem is this: How can I make an application continue to accept input while it's out of focus/Inactive.

I know for a fact that it can be done because Borderlands 2 can do it (I found that out yesterday when I first started trying to find an answer, which was aggravating because that proves there is a way, and I'm failing to find it).

At the very least if someone knows how to do this for mouse or keyboard, that would be helpful enough to allow me to figure out how to apply it to a joystick.

Some helpful details: I'm an avid and experienced programmer, and I've been coding in: C/C++, C#, Java, and a number of scripting languages for many years. It is what I do as a hobby, for my job, in my spare time, while I'm asleep...

Things I've done / tried:

  • using SetFocus + SetActive from user32 & passing null as the window losing focus
  • adding the WM_KILLFOCUS to the ignore list of the other window I want to read input while in background
  • using sendmessage to trick the other window into reading input
  • overriding the inputstream of the application (but this was useless sense I couldn't figure out how to send joystick input over the stream, and also because It still didn't solve the problem sense if that other application was out of focus (i.e. I open notepad), input would stop.

  • I also searched for a way to make a Unity standalone game/project/application accept input while in background, but that yielded nothing.

like image 384
Destects Avatar asked Oct 21 '22 03:10

Destects


1 Answers

I don't know if there is a Unity-related solution, but you can use the RawInput API to receive events directly from the Mouse/Joystick/HID hardware regardless of whether your app is in focus or not.

like image 80
Remy Lebeau Avatar answered Oct 24 '22 12:10

Remy Lebeau