[Unity] Editor Window – Keyboard Event



Overview

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();
}



歡迎您留言與分享!(Welcome for comments or sharing!)

Related Post:

Tagged on: ,