[Unity] Editor Window – Keyboard Event

1. Do it in OnGUI()
2. Using Event.current
3. Remember to call Repaint() if need refresh window after clicking shortcut

private void OnGUI()
{
    var e = UnityEngine.Event.current;
    if (e.type != EventType.keyDown)
    {
        return;
    }

    if (e.command)
    {
        switch (e.keyCode)
        {
            case KeyCode.LeftArrow:
                // Command + LeftArrow
                break;

            default:
                return;
        }
    }
    else
    {
        switch (e.keyCode)
        {
            case KeyCode.RightArrow:
                // RightArrow
                break;

            default:
                return;
        }
    }

    // Remember to repaint if need refresh window after clicking shortcut
    Repaint();
}

發佈留言

AllEscort