| 1 |
# Copyright 2014 Patrick Dawson <[email protected]> |
| 2 |
# |
| 3 |
# This software is provided 'as-is', without any express or implied |
| 4 |
# warranty. In no event will the authors be held liable for any damages |
| 5 |
# arising from the use of this software. |
| 6 |
# |
| 7 |
# Permission is granted to anyone to use this software for any purpose, |
| 8 |
# including commercial applications, and to alter it and redistribute it |
| 9 |
# freely, subject to the following restrictions: |
| 10 |
# |
| 11 |
# 1. The origin of this software must not be misrepresented; you must not |
| 12 |
# claim that you wrote the original software. If you use this software |
| 13 |
# in a product, an acknowledgment in the product documentation would be |
| 14 |
# appreciated but is not required. |
| 15 |
# 2. Altered source versions must be plainly marked as such, and must not be |
| 16 |
# misrepresented as being the original software. |
| 17 |
# 3. This notice may not be removed or altered from any source distribution. |
| 18 |
|
| 19 |
from sdl2 cimport * |
| 20 |
from libc.stdlib cimport malloc, free |
| 21 |
from libc.string cimport memcpy |
| 22 |
from pygame_sdl2.display cimport Window, main_window |
| 23 |
from pygame_sdl2.rect cimport to_sdl_rect |
| 24 |
|
| 25 |
from pygame_sdl2.error import error |
| 26 |
|
| 27 |
|
| 28 |
cdef class KeyboardState: |
| 29 |
# Allow weak references. |
| 30 |
cdef object __weakref__ |
| 31 |
|
| 32 |
cdef uint8_t *data |
| 33 |
cdef int numkeys |
| 34 |
|
| 35 |
def __cinit__(self): |
| 36 |
self.data = NULL |
| 37 |
|
| 38 |
def __dealloc__(self): |
| 39 |
if self.data != NULL: |
| 40 |
free(self.data) |
| 41 |
|
| 42 |
def __init__(self): |
| 43 |
cdef uint8_t *state = SDL_GetKeyboardState(&self.numkeys) |
| 44 |
if state == NULL: |
| 45 |
raise error() |
| 46 |
|
| 47 |
# Take a snapshot of the current state, rather than saving the pointer. |
| 48 |
self.data = <uint8_t*>malloc(self.numkeys) |
| 49 |
memcpy(self.data, state, self.numkeys) |
| 50 |
|
| 51 |
def __getitem__(self, int key): |
| 52 |
if SDLK_DELETE < key < SDLK_CAPSLOCK: |
| 53 |
raise IndexError("Out of range.") |
| 54 |
|
| 55 |
cdef int sc = <int>SDL_GetScancodeFromKey(key) |
| 56 |
if sc > self.numkeys: |
| 57 |
raise IndexError("Out of range.") |
| 58 |
|
| 59 |
return self.data[sc] |
| 60 |
|
| 61 |
|
| 62 |
def get_focused(): |
| 63 |
return SDL_GetKeyboardFocus() != NULL |
| 64 |
|
| 65 |
def get_pressed(): |
| 66 |
""" No longer returns a tuple. Use the returned object to check for |
| 67 |
individual keys, but don't loop through it. """ |
| 68 |
|
| 69 |
return KeyboardState() |
| 70 |
|
| 71 |
def get_mods(): |
| 72 |
return SDL_GetModState() |
| 73 |
|
| 74 |
def set_mods(state): |
| 75 |
SDL_SetModState(state) |
| 76 |
|
| 77 |
def set_repeat(delay=0, interval=0): |
| 78 |
# Not possible with SDL2. |
| 79 |
pass |
| 80 |
|
| 81 |
def get_repeat(): |
| 82 |
# Not possible with SDL2. |
| 83 |
return (0,0) |
| 84 |
|
| 85 |
def name(key): |
| 86 |
return SDL_GetKeyName(key) |
| 87 |
|
| 88 |
text_input = False |
| 89 |
|
| 90 |
def start_text_input(): |
| 91 |
global text_input |
| 92 |
text_input = True |
| 93 |
|
| 94 |
if SDL_HasScreenKeyboardSupport(): |
| 95 |
SDL_StartTextInput() |
| 96 |
|
| 97 |
def stop_text_input(): |
| 98 |
global text_input |
| 99 |
text_input = False |
| 100 |
|
| 101 |
if SDL_HasScreenKeyboardSupport(): |
| 102 |
SDL_StopTextInput() |
| 103 |
|
| 104 |
def set_text_input_rect(rect): |
| 105 |
cdef SDL_Rect sdl_rect; |
| 106 |
|
| 107 |
if rect is not None: |
| 108 |
to_sdl_rect(rect, &sdl_rect) |
| 109 |
SDL_SetTextInputRect(&sdl_rect) |
| 110 |
else: |
| 111 |
SDL_SetTextInputRect(NULL) |
| 112 |
|
| 113 |
def has_screen_keyboard_support(): |
| 114 |
return SDL_HasScreenKeyboardSupport() |
| 115 |
|
| 116 |
def is_screen_keyboard_shown(Window window=None): |
| 117 |
if window is None: |
| 118 |
window = main_window |
| 119 |
|
| 120 |
return SDL_IsScreenKeyboardShown(window.window) |