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. Dismiss Notice

Swtich statement using keycode.X

Discussion in 'Scripting' started by milar_ninja, Jul 7, 2010.

Thread Status:
Not open for further replies.
  1. milar_ninja

    milar_ninja

    Joined:
    Jun 11, 2010
    Posts:
    21
    I am at a lost on how to do a switch statement using a user input using the keycode.X what do I use to test the case? KeyCode or something like a variable or something like that?
    Thanks
     
  2. nvarcha

    nvarcha

    Joined:
    Sep 27, 2008
    Posts:
    191
    I don't understand what you need.

    Could you clarify?
     
  3. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    I think you're speaking on how to determine when a specific key is entered, outside of the use of the Input Manager. So instead of doing Input.GetButton("name of button"), you want to know if the specific key was pressed using a predefined enumeration.

    For example: Keys.F8 to designate the F8 button.
    Or in XNA: keyboardstate.IsKeyDown(Keys.Space)

    To my knowledge, Unity doesn't have this, but I am no way a Unity master.

    What is it you are planning on doing with the switch statement?
     
  4. waltermana

    waltermana

    Joined:
    Jun 8, 2010
    Posts:
    172
    unity has Input.GetKeyDown(Kexcode.X) - if that is what you are looking for
     
  5. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
  6. milar_ninja

    milar_ninja

    Joined:
    Jun 11, 2010
    Posts:
    21
    What i want to do is get rid of all my if statements and use a nice and tidy switch statement:
    This is just a snipit of the code that I am using and its geting out of hand the more I have to put into it.
    Code (csharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.H)){
    3.     FlameSlash();
    4. }//flameSlash
    5.  
    6. //shiningStrike
    7. if (Input.GetKeyDown(KeyCode.J)){
    8.         ShiningStar();
    9. }//end shiningStrike
    10.  
    11. //slash
    12. if (Input.GetKeyDown(KeyCode.K)){
    13.     Slash();
    14. }//end of slash
    15.  
    16.  
    But I dont know how to use the switch statment instead of the if: What do I use to test the case


    Code (csharp):
    1.  
    2. switch(Dont know what to put here to test what button to push)
    3. {
    4.   I know what to put in here
    5.  
    6. }
     
    yariquezz likes this.
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There isn't an Input class function to get which key is being pressed and there isn't a way to make the boolean Input functions work usefully with a switch statment. You can use the Event.keyCode variable for this, but it is only valid in the OnGUI function during KeyDown and KeyUp events. You may be able to get the key code from the OnGUI function and store it in a variable where it can by picked up by Update later on. Generally speaking, more than one key could be held down at once, so you should use a switch statement cautiously, in any case.
     
  8. milar_ninja

    milar_ninja

    Joined:
    Jun 11, 2010
    Posts:
    21
    Thanks for the input everyone, I think I will stick with the if statements that I have right now.
     
  9. desarrollo

    desarrollo

    Joined:
    Nov 26, 2014
    Posts:
    1
    if (Input.anyKeyDown)
    {
    switch(Input.inputString);
    }
     
    HeadlessCorspe likes this.
  10. howler123

    howler123

    Joined:
    May 7, 2017
    Posts:
    17
    Input.inputstring will give you regular alphabet letters but everything else is "". ie. leftarrow = "", m = "m"
     
Thread Status:
Not open for further replies.