Handleing input by keys

I am tring to just print something to say yes you handled the input right but I dont know either if the code is working or i am not looking in the right place where the message should print.
Any help would be appreicated Im a bit lost here;

void battle()
	{
		/*
		Input.GetKey(KeyCode.X); 
		Input.GetKey(KeyCode.V); 
		Input.GetKey(KeyCode.S); 
		Input.GetKey(KeyCode.L); 
		Input.GetKey(KeyCode.R); 
		Input.GetKey(KeyCode.C); 
		Input.GetKey(KeyCode.D); 
		Input.GetKey(KeyCode.I); 
		*/
		//see if enemy dies
		while(Ninja.hp > 0){
			
		
			
		//random number for attack power Not C# but unity engine random number
		//int dice = UnityEngine.Random.Range(1,50); 
		if(Input.GetKey(KeyCode.X)) 
			{
					
				print("X Pressed");
			}
			
		if(Input.GetKey(KeyCode.V))
			{
				print("V is Pressed");
			}
			
		if(Input.GetKey(KeyCode.S))
			{
				print("S is pressed");
			}
		
		if(Input.GetKey(KeyCode.L))
		{
			print(" L Pressed");
		}
		
		if(Input.GetKey(KeyCode.R))
		{
			print("R Pressed");
		}
		
		if(Input.GetKey(KeyCode.C))
		{
			print(" C Pressed");
		}
		
		if(Input.GetKey(KeyCode.D))
		{
			print("D Pressed");
		}
		
		if(Input.GetKey(KeyCode.I))
		{
			print("I Pressed");
		}

I normally use a GUIText to validate. (Not because it’s better just because I haven’t found the equivalent of print from JavaScript that I can use in C#)

So in your code, you will want to add the following;

   public GUIText isKeyPressed;

   void battle() 
   { 
      /* 
      Input.GetKey(KeyCode.X); 
      Input.GetKey(KeyCode.V); 
      Input.GetKey(KeyCode.S); 
      Input.GetKey(KeyCode.L); 
      Input.GetKey(KeyCode.R); 
      Input.GetKey(KeyCode.C); 
      Input.GetKey(KeyCode.D); 
      Input.GetKey(KeyCode.I); 
      */ 
      //see if enemy dies 
      while(Ninja.hp > 0){ 
          
       
          
      //random number for attack power Not C# but unity engine random number 
      //int dice = UnityEngine.Random.Range(1,50); 
      if(Input.GetKey(KeyCode.X)) 
         { 
                
            isKeyPressed = "X Pressed"; 
         } 
          
      if(Input.GetKey(KeyCode.V)) 
         { 
            isKeyPressed = "V is Pressed"; 
         } 
          
      if(Input.GetKey(KeyCode.S)) 
         { 
            isKeyPressed = "S is pressed"; 
         } 
       
      if(Input.GetKey(KeyCode.L)) 
      { 
         isKeyPressed = " L Pressed"; 
      } 
       
      if(Input.GetKey(KeyCode.R)) 
      { 
         isKeyPressed = "R Pressed"; 
      } 
       
      if(Input.GetKey(KeyCode.C)) 
      { 
         isKeyPressed = " C Pressed"; 
      } 
       
      if(Input.GetKey(KeyCode.D)) 
      { 
         isKeyPressed = "D Pressed"; 
      } 
       
      if(Input.GetKey(KeyCode.I)) 
      { 
         isKeyPressed = "I Pressed"; 
      }

Don’t forget to put a GUIText in your scene and then add it to the script.

One other thing. Efficiency wise, it would be best if you used a switch instead of the IF statement. That way, it will go directly to the key that was pressed and not on each if to see if it has to go in or not.

Something like this;

            KeyCode keyPressed;

            switch(keyPressed)
            {
                case KeyCode.X:
                    isKeyPressed = "X Pressed";
                    break;

                case KeyCode.V:
                    isKeyPressed = "V Pressed";
                    break;

                //and so on... until all your keys are in
                //and the last one would be

                case default: //because the user press a key that is not mentionned above...
                    isKeyPressed = "No valide key was pressed";
           
            }

Thanks that did help a bit, but when I try to add the KeyCode keyPressed; its says its an unassigned variable am I supposed to assign something to that or not? Am I supposed to assign the guitext object to that?
Thanks

oupss forgot that one part and screwed on the other here’s the proper way… or at least a good way of doing it.

        int lenght = Input.inputString.Length;
        if(lenght > 0)
        {
            //make sure that you only have on key press 
            //logged in and that it,s the last one.
            string keypressedStr = 
                       Input.inputString.Substring(lenght - 1, 1);          

            switch(keypressedStr) 
            { 
                case "x": 
                    isKeyPressed = "X Pressed"; 
                    break; 

                case "v": 
                    isKeyPressed = "V Pressed"; 
                    break; 

                //and so on... until all your keys are in 
                //and the last one would be 

                case default: //because the user press a key that is not mentionned above... 
                    isKeyPressed = "No valide key was pressed"; 
            
            } 
        }

You can use Debug.Log in C#. (It also works from JS - the print statement is just a shortcut for it.)

Thanks a lot that does help and now I think I got the code working:)