- Home /
Display the set up key for a button
I have a feeling this is going to be obvious and asked before, but I couldn't find a response.
I would like to display a message, like:
Press SPACE to start.
But I want SPACE to be a variable, taken out from the user defined current controls. I got so far:
string.Format("Press {0} to START", ???);
But Input.GetButton("start") has no subinformation what key the user set AFAIK. :(
If your setting it up through the Unity Input $$anonymous$$anager, using "start", then the "subinformation" your looking for is the "positive button" or "negative button" - getting that information, and binding it, Unity handles on launch, in fact im pretty sure the Input $$anonymous$$anager class, is mostly read-only so unless you override it, and have already done that to allow the user to bind keys, it would be better to create your own key binding system.
The good part is, doing so actually isnt too difficult. - Create a new script, for example we can call it "$$anonymous$$eyBinding" - In it, make a public List keybinds = new List(); - NOTE This will produce 2 errors: "$$anonymous$$eyBinds" does not exist, and neither does "List" - at the top of the document, make sure to add: using System.Collections.Generic; - if you are using Visual Studio, mousing over the word "List" will give you this as a suggestion, once you click "Show potential fixes". - The other error is that "$$anonymous$$eyBinds" does not exist, so just under your list, create a: public struct $$anonymous$$eyBinds {}; and in it, put all your public variables - which in your case, for now at least, youll just need a public string name; and public $$anonymous$$eyCode bind;
To find and bind keys with this system, its a little inefficent doing it this way, but it works, you can do something like this, under your Update function: public void Bind$$anonymous$$ey(string name, $$anonymous$$eyCode key){foreach($$anonymous$$eyBind keyData in keyBinds){if(keyData.name.ToLower() == name.ToLower()){keyData.bind = key;}} //a lot more error checking should be done here...
public $$anonymous$$eyBind Get$$anonymous$$ey(string name){foreach($$anonymous$$eyBind keyData in keyBinds){if(keyData.name.ToLower() == name.ToLower()){return keyData;}}
Then to use this script, all you need to do is place it on an object, and set it through the Inspector, then call it via script. A better way is to make it a non-monobehaviour script, and use that as a static script essentially so you can directly access it similar to how you can already directly access the Input class - for this, we can communicate through private message or live communication through Skype or Disord, and I can assist you in creating this script if you would like.
Sorry for the bad formatting as well, as characters are a little limited to go into as much detail as id like, though hopefully this helps you out.
Thanks this is really detailed information and I have a feeling this is the route I will have to take, but then I'll have to disable the launcher, since the binds wouldn't be related to it. :( It's strange that Unity doesn't have a support for this, seems so basic to get information on the key, I would be even happy if I could get all the keys as an object/array and I would search through it on start and nitpick it out for a manageable dictionary.
Thats true, I would suggest doing this, because its easier for gameplay - for example, some games iv played will have something like "Boosters - H" and I can change that and have no clue what that actually does till I use it, then find out and think "ohh ok cool, but I think im going to want that to be Q in the way I play games" - from the launcher, they are only able to bind these keys once - so you can get this info, but neither you, or they can rebind those keys without relaunching the game - and most games today will have key binding changes from within the game. This is why I would suggest this regardless
I came across a resource a while back in which I cant seem to find anymore, though it was quite confusing to understand, and once you did implement it, it virtually did the same thing, all you can do is read from the array, you cant really do any re-binds, though I guess it depends on the kind of game your building, if thats the kind of fixed binding system you WANT.
You could use something like this, but it may not be too helpful to you. It all depends.
Answer by pslattery · Aug 21, 2017 at 07:51 PM
Just to make sure I know what you're asking: You want to know what key is bound to a particular Input Axis. As far as I know there's no way to do this with native Unity code at the moment. The way I see it you have two options:
You could build a custom Input Handler that lets players customize controls and which is used to check Unity's native Input class (This is actually a good idea anyway. If you store this data in a ScriptableObject, you can test out different control schemes easily). This way you have a stored KeyCode that you can tie to a particular action (as opposed to the builtin method of just asking for the value of an axis), and you can just print that as the value.
But that's pretty complicated and might be more than you're looking to do. The alternative is to simply print the name of the axis you want the player to press, and they'll know what key that axis stands in for.
Answer by SuryaEU · Aug 21, 2017 at 07:01 PM
Hello,
You could try to use the KeyCode enumeration to store your variable. And then just call the .ToString() should display what you want.
In your case, it would be :
KeyCode key = KeyCode.Space;
string.Format("Press {0} to START", key);
Check this post for more information
This sadly wouldn't be good, if the player switches keybindings in the unity launcher, and that's what I would like to get info on, as to which key the user bound 'start' to, ins$$anonymous$$d of displaying Space in the message, and it's actually ctrl or something else.
Now I think I completely understand what you are looking for.
Unfortunately, It looks like it's not possible for now, look at this post and at this feature request
Your answer
![](https://koobas.hobune.stream/wayback/20220612133230im_/https://answers.unity.com/themes/thub/images/avi.jpg)