title: How to allow Tkinter GUI to detect key presses when not selected
body: <p>I am trying to make a full GUI, for A game borderlands 2, however it only detects my key presses when the tkinter box is selected (in focus). In a game this is not happening, so I need a way for tkinter to detect my key presses while not selected. 
  This is the code so far. It is not finished, ignore the "Auto Exec" and "Auto Exit" the problem is "Auto Reload". Clicking once will turn it ON and again will turn it OFF. Just choose any number (1-4) for the weapon slot, does not affect the error.</p>
  
  <pre><code>from easygui import *
  from pyautogui import *
  from time import *
  import os
  from tkinter import *
  count = 1
  slot = "0"
  allowReload, allowExit = False, False
  def poop():
      sleep(3)
      print("poop")
  def countdown(count):
      autoexecB.config(text = count)
      if count == 2:
          autoexecB.config(bg = "orange")
      if count == 1:
          autoexecB.config(bg = "yellow")
      if count == 0:
          autoexecB.config(text = "Running...", bg = "#44ff00")
      if count &gt; -1:
          root.after(1000, countdown, count-1)
      else:
          for i in range(1, 3):
              sleep(1)
              press("'")
              sleep(0.5)
              type("exec Patch.txt")
              press("enter")
              press("esc")
              press("enter")
              sleep(4)
              press("esc")
              pressMult("down", 4)
              press("enter")
              press("up")
              press("enter")
          sleep(1)
          press("'")
          sleep(0.5)
          type("exec STV.txt")
          press("enter")
          press("esc")
          autoexecB.config(text = "Auto Exec", bg = "red")
  def type(text):
      typewrite(text)
  def pressMult(key, amount):
      for i in range(1, amount+1):
          press(key)
  def autoexec():
      countdown(3)
  def info():
      msgbox("Auto Exec: Runs Mods automaticly\nAuto Exit: Exits the game automaticly to the main menu using INSERT\nAuto Reload: Automaticly reloads your gun using LEFT SHIFT")
  def exit():
      global count
      count = 1
      press("esc")
      pressMult("down", 4)
      press("enter")
      press("up")
      press("enter")
      sleep(2.1)
      press("enter")
      if choose == "FARM at Hero's Pass":
          sleep(3)
          keyDown("w")
          sleep(0.7)
          keyUp("w")
          keyDown("d")
          sleep(1)
          keyUp("d")
          keyDown("ctrl")
          sleep(0.5)
          press("e")
          keyUp("ctrl")
          count += 1
  def reloadslot():
      global allowReload, slot
      while True:
          if allowReload == True:
              break
          slot = str(integerbox("Enter in the weapon's slot to be reloaded"))
          if slot not in ("1","2","3","4"):
              msgbox("A weapon can only be in slot 1, 2, 3 or 4")
          else:
              break
      if allowReload == True:
          allowReload = False
      else:
          allowReload = True
  def on_press(event):
      print(event.keysym)
      if event.keysym == "Insert" and allowExit == True:
          print("exit")
          exit()
      if event.keysym == "Shift_L" and allowReload == True:
          print("running reload")
          press(";")
          press("e")
          press(slot)
      print("done")
  root = Tk()
  root.bind('&lt;KeyPress&gt;', on_press)
  root.geometry("378x134")
  root.config(bg = "blue")
  autoexecB = Button(text = "Auto Exec", bg = "red", font = ("calibri","13"), height = 3, width = 13, command = lambda: autoexec())
  autoexitB = Button(text = "Auto Exit", bg = "red", font = ("calibri","13"), height = 3, width = 13)
  autoreloadB = Button(text = "Auto Reload", bg = "red", font = ("calibri","13"), height = 3, width = 13, command = lambda: reloadslot())
  infoB = Button(text = "INFO", bg = "blue", width = 26, height = 3, command = lambda: info())
  exitB = Button(text = "EXIT", bg = "blue", width = 25, height = 3, command = lambda: root.destroy())
  autoexecB.place(x = 0, y = 0)
  autoexitB.place(x = 126, y = 0)
  autoreloadB.place(x = 252, y = 0)
  infoB.place(x = 0, y = 78)
  exitB.place(x = 193, y = 78)
  root.mainloop()
  root.mainloop()
  </code></pre>