Fix Ctrl/Alt+key not working on systems with alternate keyboard layouts

Fixes #17
This commit is contained in:
Kovid Goyal 2017-01-12 10:17:21 +05:30
parent 4b28cc755e
commit dedaf6caf7
4 changed files with 32 additions and 8 deletions

View File

@ -27,14 +27,7 @@ static PyMethodDef module_methods[] = {
{"parse_bytes_dump", (PyCFunction)parse_bytes_dump, METH_VARARGS, ""}, {"parse_bytes_dump", (PyCFunction)parse_bytes_dump, METH_VARARGS, ""},
{"read_bytes", (PyCFunction)read_bytes, METH_VARARGS, ""}, {"read_bytes", (PyCFunction)read_bytes, METH_VARARGS, ""},
{"read_bytes_dump", (PyCFunction)read_bytes_dump, METH_VARARGS, ""}, {"read_bytes_dump", (PyCFunction)read_bytes_dump, METH_VARARGS, ""},
{"glfw_set_error_callback", (PyCFunction)glfw_set_error_callback, METH_O, ""}, GLFW_FUNC_WRAPPERS
{"glfw_init", (PyCFunction)glfw_init, METH_NOARGS, ""},
{"glfw_terminate", (PyCFunction)glfw_terminate, METH_NOARGS, ""},
{"glfw_window_hint", (PyCFunction)glfw_window_hint, METH_VARARGS, ""},
{"glfw_swap_interval", (PyCFunction)glfw_swap_interval, METH_VARARGS, ""},
{"glfw_wait_events", (PyCFunction)glfw_wait_events, METH_VARARGS, ""},
{"glfw_post_empty_event", (PyCFunction)glfw_post_empty_event, METH_NOARGS, ""},
{"glfw_get_physical_dpi", (PyCFunction)glfw_get_physical_dpi, METH_NOARGS, ""},
{NULL, NULL, 0, NULL} /* Sentinel */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };

View File

@ -192,6 +192,13 @@ glfw_get_physical_dpi(PyObject UNUSED *self) {
return Py_BuildValue("ff", dpix, dpiy); return Py_BuildValue("ff", dpix, dpiy);
} }
PyObject*
glfw_get_key_name(PyObject UNUSED *self, PyObject *args) {
int key, scancode;
if (!PyArg_ParseTuple(args, "ii", &key, &scancode)) return NULL;
return Py_BuildValue("s", glfwGetKeyName(key, scancode));
}
// }}} // }}}
static void static void

View File

@ -16,3 +16,16 @@ PyObject* glfw_swap_interval(PyObject UNUSED *self, PyObject *args);
PyObject* glfw_wait_events(PyObject UNUSED *self, PyObject*); PyObject* glfw_wait_events(PyObject UNUSED *self, PyObject*);
PyObject* glfw_post_empty_event(PyObject UNUSED *self); PyObject* glfw_post_empty_event(PyObject UNUSED *self);
PyObject* glfw_get_physical_dpi(PyObject UNUSED *self); PyObject* glfw_get_physical_dpi(PyObject UNUSED *self);
PyObject* glfw_get_key_name(PyObject UNUSED *self);
#define GLFW_FUNC_WRAPPERS \
{"glfw_set_error_callback", (PyCFunction)glfw_set_error_callback, METH_O, ""}, \
{"glfw_init", (PyCFunction)glfw_init, METH_NOARGS, ""}, \
{"glfw_terminate", (PyCFunction)glfw_terminate, METH_NOARGS, ""}, \
{"glfw_window_hint", (PyCFunction)glfw_window_hint, METH_VARARGS, ""}, \
{"glfw_swap_interval", (PyCFunction)glfw_swap_interval, METH_VARARGS, ""}, \
{"glfw_wait_events", (PyCFunction)glfw_wait_events, METH_VARARGS, ""}, \
{"glfw_post_empty_event", (PyCFunction)glfw_post_empty_event, METH_NOARGS, ""}, \
{"glfw_get_physical_dpi", (PyCFunction)glfw_get_physical_dpi, METH_NOARGS, ""}, \
{"glfw_get_key_name", (PyCFunction)glfw_get_key_name, METH_VARARGS, ""}, \

View File

@ -49,8 +49,19 @@ control_codes[defines.GLFW_KEY_DELETE] = bytearray(key_as_bytes('kdch1').replace
alt_codes = {k: (0x1b, k) for i, k in enumerate(range(defines.GLFW_KEY_SPACE, defines.GLFW_KEY_RIGHT_BRACKET + 1))} alt_codes = {k: (0x1b, k) for i, k in enumerate(range(defines.GLFW_KEY_SPACE, defines.GLFW_KEY_RIGHT_BRACKET + 1))}
valid_localized_key_names = {
k: getattr(defines, 'GLFW_KEY_' + k) for k in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
}
def get_localized_key(key, scancode):
name = defines.glfw_get_key_name(key, scancode)
return valid_localized_key_names.get((name or '').upper(), key)
def interpret_key_event(key, scancode, mods): def interpret_key_event(key, scancode, mods):
data = bytearray() data = bytearray()
key = get_localized_key(key, scancode)
if mods == defines.GLFW_MOD_CONTROL and key in control_codes: if mods == defines.GLFW_MOD_CONTROL and key in control_codes:
# Map Ctrl-key to ascii control code # Map Ctrl-key to ascii control code
data.extend(control_codes[key]) data.extend(control_codes[key])