|
| 1 | +// |
| 2 | +// tkdar - Tk/Tkinter Detectable Auto Repeat for Python |
| 3 | +// Copyright 2026 B.Stultiens |
| 4 | +// |
| 5 | +// This program is free software; you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation; either version 2 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program; if not, write to the Free Software |
| 17 | +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | +// |
| 19 | + |
| 20 | +// |
| 21 | +// Switch the X server's detectable auto repeat feature (if supported). |
| 22 | +// Normal auto repeat sends a KeyRelease/KeyPress event sequence |
| 23 | +// resulting in: |
| 24 | +// press - release, press - release, press - ... - release |
| 25 | +// |
| 26 | +// Detectable auto repeat modifies the event sequence into: |
| 27 | +// press - press - press - ... - release |
| 28 | +// |
| 29 | +// The first KeyPress event is the initial press of the button and the |
| 30 | +// final KeyRelease event is the actual physical release of the button. |
| 31 | +// |
| 32 | +// This code was inspired by the example found at: |
| 33 | +// https://wiki.tcl-lang.org/page/Disable+autorepeat+under+X11 |
| 34 | +// |
| 35 | +// |
| 36 | +// Usage in Python/Tkinter: |
| 37 | +/* |
| 38 | +import tkinter |
| 39 | +import tkdar # exposes tkdar.enable() and tkdar.disable() |
| 40 | +
|
| 41 | +pressed_keys = [] # Current list if pressed keys |
| 42 | +
|
| 43 | +def keypress(event): |
| 44 | + if event.keysym in pressed_keys: |
| 45 | + return # already pressed, ignore repeats |
| 46 | + pressed_keys.append(event.keysym) |
| 47 | + print("Press ", event.keysym) |
| 48 | +
|
| 49 | +def keyrelease(event): |
| 50 | + # KeyRelease without KeyPress may happen when a modifier is active when |
| 51 | + # the key is pressed without a KeyPress handler. No KeyPress event is |
| 52 | + # generated, but releasing the actual key while still holding the modifier |
| 53 | + # generates a KeyRelease event that may be handled if there is a handler |
| 54 | + # installed. Therefore, we test the list to prevent an exception. |
| 55 | + if ev.keysym in pressed_keys: |
| 56 | + pressed_keys.remove(event.keysym) |
| 57 | + print("Release", event.keysym) |
| 58 | +
|
| 59 | +rootwin = tkinter.Tk(className="KeyRepeater") |
| 60 | +rootwin.title = "Key-repeat tester" |
| 61 | +rootwin.minsize(640, 400); |
| 62 | +
|
| 63 | +tkdar.enable(rootwin) # Set detectable auto repeat |
| 64 | +
|
| 65 | +for key in ["Up", "Down", "Left", "Right"]: |
| 66 | + rootwin.bind("<KeyPress-{}>".format(key), keypress) |
| 67 | + rootwin.bind("<KeyRelease-{}>".format(key), keyrelease) |
| 68 | +
|
| 69 | +rootwin.mainloop() |
| 70 | +*/ |
| 71 | + |
| 72 | +#include <Python.h> |
| 73 | +#include <tk.h> |
| 74 | +#include <X11/XKBlib.h> |
| 75 | + |
| 76 | +static PyObject *tkdar(PyObject *arg, Bool enable) |
| 77 | +{ |
| 78 | + // Retrieve the Tcl interpreter instance |
| 79 | + PyObject *interpaddrobj = PyObject_CallMethod(arg, "interpaddr", NULL); |
| 80 | + if(!interpaddrobj) { |
| 81 | + PyErr_SetString(PyExc_TypeError, "get_interpreter: 'interpaddr' call returned NULL"); |
| 82 | + return NULL; |
| 83 | + } |
| 84 | + Tcl_Interp *interp = (Tcl_Interp *)PyLong_AsVoidPtr(interpaddrobj); |
| 85 | + Py_DECREF(interpaddrobj); |
| 86 | + if(interp == (void*)-1) { |
| 87 | + PyErr_SetString(PyExc_TypeError, "get_interpreter: 'interpaddrobj' returned NULL"); |
| 88 | + return NULL; |
| 89 | + } |
| 90 | + |
| 91 | + // Get the X server display via the main Tk window of the interpreter |
| 92 | + Tk_Window tkwin = Tk_MainWindow(interp); |
| 93 | + if(!tkwin) { |
| 94 | + PyErr_SetString(PyExc_RuntimeError, "Error while getting Tk_MainWindow"); |
| 95 | + return NULL; |
| 96 | + } |
| 97 | + Display *display = Tk_Display(tkwin); |
| 98 | + if(!display) { |
| 99 | + PyErr_SetString(PyExc_RuntimeError, "Error while getting display connection to X server"); |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + |
| 103 | + // Set the intended detectable auto repeat |
| 104 | + Bool supported = 1; |
| 105 | + Bool result = XkbSetDetectableAutoRepeat(display, enable, &supported); |
| 106 | + XFlush(display); |
| 107 | + |
| 108 | + if(!supported) { |
| 109 | + PyErr_SetString(PyExc_NotImplementedError, "Setting detectable auto repeat not supported by X server"); |
| 110 | + return NULL; |
| 111 | + } |
| 112 | + if(enable != result) { |
| 113 | + PyErr_SetString(PyExc_RuntimeError, "Could not set detectable auto repeat"); |
| 114 | + return NULL; |
| 115 | + } |
| 116 | + |
| 117 | + Py_INCREF(Py_None); |
| 118 | + return Py_None; |
| 119 | +} |
| 120 | + |
| 121 | +// Python function tkdar.enable() handler |
| 122 | +static PyObject *tkdar_ena(PyObject *s, PyObject *arg) |
| 123 | +{ |
| 124 | + (void)s; |
| 125 | + return tkdar(arg, 1); |
| 126 | +} |
| 127 | + |
| 128 | +// Python function tkdar.disable() handler |
| 129 | +static PyObject *tkdar_dis(PyObject *s, PyObject *arg) |
| 130 | +{ |
| 131 | + (void)s; |
| 132 | + return tkdar(arg, 0); |
| 133 | +} |
| 134 | + |
| 135 | +static PyMethodDef tkdar_methods[] = { |
| 136 | + {"enable", (PyCFunction)tkdar_ena, METH_O, "Enable detectable auto repeat"}, |
| 137 | + {"disable", (PyCFunction)tkdar_dis, METH_O, "Disable detectable auto repeat"}, |
| 138 | + {} |
| 139 | +}; |
| 140 | + |
| 141 | +static struct PyModuleDef tkdar_moduledef = { |
| 142 | + .m_base = PyModuleDef_HEAD_INIT, |
| 143 | + .m_name = "tkdar", |
| 144 | + .m_doc = "Detectable auto repeat extension for Tk/Tkinter", |
| 145 | + .m_size = -1, |
| 146 | + .m_methods = tkdar_methods, |
| 147 | +}; |
| 148 | + |
| 149 | +PyMODINIT_FUNC PyInit_tkdar(void); |
| 150 | +PyMODINIT_FUNC PyInit_tkdar(void) |
| 151 | +{ |
| 152 | + PyObject *m = PyModule_Create(&tkdar_moduledef); |
| 153 | + return m; |
| 154 | +} |
| 155 | +// vim: ts=4 shiftwidth=4 |
0 commit comments