title: Keyboard Listener from pynput
body: <p>I am trying to import a keyboard listener into my class but keep getting a </p>
  
  <pre><code>NameError: name 'on_press' is not defined
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>from pynput import keyboard
  class game_code:
  
      with keyboard.Listener(on_press=on_press) as listener:
          listener.join()
  
      def check_key_press(self,key):
         try: k = key.char
         except: k = key.name
  
         if k in ['up', 'down', 'left', 'right']:
            self.key = keys.append(k)
            return True
  
          else:
            return False
  </code></pre>
  
  <p>Also not 100% sure on how with statements work.</p>