Skip to content

Commit 57c3222

Browse files
authored
Merge pull request #3357 from pygame-community/ankith26-fix-event-keydown
Fix unicode property when keyup blocked
2 parents 33317a1 + 004f4f1 commit 57c3222

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src_c/event.c

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ static char released_mouse_buttons[5] = {0};
131131
}
132132
#endif /* not on emscripten */
133133

134+
static Uint32
135+
_pg_pgevent_proxify(Uint32 type);
136+
static Uint32
137+
_pg_pgevent_deproxify(Uint32 type);
138+
134139
#if SDL_VERSION_ATLEAST(3, 0, 0)
135140
static Uint32
136141
_pg_repeat_callback(void *param, SDL_TimerID timerID, Uint32 interval)
@@ -283,11 +288,28 @@ _pg_strip_utf8(const char *str, char *ret)
283288
}
284289
}
285290

291+
/* Go over our cache, deleting entries with the same scancode */
292+
static void
293+
_pg_del_event_unicode(SDL_Event *event)
294+
{
295+
for (int i = 0; i < MAX_SCAN_UNICODE; i++) {
296+
#if SDL_VERSION_ATLEAST(3, 0, 0)
297+
if (scanunicode[i].key == event->key.scancode)
298+
#else
299+
if (scanunicode[i].key == event->key.keysym.scancode)
300+
#endif
301+
{
302+
/* Mark as free real estate for other events to occupy */
303+
scanunicode[i].key = 0;
304+
}
305+
}
306+
}
307+
286308
static int
287309
_pg_put_event_unicode(SDL_Event *event, const char *uni)
288310
{
289-
int i;
290-
for (i = 0; i < MAX_SCAN_UNICODE; i++) {
311+
_pg_del_event_unicode(event);
312+
for (int i = 0; i < MAX_SCAN_UNICODE; i++) {
291313
if (!scanunicode[i].key) {
292314
#if SDL_VERSION_ATLEAST(3, 0, 0)
293315
scanunicode[i].key = event->key.scancode;
@@ -304,17 +326,14 @@ _pg_put_event_unicode(SDL_Event *event, const char *uni)
304326
static PyObject *
305327
_pg_get_event_unicode(SDL_Event *event)
306328
{
307-
/* We only deal with one byte here, but still declare an array to silence
308-
* compiler warnings. The other 3 bytes are unused */
309-
char c[4];
310-
int i;
311-
for (i = 0; i < MAX_SCAN_UNICODE; i++) {
329+
for (int i = 0; i < MAX_SCAN_UNICODE; i++) {
312330
#if SDL_VERSION_ATLEAST(3, 0, 0)
313331
if (scanunicode[i].key == event->key.scancode) {
314332
#else
315333
if (scanunicode[i].key == event->key.keysym.scancode) {
316334
#endif
317-
if (event->type == SDL_KEYUP) {
335+
if (event->type == SDL_KEYUP ||
336+
PG_EventEnabled(_pg_pgevent_proxify(SDL_KEYUP)) == SDL_FALSE) {
318337
/* mark the position as free real estate for other
319338
* events to occupy. */
320339
scanunicode[i].key = 0;
@@ -323,12 +342,10 @@ _pg_get_event_unicode(SDL_Event *event)
323342
}
324343
}
325344
/* fallback to function that determines unicode from the event.
326-
* We try to get the unicode attribute, and store it in memory*/
327-
*c = _pg_unicode_from_event(event);
328-
if (_pg_put_event_unicode(event, c)) {
329-
return _pg_get_event_unicode(event);
330-
}
331-
return PyUnicode_FromString("");
345+
* We don't need to store this in our cache because this is entirely
346+
* determined from the event fields, and therefore needs no other info. */
347+
char fallback[] = {_pg_unicode_from_event(event), '\0'};
348+
return PyUnicode_FromString(fallback);
332349
}
333350

334351
#define _PG_HANDLE_PROXIFY(name) \
@@ -602,6 +619,10 @@ pg_event_filter(void *_, SDL_Event *event)
602619

603620
else if (event->type == SDL_KEYUP) {
604621
PG_LOCK_EVFILTER_MUTEX
622+
/* Actual keyup is blocked, so clear unneeded cache if it exists */
623+
if (PG_EventEnabled(_pg_pgevent_proxify(SDL_KEYUP)) == SDL_FALSE) {
624+
_pg_del_event_unicode(event);
625+
}
605626
#if SDL_VERSION_ATLEAST(3, 0, 0)
606627
released_keys[event->key.scancode] = 1;
607628
if (_pg_repeat_timer &&

0 commit comments

Comments
 (0)