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

key inputs wont work on Input.GetKey(KeyCode.LeftControl)

Discussion in 'Scripting' started by Aldo, Jun 8, 2015.

  1. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    so, I have the next code, it works fine:

    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.X))
    2. {
    3.    baseTransform.localScale = new Vector3(baseTransform.localScale.x *-1, baseTransform.localScale.y, baseTransform.localScale.z);
    4. }
    5. if(Input.GetKeyDown(KeyCode.Y))
    6. {
    7.    baseTransform.localScale = new Vector3(baseTransform.localScale.x, baseTransform.localScale.y *-1, baseTransform.localScale.z);
    8.  }
    BUT if I add it inside an if(Input.GetKey(KeyCode.LeftControl)){} like this


    Code (CSharp):
    1. if(Input.GetKey(KeyCode.LeftControl))
    2. {
    3.  if(Input.GetKeyDown(KeyCode.X))
    4.  {
    5.      baseTransform.localScale = new Vector3(baseTransform.localScale.x *-1, baseTransform.localScale.y, baseTransform.localScale.z);
    6.   }
    7.  
    8.   if(Input.GetKeyDown(KeyCode.Y))
    9.   {
    10.       baseTransform.localScale = new Vector3(baseTransform.localScale.x, baseTransform.localScale.y *-1, baseTransform.localScale.z);
    11.   }
    12. }
    It won't work, mouse buttons and scroll work fine but keyboard doesn't, any ideas?
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    bump
     
  3. AlexConnolly

    AlexConnolly

    Joined:
    Dec 23, 2012
    Posts:
    32
    I mean, this should work, but only once. You need to use the same logic as you do with the left control for it to happen continuously.

    Eg. if(Input.GetKey(KeyCode.X))

    I'd assume this would fix your problem, since the reason why the scroll wheel works is that it's not a single event like a button press, the scroll is technically multiple events rather than the single button press.
     
  4. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    I do need KeyDown and mouse works on MouseButtonDown, I believe that the keyboard configuration changes while you hold down control (in this case left control), so there might be a different KeyCode for every key in the keyboard
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I assume you are working within the runtime editor (i.e. hitting play at the top)... try building your project and running from the .exe, the above code should work.

    ctrl+x looks to be intercepted by the windows as if it is expecting to cut rather than being passed through when in runtime within the editor.
     
  6. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    Yes I am working within the runtime editor, it also intercepts ctrl+y, ctrl+1, ctrl+n I stoped testing keys after the 4rth.
    Is there a workaround? I need it to work inside the runtime editor (right now I made a second pair of controls without ctrl)
     
  7. ABerlemont

    ABerlemont

    Joined:
    Sep 27, 2012
    Posts:
    67
    Still have that kind of problem 4 years later ^^.

    LeftControl is forwarded to the editor and sends a lot of actions that I don't want to do.

    ie : CTRL+S (context : FPS controller -> crouch + walking backward)