Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 darkal · May 29, 2017 at 03:52 AM · c#uikeyboardkeycode

i can assign all keys but the shift any ideas

so code works fully thats not problem no errors or anything but i cant assign a key to use shift all other keys work on keyboard and can be assigned

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class keysetting : MonoBehaviour {
 
     private Dictionary<string, KeyCode> keys = new Dictionary<string, KeyCode>();
 
     public Text up, right, left, down, run, jump, interact, hideui;
 
     public GameObject menu;
     private bool disablemenu;
 
     private GameObject currentKey;
 
     private Color32 normal = new Color32(125, 3, 3, 255);
     private Color32 slected = new Color32(239, 116, 36, 255);
 
     public float speed = 5f;
     public GameObject character;
 
     //public CharacterController movementCode;
 
 
     // Use this for initialization
     void Start () {
         
         //not saving
         
 
         //working
         keys.Add("Up", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Up", "W")));
         keys.Add("Right", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Right", "D")));
         keys.Add("Left", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Left", "A")));
         keys.Add("Down", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Down", "S")));
         keys.Add("Run", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Run", "Q")));
         keys.Add("Jump", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Jump", "Space")));
         keys.Add("Interact", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interact", "E")));
         keys.Add("HideUI", (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("HideUI", "Escape")));
 
         up.text = keys["Up"].ToString(); 
         right.text = keys["Right"].ToString();
         left.text = keys["Left"].ToString();
         down.text = keys["Down"].ToString();
         run.text = keys["Run"].ToString();
         jump.text = keys["Jump"].ToString();
         interact.text = keys["Interact"].ToString();
         hideui.text = keys["HideUI"].ToString();
 
         //movementCode = character.GetComponent <CharacterController> ();
     }
     
     // Update is called once per frame
     void Update () {
 
         //Movement Keys
         if (Input.GetKey(keys["Up"]))
         {
             //move action
             character.transform.localPosition += character.transform.forward * speed * Time.deltaTime;
             Debug.Log("Forword is pressed");
         }
         if (Input.GetKey(keys["Right"]))
         {
             character.transform.localPosition += character.transform.right * speed * Time.deltaTime;
             //move action
             Debug.Log("Right is pressed");
         }
         if (Input.GetKey(keys["Left"]))
         {
             character.transform.localPosition -= character.transform.right * speed * Time.deltaTime;
             //move action
             Debug.Log("Left is pressed");
         }
         if (Input.GetKey(keys["Down"]))
         {
             character.transform.localPosition -= character.transform.forward * speed * Time.deltaTime;
             //move action
             Debug.Log("Down is pressed");
         }
         if (Input.GetKey(keys["Run"]))
         {
             speed = 10f;
         }
         else
         {
             speed = 5f;
         }
 
             //MenuKeys
         if (Input.GetKeyDown(keys["HideUI"]))
         {
             //menu action
             disablemenu = !disablemenu;
             menu.SetActive(disablemenu);
             Debug.Log("HideUI is working");
         }
     }
 
     private void moveForward(float speed)
     {
         transform.localPosition += transform.forward * speed * Time.deltaTime;
     }
     private void moveBack(float speed)
     {
         transform.localPosition -= transform.forward * speed * Time.deltaTime;
     }
 
     private void moveRight(float speed)
     {
         transform.localPosition += transform.right * speed * Time.deltaTime;
     }
 
     private void moveLeft(float speed)
     {
         transform.localPosition -= transform.right * speed * Time.deltaTime;
     }
 
     void OnGUI()
     {
         if(currentKey != null)
         {
             Event e = Event.current;
             if(e.isKey)
             {
                 keys[currentKey.name] = e.keyCode;
                 currentKey.transform.GetChild(0).GetComponent<Text>().text = e.keyCode.ToString();
                 currentKey.GetComponent<Image>().color = normal;
                 currentKey = null;
             }
         }
     }
 
     public void ChangeKey( GameObject clicked)
     {
         if(currentKey != null)
         {
             currentKey.GetComponent<Image>().color = normal;
         }
         currentKey = clicked;
         currentKey.GetComponent<Image>().color = slected;
     }
 
     public void SaveKeys()
     {
         foreach (var key in keys)
         {
             PlayerPrefs.SetString(key.Key, key.Value.ToString());
             //saves each key
         }
 
         PlayerPrefs.Save();
     }
 }
 
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Genei_180 · May 29, 2017 at 04:33 AM

Maybe you Could use

 If(Input.GetKeyDown(keycode.shift){
         //stuff you want to do
 }
  

If you want to toggel with the shift key you would need a bool that you toggel Like:

Bool toggel;

 If( ... ) {
        toggel = !toggel;
 }

I hope i could help you with my rather not so expert advice

May you have no Errors in your code

Comment
Add comment · Show 7 · 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 darkal · May 30, 2017 at 02:41 PM 0
Share

no the code is made to change a string into a keycode it allows players to have custom keys problem is shift cant be assigned nor the mouse keys

avatar image Genei_180 darkal · Jun 10, 2017 at 01:17 PM 0
Share

Because the $$anonymous$$eycode doesn't get Returned? Seems Like i have the same Question as You Sry man im Not the best Programmer but i don't get the Problem why shift or $$anonymous$$ouse would no work? Because $$anonymous$$eyCode.LeftShift or $$anonymous$$eycode.RightShift do exist Also do $$anonymous$$eyCode.$$anonymous$$ouse0 1 2 3 etc...

avatar image Genei_180 darkal · Jun 28, 2017 at 01:19 PM 0
Share

Sry but i dont get how your keys get Assigned? You klick on the Aktion you like to assign on the UI and the the Application listens for $$anonymous$$ey Inputs?

avatar image CraftedGaming · Jun 10, 2017 at 01:30 PM 0
Share

you can alternatively have a function that call the shift and you can get it to return the keycode that you want

avatar image darkal CraftedGaming · Jun 11, 2017 at 03:00 AM 0
Share

that may work but wont that cause issues if i add more to the code?

avatar image CraftedGaming darkal · Jun 11, 2017 at 07:29 AM 0
Share

well considering that you're just going to call a function that contains this. You will save yourself from writing lots of repetitive lines of code by simply calling the function

Show more comments
avatar image
0

Answer by Killerbro389 · Jun 16, 2017 at 08:36 AM

Just saying the key for shift is KeyCode. LeftShift and KeyCode.RightShift.

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
avatar image
0

Answer by DiGiaCom-Tech · Jun 16, 2017 at 05:40 AM

It's KeyCode.LeftShift or KeyCode.RightShift ;)

Code would be something like...

 if (Input.GetKey(KeyCode.LeftShift ) && Input.GetKeyUp(KeyCode.A)) {
      do whatever;
 }

https://docs.unity3d.com/ScriptReference/KeyCode.html

Comment
Add comment · Show 1 · 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 darkal · Jun 20, 2017 at 03:55 AM 0
Share

code has to turn that into string then back into keycode that is the problem it little harder but it allow for keybinding so every player has there own choices it hard code to do it works for every key except shift and mouse buttons which haven't figured out why

avatar image
0

Answer by Killerbro389 · Jun 16, 2017 at 06:00 AM

@DiGiaCom-Tech yeah that works I think

Comment
Add comment · Show 3 · 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 Killerbro389 · Jun 16, 2017 at 06:03 AM 0
Share

But why are you using custom names for key codes. You could just use GetButtom ins$$anonymous$$d of Get$$anonymous$$ey; the name will be whatever you assign.

avatar image DiGiaCom-Tech Killerbro389 · Jun 16, 2017 at 01:44 PM 0
Share

$$anonymous$$ost games these days use a combination of keys and buttons. In his case he is creating a user interface (UI) for mapping keyboard keys to game functions. This is so a player can map keys like I, $$anonymous$$, J, L to player movement vs. the standard W, S, A, D keys.

Yes, he could use Input.GetButton("Whatever") but this would not solve the remapping issue as now the Input item for "Whatever' still needs to have it's keys reassigned.

That being said, it is a better way to go as the game inputs are now named (like "$$anonymous$$oveLeft" or "FireWeapon") so you don't have to refactor code throughout your whole application when 'Ctrl-A' becomes 'Alt-B' ;)

I know most of us prefer to write our own code but in some cases it might be a better idea to look at an asset ... I use 'Rewired'. That is, why spend hundreds of hours creating a solution when there are several plugins available in the Asset Store. I do realize that cost might be a factor for some hobbyists but I'd rather focus my work on my game mechanics that foundation stuff like this ;)

[Asset Store - Rewired:] https://www.assetstore.unity3d.com/en/#!/content/21676

avatar image darkal DiGiaCom-Tech · Jun 20, 2017 at 03:43 AM 0
Share

the issue is more so that the keybinding will play a much larger part in the game which is why its important that i can map the shift keys and the mouse keys I am not 100% sure why its not doing it but i may have no choice but to set them as unchangeable keys and simply allow task to still be mapped aka shift is always sprint key but you can also make it Q i really didn't want to go that route though i rather have all the keys be able to be used and mapped

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

10 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

Related Questions

Force UI Input Field Character limit with C Sharp 0 Answers

How to get Key with KeyCode.Question? 1 Answer

KeyCode to Ui ? for mobile 1 Answer

Unity 2d select game object with keyboard 2 Answers

Help with Virtual Keyboard 0 Answers


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