Search Unity

Print to console what key was pressed

Discussion in 'Scripting' started by Troas, Nov 8, 2015.

  1. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    I have been trying to search for something that will allow me to store what has been pressed by the user as a variable.

    I realize that this could be "solved" by making if statements for every key possible but that seems inefficient.

    I've tried several types of code but right now this is what I am sticking with:

    Method #1

    Code (CSharp):
    1.  
    2. void OnGUI() {
    3.  
    4.  Event e = Event.current;
    5.  
    6.   if (e.isKey){
    7.  
    8.    string key = e.keyCode.ToString();
    9.  
    10.    Debug.Log(key);
    11.  
    12.    print ("Hi");
    13.  
    14.   }
    15.  
    16. }
    Method #2

    Code (CSharp):
    1. userInput = Input.inputString;
    2.  
    3. print (userInput);
    Method #3

    Code (CSharp):
    1. foreach (char c in Input.inputString) {
    2.  
    3. if (c == "\b"[0]) {
    4.  
    5.   print (Input.inputString);
    6.  
    7. }else{
    8.  
    9.   if (c == "\n"[0] || c == "\r"[0]) {
    10.  
    11.    print (Input.inputString);
    12.  
    13.   }else{
    14.  
    15.    print (Input.inputString);
    16.  
    17.   }
    18.  
    19. }
    20.  
    21. }
    #1: It should work (and there are several other version I have tried) but each time the console puts a warning of "Unreachable code" and doesn't print anything.

    #2: This just prints an empty string and gives the above warning code.

    #3: Same as #1.

    Anyone have any idea how to fix this? I'm truly lost as all the answers on the internet are exactly the same as my solution saying that it worked for them but it does not for me.

    All I need is for the console to print what was pressed.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Option 1 is what I've gone with in the past, and it hasn't given me any trouble. As far as I can see, there is no unreachable code. But you could check by debugging Event.current.isKey and making sure that it is changing when a key is pressed.
     
  3. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    So now I run this code:

    Code (CSharp):
    1. void Update () {
    2.      
    3.         if (Input.anyKeyDown) {
    4.             OnGUI ();
    5.         }
    6. }
    7.  
    8.      void OnGUI () {
    9.         Debug.Log("Current detected event: " + Event.current);
    10.         Event e = Event.current;
    11.         if (e.isKey){
    12.             string key = e.keyCode.ToString();
    13.             Debug.Log(key);
    14.         }
    15.     }

    It prints out what I pressed but it is also printing out everything else going on. How to I make it so it just detects keyboard inputs?


    EDIT:

    New code

    Code (CSharp):
    1. string userInput;
    2. bool userValue;
    3.  
    4. void Update () {
    5.        
    6.         userInput = bool.TrueString;
    7.         userValue = bool.Parse(userInput);
    8.        
    9.         if (userValue) {
    10.             OnGUI ();
    11.         }
    12. }
    13.  
    14. void OnGUI () {
    15.         Event e = Event.current;
    16.         if (e.isKey){
    17.             string key = e.keyCode.ToString();
    18.             Debug.Log(key);
    19.         }
    20. }
    Produces:



    Exactly what I want minus the dumb null reference. How do I get rid of that?
     
    Last edited: Nov 8, 2015
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    We cannot help you remove the null reference if you do not show us the code.
    But either way it simply means you told it to do something and it could not find what it was looking for.
    (Im no pro, just someone trying to help.)
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Don't call OnGUI. OnGUI will be called automatically. Calling it yourself will give you odd results.
     
  6. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Thanks guys.

    The only thing I'm trying to figure out now is how to make it so it only prints integer inputs ie: 0-9

    I've tried this:

    Code (CSharp):
    1.  
    2. string userInput
    3. int userNumber;
    4. bool valueIsInt;
    5.  
    6. if(int.TryParse(userInput, out userNumber)) {
    7.             valueIsInt = true;
    8.             Debug.Log ("String is the number: " + userNumber);
    9. }
    But I don't think I am using this correctly. Basically trying to test if the string can be parsed into an int and if it can set a bool to true and print the number.

    The bool is used for something later.