5

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.

ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
Destects
  • 81
  • 1
  • 6
  • Oh one more thing: I really don't want to have to go and use some 3rd party application to accomplish this, that somewhat undermines my whole ordeal. – Destects Aug 15 '13 at 19:00
  • possible duplicate of [How do I send key strokes to a window without having to activate it using Windows API?](http://stackoverflow.com/questions/1220820/how-do-i-send-key-strokes-to-a-window-without-having-to-activate-it-using-window) Although it might not be the answer you were hoping for :( – Neil Neyman Aug 15 '13 at 19:01
  • Nope :/... I looked at that previously, but as mentioned it is in fact possible in some way otherwise my example wouldn't exist... – Destects Aug 15 '13 at 19:06

2 Answers2

3

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I did look into the RawInput API and tried to use it previously, and again just last night, I can't figure out how exactly to push the input to the Unity app and have it properly received. Using SendInput, I attempted to send whatever I received from the joystick to unity, but unity didn't react... Could it be that the service I created to pull the input and forward it was blocked by UIPI ? It's a new day so maybe I'll have better luck than I did yesterday. – Destects Aug 16 '13 at 12:59
  • If you are referring to an actual background service, then it runs on its own desktop session and cannot send window messages to other desktop sessions. But why are you trying t capture input in a separate service and not from in the app that needs the input? – Remy Lebeau Aug 16 '13 at 15:55
  • Did you make sure to enable `Run In Background`? Found at `Edit -> Project Settings -> Player` – Jerdak Aug 17 '13 at 02:16
  • @Jerdak Yup, I think I've figured it out though, I don't think I was properly sending the input to unity, I'm in the process of testing this right now. – Destects Aug 19 '13 at 17:54
  • @RemyLebeau The are multiple parts to the project that each require input from different origins, that's why I'm using a separate app that'll run in the background and push the input to the appropriate part. – Destects Aug 19 '13 at 17:57
0

I really think the answer given by @RemyLebeau is the proper way of tracking the input. If however you're into hacking, you could use GetForegroundWindow, GetWindowThreadProcessId, AttachThreadInput to attach to the active UI thread. But then you'd need to 'pull' the state of input (e.g., with GetKeyState, GetKeyboardState or GetKeyboardState) and track the focus as it switches threads to do detach/attach to a new thread. You can not just wait (say, with GetMessage) for an input message posted to a different thread.

avo
  • 10,101
  • 13
  • 53
  • 81
  • I looked into use attach thread input and stuff, but It's only useful for the keyboard/mouse input, and as you said, you'd have to track the foreground window, and would lose input if no window is active sense you can't attach to a system thread – Destects Aug 16 '13 at 12:28
  • [off-topic] why would you want to steal joystick input while your app is not an active app to the user?[/off-topic] – avo Aug 16 '13 at 15:04
  • Because for example, if you have a setup where there's an external interface, you want to be able to control the app while focus is on the external interface. – Destects Aug 16 '13 at 15:48