prelim. keyboard support
This commit is contained in:
parent
ca4d04c207
commit
5322668415
33
demo/keyboard.py
Normal file
33
demo/keyboard.py
Normal file
@ -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()
|
43
photon/keyboard.py
Normal file
43
photon/keyboard.py
Normal file
@ -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)
|
39
photon/tests/test_keyboard.py
Normal file
39
photon/tests/test_keyboard.py
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user