- Home /
Input.GetKey question
Okay I'm going to attempt to ask this question again, does Input.GetKeyDown(KeyCode.F)){ } work on all 3 (Windows, Mac and Linux) or is there some other way I have to do this between each platform?
This is the third time trying to ask for help and I really urgently need to know as I seriously can not whatsoever find anything online about it, and being I don't own a Mac or Linux to test this I have no other way of finding out.
Thanks.
Answer by JoshuaMcKenzie · Mar 07, 2016 at 07:32 AM
For this specific set up, yes. The Input class entire purpose is to give cross-platform support while minimizing the impact to your code. So for all standalones, this will work just fine.
There are some cross-platform caveats tho with this specific line of code. GetKeyDown will only work for standalones (unsure if it works for mobile keyboards but then again its a bad idea to rely on a those for most mobile games). Also this is hard-coding the hotkey which disallows the player from changing their key setup.
Instead to provide easily configurable and fully cross-platform support you should fill out the Input Manager (Edit>Project Settings>Input) with the input slots relevant to your game. so lets say that you were using "F" to open inventory before, you'd then create an input slot named "Open Inventory" with the "F" key mapped to it. but now you can also map a joystick setup to it as well so that you could use an XBox controller to open the inventory via the "x" button.
From here instead of using Input.GetKeyDown(KeyCode.F) which is hard-coded and works only for keyboard events, you use Input.GetButtonDown("Open Inventory") and that will work on all platforms, keyboard or mouse, and even plug-in controllers (xbox, playstation, or whatever). The player can choose to map the hotkeys around to their liking.
the getButton functions can really minimize the amount of code you have to check for since you can actually map multiple hotkeys to the same input slot ( for example, in my game I remapped horizontal and vertical to both wasd and the arrow keys).