Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

KeyCodes return

Discussion in 'Scripting' started by Larz Caetano, Jul 7, 2015.

  1. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    Hello, guys. I don't know what a KeyCode return.
    It returns a byte or a key (like A, B, C, D, E...)?
     
  2. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
  3. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    Thanks :>

    Then, can i return a Key that is pressed? A key code or a key byte?
     
  4. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Not sure what you're getting at. Do you mean to have your method return it? What do you mean by key byte?
    Perhaps you can provide some context to what you're trying to do?

    EDIT: After a quick look into UnityEngine.dll the KeyCode enum definition:
    Code (csharp):
    1. public enum KeyCode
    so the elements are of default int type and not byte if that's what you were asking.
     
    Last edited: Jul 7, 2015
  5. massey_digital

    massey_digital

    Joined:
    Apr 28, 2013
    Posts:
    94
    Last edited: Jul 7, 2015
  6. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    YES!
    That's what i was looking for. I'm trying to just assign KeyCodes with byte, but now i see they are integers.
    How can i return them? Is there a list of those integers?
     
  7. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Enums can be casted to an integer. See the example section here:

    https://msdn.microsoft.com/en-us/library/sbbt4032.aspx

    Edit: Removed assumption that Unity didn't assign random values to each enum member.
     
    Last edited: Jul 7, 2015
  8. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    But you probably don't need to cast or store the values as int (unless you really do but probably not). For example to have a method that returns value of keybaord button that you want to be "walking forward" button you would have this method return KeyCode type:
    Code (csharp):
    1. private KeyCode GetForwardKey()
    2. {
    3.     return KeyCode.UpArrow;
    4. }
    For list of KeyCode enum elements check the KeyCode documentation.

    If you really want to cast to int and need to know the int values of elements before the fact (at runtime you can get the int value of specific element by casting to int as suggested) you have to take a look inside UnityEngine.dll where it is defined to find:
    Code (csharp):
    1.  
     
    Last edited: Jul 7, 2015
  9. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Why?
     
  10. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Not sure what the question is exactly?
    The code is not mine. It's extracted from UnityEngine.dll - it's the actuall definition of KeyCode.
     
  11. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    Ok, that's so helpful. Thanks to everyone. :D
    But how can i return the Key that is Pressed?

    Code (CSharp):
    1. void GetPressedKey ()
    2.     {
    3.         return Input.GetKeyDown (KeyCode);
    4.     }
    Something like this?
     
  12. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    To check for specific key like Up Arrow:
    Code (CSharp):
    1. void GetPressedKey ()
    2. {
    3.    return Input.GetKeyDown (KeyCode.UpArrow);
    4. }
    If you don't know what button you are looking for and need something like GetAllButtonsDown then the Input class doesn't seem to provide this functionality: http://docs.unity3d.com/ScriptReference/Input.html

    There is property to check if any button is down: http://docs.unity3d.com/ScriptReference/Input-anyKeyDown.html

    Otherwise what you would have to do is to have a list of all buttons you want to monitor (all buttons that do anything in your game for example) and then in Update call GetKeyDown in loop for each one of them and react accordingly.
     
  13. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  14. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    Ok, let me see if i undestand. You guys are saying that i will have to do all the keys?

    Like : if (Input.GetKeyDown (KeyCode.W)) { key[119] = true; }

    ?
     
  15. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    What exactly is it you're trying to achieve?
     
  16. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    A system that change variables value when a key is pressed.
    I was doing an "if" statement for all the KeyCodes, but i want to do a function for all, like, when the key is pressed, the function get the key pressed and find a variable that have the same value (like a bool called 'key119', and the key pressed is the key 119 [w]), so the variable will be activated/deactivated.
     
  17. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    So you're trying to flip an index in an array of booleans based on what key is pressed?

    You could do something like this, assuming keys is a boolean[] that is initialized to a length of whatever the maximum integer value is contained in KeyCode

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     var e = Event.current;
    5.     if (e.isKey)
    6.     {
    7.         keys[(int)e.keyCode] = true;
    8.     }
    9. }
    10.  
     
    Pirs01 likes this.
  18. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    What needs to happen to OnGUI () get called?
     
  19. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
  20. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
  21. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    Ok, i'm sorry Pirs01, that's because i searched and i didn't found it.
    So the OnGUI () is, basically, an Update () or FixedUpdate ()?
     
  22. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    How can i check if the Key is released?
     
  23. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sort of. It is called at least twice per frame, and twice more for every GUI event that occurs. You can check which event is occurring by using Event.current. its a painful way to work, but appears to be the only way to listen for an arbitrary key press.
     
  24. Larz Caetano

    Larz Caetano

    Joined:
    Jul 7, 2015
    Posts:
    15
    Thanks for everyone who helped me. It's being pretty hard to learn, but thanks so much, guys :D