title: event.keycode is not working tkinter in python
body: <p>Here I am just designing simple wandrer game, and I would like to use <kbd>Esc</kbd> button to exit game and use <kbd>Space</kbd> button to attack. I have used mac Keycode of each in my code but it is not working. Here is my main.py:</p>
  <pre><code> m = Utils.matrix
   def on_key_press(event):
      #exit
      if event.keycode == 53:
       print(event.keycode)
       root.destroy()
  
      #battle
      opponent = False
      for char in game.get_characters():
          if event.keycode == 49 and coords(hero) == coords(char):
              opponent = hero.battle(char)
      if opponent:
          game.remove_character(opponent)
      #initialise new game object and level up characters
      if game.get_char_length() &lt; 1:
          new_level()
          game.create_monsters()
          game.level_up_chars(hero.level)
          hero.x = 0
          hero.y = 0
          hero.set_image(hero_down)
  
      canvas.delete(&quot;all&quot;)
      stats()
      draw_canvas()
  
  
  canvas.bind(&quot;&lt;KeyPress&gt;&quot;, on_key_press)
  canvas.pack()
  </code></pre>
  <p>Here i just tried to create a separate function and it is still not working, the other similar function such as keyup(), keydown() ... are working perfectly but for &quot;esc&quot; and &quot;space&quot; no.</p>
  <pre><code>m = Utils.matrix
  def coords(char):
      x, y = char.get_coordinates()
      x, y = int(x / 72), int(y / 72)
      return x, y
  def leftKey(event):
    if  m[coords(hero)[1]][coords(hero)[0] - 1] != 1 and coords(hero)[0] &gt; 0: #left
       hero.move(x=-72)
       hero.set_image(hero_left)
  
  def rightKey(event):
      if  m[coords(hero)[1]][coords(hero)[0]+1] != 1 and coords(hero)[0]&lt;=9:   #right
       hero.move(x=72)
       hero.set_image(hero_right)
  
  def upKey(event):
      if  m[coords(hero)[1] - 1][coords(hero)[0]] != 1 and coords(hero)[1] &gt; 0:  # up
       hero.move(y=-72)
       hero.set_image(hero_up)
  
  def downKey(event):
      if  m[coords(hero)[1] + 1][coords(hero)[0]] != 1 and coords(hero)[1] &lt;= 9:  # down
       hero.move(y=72)
       hero.set_image(hero_down)
  
  def close(event):
      # canvas.withdraw()  # if you want to bring it back
      # sys.exit() # if you want to exit the entire thing
      root.destry()
  
  root.bind('&lt;Escape&gt;', close)
  
  root.bind('&lt;Left&gt;', leftKey)
  root.bind('&lt;Right&gt;', rightKey)
  root.bind('&lt;Up&gt;', upKey)
  root.bind('&lt;Down&gt;', downKey)
  # root.bind(&quot;&lt;KeyPress&gt;&quot;, close)
  
  
  
  
  enter code here
  </code></pre>