Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds function to pygame.key module to get key code from name of key #1694

Merged
merged 8 commits into from May 5, 2020
1 change: 1 addition & 0 deletions buildconfig/pygame-stubs/key.pyi
Expand Up @@ -16,6 +16,7 @@ def set_mods() -> int: ...
def set_repeat(delay: Optional[int], interval: Optional[int]) -> None: ...
def get_repeat() -> Tuple[int, int]: ...
def name(key: int) -> str: ...
def key_code(name: str) -> int: ...
def start_text_input() -> None: ...
def stop_text_input() -> None: ...
def set_text_input_rect(_RectValue) -> None: ...
26 changes: 26 additions & 0 deletions docs/reST/ref/key.rst
Expand Up @@ -332,6 +332,32 @@ for ``KMOD_NONE``, which should be compared using equals ``==``). For example:

.. ## pygame.key.name ##

.. function:: key_code

| :sl:`get the key identifier from a key name`
| :sg:`key_code(name=string) -> int`

Get the key identifier code from the descriptive name of the key. This
returns an integer matching one of the K_* keycodes. For example:

::

>>> pygame.key.key_code("return") == pygame.K_RETURN
True
>>> pygame.key.key_code("0") == pygame.K_0
True
>>> pygame.key.key_code("space") == pygame.K_SPACE
True

:raises ValueError: if the key name is not known.
:raises NotImplementedError: if used with SDL 1.

.. ## pygame.key.key_code ##

.. versionadded:: 2.0.0

.. ## pygame.key.key_code ##

.. function:: start_text_input

| :sl:`start handling IME compositions`
Expand Down
5 changes: 5 additions & 0 deletions src_c/doc/key_doc.h
Expand Up @@ -7,6 +7,7 @@
#define DOC_PYGAMEKEYSETREPEAT "set_repeat() -> None\nset_repeat(delay) -> None\nset_repeat(delay, interval) -> None\ncontrol how held keys are repeated"
#define DOC_PYGAMEKEYGETREPEAT "get_repeat() -> (delay, interval)\nsee how held keys are repeated"
#define DOC_PYGAMEKEYNAME "name(key) -> string\nget the name of a key identifier"
#define DOC_PYGAMEKEYKEYCODE "key_code(name=string) -> int\nget the key identifier from a key name"
#define DOC_PYGAMEKEYSTARTTEXTINPUT "start_text_input() -> None\nstart handling IME compositions"
#define DOC_PYGAMEKEYSTOPTEXTINPUT "stop_text_input() -> None\nstop handling IME compositions"
#define DOC_PYGAMEKEYSETTEXTINPUTRECT "set_text_input_rect(Rect) -> None\ncontrols the position of the candidate list"
Expand Down Expand Up @@ -49,6 +50,10 @@ pygame.key.name
name(key) -> string
get the name of a key identifier

pygame.key.key_code
key_code(name=string) -> int
get the key identifier from a key name

pygame.key.start_text_input
start_text_input() -> None
start handling IME compositions
Expand Down
34 changes: 34 additions & 0 deletions src_c/key.c
Expand Up @@ -709,6 +709,38 @@ key_name(PyObject *self, PyObject *args)
#endif
}

static PyObject *
key_code(PyObject *self, PyObject *args, PyObject *kwargs)
{
const char * name;
SDL_Keycode code;

static char *kwids[] = {
"name",
NULL
};

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwids, &name))
return NULL;

#if IS_SDLv1
PyErr_SetString(PyExc_NotImplementedError, "not supported with SDL 1");
return 0;
#else
code = SDL_GetKeyFromName(name);
if (code != SDLK_UNKNOWN){
return PyInt_FromLong(code);
}
else{
// Raise an unknown key name error?
PyErr_SetString(PyExc_ValueError, "unknown key name");
return 0;
}

#endif

}

static PyObject *
key_get_mods(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -786,6 +818,8 @@ static PyMethodDef _key_methods[] = {
{"get_pressed", key_get_pressed, METH_NOARGS,
DOC_PYGAMEKEYGETPRESSED},
{"name", key_name, METH_VARARGS, DOC_PYGAMEKEYNAME},
{"key_code", (PyCFunction)key_code, METH_VARARGS | METH_KEYWORDS,
DOC_PYGAMEKEYNAME},
{"get_mods", key_get_mods, METH_NOARGS, DOC_PYGAMEKEYGETMODS},
{"set_mods", key_set_mods, METH_VARARGS, DOC_PYGAMEKEYSETMODS},
{"get_focused", key_get_focused, METH_NOARGS,
Expand Down
13 changes: 13 additions & 0 deletions test/key_test.py
Expand Up @@ -2,6 +2,8 @@
import pygame
import pygame.key

SDL1 = pygame.get_sdl_version()[0] < 2


class KeyModuleTest(unittest.TestCase):
@classmethod
Expand Down Expand Up @@ -45,6 +47,17 @@ def test_name(self):
self.assertEqual(pygame.key.name(pygame.K_0), "0")
self.assertEqual(pygame.key.name(pygame.K_SPACE), "space")

def test_key_code(self):
MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
if SDL1:
self.assertRaises(NotImplementedError, pygame.key.key_code,
"return")
else:
self.assertEqual(pygame.key.key_code("return"), pygame.K_RETURN)
self.assertEqual(pygame.key.key_code("0"), pygame.K_0)
self.assertEqual(pygame.key.key_code("space"), pygame.K_SPACE)

self.assertRaises(ValueError, pygame.key.key_code, "fizzbuzz")

def test_set_and_get_mods(self):
pygame.key.set_mods(pygame.KMOD_CTRL)
self.assertEqual(pygame.key.get_mods(), pygame.KMOD_CTRL)
Expand Down