Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cr1t1cal · Aug 16, 2012 at 11:15 PM · stringconvertgetkey

Converting a string to keycode

I am in need of a way to take a string(or character) and convert it into its appropriate KeyCode enum.

Ultimately, I'm trying to read in a file containing key bindings and then use the parsed information to set my GetKey calls. The way I'm wanting to do this right now is to set variables with their related functions, like "forward" for example to move the character forward. I then want to take this variable and use it in my GetKey calls (GetKey(forward), for example).

Is there an easy way to take a character (like 'w') and set it to a KeyCode for use as a variable?

This is what I'm currently doing:

 if(Input.GetKey(KeyCode.W)) {
     accelerationZ = accelerationRate;
 }

This is what I need it to be:

 if(Input.GetKey(forward)) {
     accelerationZ = accelerationRate;
 }

Any suggestions would be helpful! I'm willing to redesign my approach if needed. I just need some way to read in a file and use GetKey on the bound characters.

EDIT: I think I can instead just store the enum integers instead of a character in my key bindings file, however I still need to somehow store this in a variable and also retrieve the key corresponding to the enum for display in the game.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Aug 16, 2012 at 11:30 PM

You want this I think: http://msdn.microsoft.com/en-us/library/essfb559 Enum.Parse

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cr1t1cal · Aug 16, 2012 at 11:32 PM 0
Share

This might work. I'll give it a try.

avatar image Mizuho · Aug 17, 2012 at 12:12 AM 4
Share

I really think this should say "Want this, I think you do".

avatar image
1

Answer by Muuskii · Aug 17, 2012 at 12:08 AM

You could save yourself some trouble by looking at Custom Inputmanager v1.4

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Aug 17, 2012 at 06:49 PM 0
Share

That thing looks pretty good! $$anonymous$$uch like my old friend Unreal's input manager. Looks like a 2.x version for sale too.

avatar image Muuskii · Aug 17, 2012 at 07:00 PM 0
Share

I haven't gotten around to testing it quite yet, but if it's GUI isn't satisfactory I'm also looking into NGUI plugin which apparently has player pref utilities, making it a candidate to integrate with cInput.

avatar image
0

Answer by TheNewNerd · Oct 18, 2016 at 06:14 PM

I found a very basic, but tedious way of doing this. Hopefully this helps someone :) Also if anyone knows how to make this better please let me know, It would be very helpful.

using UnityEngine; using System.Collections;

public class CustomKeys : MonoBehaviour { //Variables public KeyCode Key1 = KeyCode.D; public string KeyA;

 // Update is called once per frame
 void Update () {
     //This checks if "KeyA" = "a". If it does then it changes "Key1" to = A aka (KeyCode.A)
     if (KeyA == "a")
     {
         Key1 = KeyCode.A;
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert type `UnityEngine.Collider' to `string'? 1 Answer

Convert Array to String 3 Answers

How to convert a list of Resolution to String 2 Answers

GetComponent and converting string to Type type 1 Answer

Converting 'char' to the name of a variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges