From 532266841575385ee0a0f008c8a2ed9cc40f3ebc Mon Sep 17 00:00:00 2001 From: James Turk Date: Fri, 13 May 2011 00:48:15 -0400 Subject: [PATCH] prelim. keyboard support --- demo/keyboard.py | 33 +++++++++++++++++++++++++++ photon/keyboard.py | 43 +++++++++++++++++++++++++++++++++++ photon/tests/test_keyboard.py | 39 +++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 demo/keyboard.py create mode 100644 photon/keyboard.py create mode 100644 photon/tests/test_keyboard.py diff --git a/demo/keyboard.py b/demo/keyboard.py new file mode 100644 index 0000000..1fd38b5 --- /dev/null +++ b/demo/keyboard.py @@ -0,0 +1,33 @@ +from photon import init, InitFlags, keyboard, video +from photon.events import poll_event, EventType, WindowEventType + +def main(): + init(InitFlags.EVERYTHING) + + w = video.Window('keyboard test', 10, 10, 300, 300) + running = True + + while running: + + # event loop + while True: + event = poll_event() + if not event: + break + elif event.type == EventType.QUIT: + running = False + elif event.type == EventType.WINDOWEVENT: + if event.window.event == WindowEventType.CLOSE: + running = False + elif event.type == EventType.KEYDOWN: + scan = event.key.keysym.scancode + key = event.key.keysym.keycode + print 'Key Pressed: %s (%s) | %s (%s)' % ( + keyboard.get_scancode_name(scan), scan, + keyboard.get_key_name(key), key) + # enter -- check letter keys held + if key == 13: + print sum(keyboard.get_keyboard_state()[4:30]), 'letter keys held' + +if __name__ == '__main__': + main() diff --git a/photon/keyboard.py b/photon/keyboard.py new file mode 100644 index 0000000..538f055 --- /dev/null +++ b/photon/keyboard.py @@ -0,0 +1,43 @@ +import ctypes +from .internal import _SDL + +# SDL_GetKeyboardFocus +# have this return a Window() object + +_SDL.SDL_GetKeyboardState.restype = ctypes.POINTER(ctypes.c_uint8) +_keyboard_state = None +def get_keyboard_state(): + # index by scancode + global _keyboard_state + if not _keyboard_state: + _keyboard_state = _SDL.SDL_GetKeyboardState(None) + return _keyboard_state + +def get_mod_state(): + return _SDL.SDL_GetModState() + +def set_mod_state(modstate): + _SDL.SDL_SetModState(modstate) + +def get_key_from_scancode(scancode): + return _SDL.SDL_GetKeyFromScancode(scancode) + +def get_scancode_from_key(keycode): + return _SDL.SDL_GetScancodeFromKey(keycode) + +_SDL.SDL_GetKeyName.restype = ctypes.c_char_p +def get_key_name(keycode): + return _SDL.SDL_GetKeyName(keycode) + +_SDL.SDL_GetScancodeName.restype = ctypes.c_char_p +def get_scancode_name(scancode): + return _SDL.SDL_GetScancodeName(scancode) + +def start_text_input(): + _SDL.SDL_StartTextInput() + +def stop_text_input(): + _SDL.SDL_StopTextInput() + +def set_text_input_rect(rect): + _SDL.SDL_SetTextInputRect(rect) diff --git a/photon/tests/test_keyboard.py b/photon/tests/test_keyboard.py new file mode 100644 index 0000000..d1ce67f --- /dev/null +++ b/photon/tests/test_keyboard.py @@ -0,0 +1,39 @@ +from .. import init, InitFlags +from ..keyboard import (get_mod_state, set_mod_state, get_key_from_scancode, + get_scancode_from_key, get_key_name, get_scancode_name) +from nose.tools import with_setup + +_keys = [ + # scancode, keycode, name + (40, 13, 'Return'), + (4, 97, 'A'), + (87, 1073741911, 'Keypad +'), + (225, 1073742049, 'Left Shift') +] + +def init_everything(): + init(InitFlags.EVERYTHING) + +@with_setup(init_everything) +def test_mod_state(): + pass # TODO + +@with_setup(init_everything) +def test_get_key_from_scancode(): + for scan, key, name in _keys: + assert get_key_from_scancode(scan) == key + +@with_setup(init_everything) +def test_get_scancode_from_key(): + for scan, key, name in _keys: + assert get_scancode_from_key(key) == scan + +@with_setup(init_everything) +def test_get_key_name(): + for scan, key, name in _keys: + assert get_key_name(key) == name + +@with_setup(init_everything) +def test_get_scancode_name(): + for scan, key, name in _keys: + assert get_scancode_name(scan) == name