EditorWindow needs to be focused to run the script

Can someone help me debug this… I really don’t understand why you need the window focused to work to toggle a bool when key is pressed = true , unpressed = false.

using UnityEngine;
using UnityEditor;
using System.Collections;
[InitializeOnLoad]
public class Editor : EditorWindow
{
  private static bool toggle = false;
  [MenuItem( "Window/Editor" )]
static void Init()
{
  var window = (Editor)EditorWindow.GetWindow( typeof( Editor ));
  window.maxSize = new Vector2( 200, 100 );
}
public void OnGUI()
{
  toggle = EditorGUILayout.Toggle( "Toggle", toggle );
      if (GUIKeyDown(KeyCode.X))
       {
           Debug.Log("You used the shortcut X Down");
           toggle = true;
       }
       else if (GUIKeyUp(KeyCode.X))
       {
           Debug.Log("You used the shortcut X Up");
           toggle = false;
      
       }
}
bool GUIKeyDown(KeyCode key)
{
  if (Event.current.type == EventType.KeyDown)
  return (Event.current.keyCode == key);
  return false;
}
bool GUIKeyUp(KeyCode key)
{
  if (Event.current.type == EventType.KeyUp)
  return (Event.current.keyCode == key);
  return false;
}
public void Update()
{
  Repaint();
}
}

Hi, try set RunInBackground property to true in player settings.