0

in the code bellow it´s impossible to print "FBleftcontrol", also depending on the order and combination of keys pressed sometimes other lines are impossible to print

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    bool LeftControl = false;
    bool LeftShift = false;
    bool LeftAlt = false;
    // Update is called once per frame
    void Update()
    {
        LeftControl = Input.GetKey(KeyCode.LeftControl);
        LeftShift = Input.GetKey(KeyCode.LeftShift);
        LeftAlt = Input.GetKey(KeyCode.LeftAlt);

        if (LeftAlt || LeftShift) fb();
        else fa();
    }

    private void fb()
    {
        print("fb");
        if (LeftControl)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1)) print("leftcontrol");
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.Alpha1)) print("NOleftcontrol");
        }
    }

    private void fa()
    {
        print("fa");
        if (LeftControl)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1)) print("leftcontrol");
        }
        else if(LeftShift)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1)) print("leftshift");
        }
    }
}

Things I´ve tried: latch booleans; use all posible combinations of getkey/getkeydown/getkeyup; put it all in the update and forget about calling functions

5
  • 1
    It works for me. Are you sure you tested your code properly? Commented Mar 3, 2019 at 20:10
  • 1
    OK you need to have left alt or left shift down to run fb, and then you also need left control to print leftcontrol.. but you also need that frame to have pressed alpha 1.. so yes, it is very specific Commented Mar 3, 2019 at 20:15
  • 1
    This is a hardware limitation: your keyboard can only report so many simultaneous keypresses. Typically 3. Commented Mar 3, 2019 at 20:38
  • 1
    As @Draco18s said, this is probably a issue regarding your keyboard. Check out keyboard anti-ghosting and to see how many simultaneous keypresses your keyboard can handle check this website out: Ghosting Demonstration Commented Mar 4, 2019 at 9:47
  • regarding the website: I can press down +10 keys and still registers, also fa works just fine (provided i didn't try using fb first) and involves more keys than fb. Just in case I've tried several keyboards, currently I have a mechanical keyboard Commented Mar 4, 2019 at 14:53

1 Answer 1

2

Unity Editor is stealing your keyboard focus because most modifier keys are bound to it's GUI. Try the same code with KeyCode.A, KeyCode.S, KeyCode.C instead of Alt, Shift, Control and your code will work as expected.

This should happen only in the Editor and not in the final build, though.

Unfortunately, I don't know of any solution to avoid this in the Editor: I would just use different key-bindings during development.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.