title: PySDL2 and SDL_GetKeyboardState
body: <p>I'm trying to learn SDL via PySDL2 (currently version 0.7.0; <a href="https://bitbucket.org/marcusva/py-sdl2" rel="nofollow">source</a> / <a href="http://pysdl2.readthedocs.org/en/latest/" rel="nofollow">documentation</a>). It provides a <a href="http://docs.python.org/2/library/ctypes.html" rel="nofollow">ctypes</a> Python wrapper to (I assume) all of SDL's normal functions, and it provides a few more Pythonic functions via its <code>sdl2.ext</code> package to handle the nitty-gritty and return results in Python types. For example, <code>sdl2.ext.get_events()</code> will call <code>sdl2.SDL_PumpEvents()</code>, then peep at all the events on the queue and return them as a Python list.</p>
  
  <p>However, I cannot tell if such a convenience function exists for <code>SDL_GetKeyboardState()</code>. The official <a href="http://wiki.libsdl.org/SDL_GetKeyboardState" rel="nofollow">SDL Wiki documentation</a> for the function defines it as:</p>
  
  <pre><code>const Uint8* SDL_GetKeyboardState(int* numkeys)
  </code></pre>
  
  <p>where numkeys may be null (otherwise it will receive the length of the returned array) and the returned Uint8* is a pointer to an array of key states, indexed by SDL_Scancode values, which will hold 1 if the key is down or 0 if it is up.</p>
  
  <p>PySDL2 does provide a direct call to SDL_GetKeyboardState via <code>sdl2.keyboard.SDL_GetKeyboardState(numkeys)</code>, but that returns a ctypes value that is not immediately useful.</p>
  
  <p>So my question is, <strong>is there some more Pythonic function for getting the current SDL keyboard state via PySDL2</strong>? If there is, what is it and how should it be used? If there's not, should one be submitted?</p>
  
  <p>For reference, I think I figured out how to achieve this functionality myself, but it was a rough baptism in ctypes so I might have mangled something:</p>
  
  <pre><code>import ctypes
  import sdl2
  
  
  KEYBOARD_ARRAY_TYPE = ctypes.c_uint8 * sdl2.SDL_NUM_SCANCODES
  
  
  def get_keyboard_state():
      """ Returns a pointer to the current SDL keyboard state,
      which is updated on SDL_PumpEvents. """
      raw_keystate = sdl2.keyboard.SDL_GetKeyboardState(None)
      pointer = ctypes.cast(raw_keystate, ctypes.POINTER(KEYBOARD_ARRAY_TYPE))
      return pointer.contents
  </code></pre>