- Home /
Check for any input
How would I go about checking for any input whatsoever? Input.anyKey returns true if a key or mouse button is pressed but not if the mouse wheel is scrolled. I'd like to know if there is a variable/function call that can tell me when the user has performed any form of interaction with my game. Whether it's a key press, mouse click, mouse movement, touchscreen press, finger movement, etc.
For context, what I'm essentially implementing is a "sleep mode" that temporarily disables rendering to when the user is idle but immediately wakes up when any interaction is detected. This is to save battery on mobile devices.
you could use || to include all the different inputs.
I understand that but my issue is that there could be an input type that I miss. I'm developing for many different platforms so might forget/overlook something. The reason I asked this question is that I had code similar to what you suggested and, during testing, I tried scrolling the mouse wheel and it was not detected. To detect a mouse wheel scroll will require me to make a new variable to check if the axis value is different on the current frame to the last frame. This will make my code bloated especially if there are other inputs that are missed. I'd much prefer to simply have: if (Input.AnyInput) { WakeUp(); }
I'd be a bit surprised if there were such a thing.
Obviously, don't know your exact needs, but on the face of it your use-case seems strange to me.
1. Having to actively choose to wake a device/app up after it goes to sleep is pretty standard. So is there really a big advantage to having any interaction wake it up (as opposed to a "Resume" button for instance)?
2. Why go to the trouble, when people can manage their own battery life? Chances are their phone or whatever will have gone into sleep mode anyway.
Hi, I don't think I was super clear. "Sleep Mode" keeps the screen on and the app running, it just uses the OnDemandRendering class to reduce framerate to 1fps. It's essentially for when the screen becomes static so anything more than 1fps is wasteful. My app allows users to rotate and zoom in on a 3d model but when they aren't directly doing that it's essentially just a static image.
Ah ok, fair enough. And your sleep mode kicks in earlier than people's own battery-saving stuff would...?
But to flesh out my first sentence a bit more... input devices are (architecturally if not physically) peripherals. So I'm not sure that it'd be feasible or desirable to have a function call of the sort you're suggesting in the engine. To maintain a core function that truly reported on all input devices, anyone who added a plugin for a new input device would have to somehow be compelled to register with it.
And what inputs are inputs? I'm working mostly in VR - is headset movement an input or not. Answer - for some apps, maybe. What about a phone's gyro/accelerometer inputs? Again - for some apps. And if they are you'd probably want to only consider such an input a "wake-up-worthy" input if it exceeded some threshold anyway.
So for me it makes sense to leave this stuff to the developers of specific apps to decide which inputs they care about.
Answer by RKadeZone · Sep 07, 2021 at 03:32 PM
Not sure this is possible, reference this https://forum.unity.com/threads/input-manager-detect-any-key.13607/#post-94769
However, you should be able to get what you described in a single if check by or'ing the calls together.
if (Input.anyKeyDown || Input.GetAxis("Mouse ScrollWheel" || ..)
{
// code
}
Note: Unity doesn't provide a way to detect finger swipes afaik, but you can accomplish that in three lines of code shown here: https://answers.unity.com/questions/1572835/moving-object-with-finger-move.html
Your answer
