title: Bind many keys at the same time
body: <p>I've noticed that i was quickly stuck when i wanted to do a game which needed simultaneous key presses on Tkinter. That's why i decided to create a widget which allow the user to get all active keys.</p>
  
  <p>The methode is simple:</p>
  
  <ul>
  <li>I've a set</li>
  <li>When a key is pressed, I add it into the set</li>
  <li>When a key is released, I discard from the set</li>
  <li>If the user want to know which keys are pressed, I just return the set</li>
  </ul>
  
  <p>I've done this code:</p>
  
  <pre><code>import tkinter as tk
  
  class Tk_Multikey(tk.Frame):
      """Allow the user to get all actived keys"""
  
      def __init__(self, master, **kwargs):
          super().__init__(master, **kwargs)
  
          self.keysym = set()
          self.keycode = set()
          self.keysym_num = set()
          master.bind('&lt;KeyPress&gt;', self.add_a_key, add='+')
          master.bind('&lt;KeyRelease&gt;', self.discard_a_key, add='+')
  
      def add_a_key(self, event):
          """When a key is pressed, add its keysym, keycode and keysym_num to the corresponding set"""
  
          self.keysym.add(event.keysym.upper()) # the upper method prevents a 
          # bug when the user is using the Shift/Caplock key and a letter key
          self.keycode.add(event.keycode)
          self.keysym_num.add(event.keysym_num)
  
  
      def discard_a_key(self, event):
          """When a key is released, discard its keysym, keycode and keysym_num to the corresponding set"""
  
          self.keysym.discard(event.keysym.upper()) 
          self.keycode.discard(event.keycode)
          self.keysym_num.discard(event.keysym_num)
  
  
      def __delattr__(self, nameattr):
          """The user is not allowed to delete a set"""
          raise AttributeError("Unable to delete")
  
      def get_keys(self):
          return((self.keysym, self.keycode, self.keysym_num))
  
  
  if __name__ == "__main__":
  
      def update(event=None):
          print("updating")
          strvar_keysym.set(mtk.keysym)
          strvar_keycode.set(mtk.keycode)
          strvar_keysym_num.set(mtk.keysym_num)
  
      root = tk.Tk()
  
      mtk = Tk_Multikey(root)
      mtk.pack()
  
      root.bind('&lt;KeyPress&gt;', update, add='+')
      root.bind('&lt;KeyRelease&gt;', update, add='+')
  
      strvar_keysym = tk.StringVar()
      strvar_keycode = tk.StringVar()
      strvar_keysym_num = tk.StringVar()
      update()
  
      tk.Label(root, textvariable=strvar_keysym).pack()    
      tk.Label(root, textvariable=strvar_keycode).pack()    
      tk.Label(root, textvariable=strvar_keysym_num).pack()
  
      root.mainloop()
  </code></pre>
  
  <p>Now I have a bug that i don't understand.</p>
  
  <p>When I press A, Z, E, R, that works; But if I press A, Z, E, R T, that doesn't work (the T does not appear). However, if i only press A, Z, E, T, that works. I believed that the maximum was 4 but i've tried with many other key and it's not that. (I've the same bug with W, X, C for exemple)</p>
  
  <p>Why this bug happens and what can i do to solve it?</p>
  
  <p>Thank you :)</p>
  
  <p>PS: I don't know why i can't say hello at the beginning of my message.. So i put it a the end: Hello there!</p>