Skip to content

Commit 7e33065

Browse files
committed
dos: handle text input events
Basic TEXTINPUT event firing that assumes US keyboard layout.
1 parent 6529694 commit 7e33065

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

src/core/dos/SDL_dos.c

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,19 @@ static const SDL_Scancode extended_key_to_sdl_scancode[128] = {
258258
0, /* 127 0x7f */
259259
};
260260

261+
static const char shift_digits[16] = {
262+
')', /* 0 */
263+
'!', /* 1 */
264+
'@', /* 2 */
265+
'#', /* 3 */
266+
'$', /* 4 */
267+
'%', /* 5 */
268+
'^', /* 6 */
269+
'&', /* 7 */
270+
'*', /* 8 */
271+
'(', /* 9 */
272+
};
273+
261274
static volatile Uint8 scancode_buf[100];
262275
static volatile int scancode_count;
263276

@@ -338,29 +351,55 @@ DOS_InitKeyboard(void)
338351
}
339352

340353
static void
341-
DOS_ProcessScancode(Uint8 scancode)
354+
DOS_ProcessScancode(Uint8 raw)
342355
{
343356
static SDL_bool extended_key = SDL_FALSE;
344-
Uint8 state = scancode & 0x80 ? SDL_RELEASED : SDL_PRESSED;
357+
Uint8 state = raw & 0x80 ? SDL_RELEASED : SDL_PRESSED;
358+
SDL_Scancode scancode;
345359

346360
/* Check if the code is an extended key prefix. */
347-
if (scancode == 0xE0) {
361+
if (raw == 0xE0) {
348362
extended_key = SDL_TRUE;
349363
return;
350364
}
351365

352366
/* Mask off state bit. */
353-
scancode &= 0x7F;
367+
raw &= 0x7F;
354368

355-
/* Generate SDL key event. */
369+
/* Convert to SDL scancode. */
356370
if (extended_key) {
357-
SDL_SendKeyboardKey(state, extended_key_to_sdl_scancode[scancode]);
371+
scancode = extended_key_to_sdl_scancode[raw];
358372
} else {
359-
SDL_SendKeyboardKey(state, bios_to_sdl_scancode[scancode]);
373+
scancode = bios_to_sdl_scancode[raw];
360374
}
361375

362376
/* Reset extended key flag. */
363377
extended_key = SDL_FALSE;
378+
379+
/* Send a key event. Return if it wasn't posted. */
380+
if (SDL_SendKeyboardKey(state, scancode) == 0) return;
381+
382+
/* If text input events are enabled, send one with basic US layout conversion. */
383+
if (state == SDL_PRESSED && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE) {
384+
const SDL_Keymod modstate = SDL_GetModState();
385+
const SDL_Keycode keycode = SDL_GetKeyFromScancode(scancode);
386+
SDL_Log("Text input code: %ld", keycode);
387+
if (((modstate & (KMOD_CTRL | KMOD_ALT)) == 0) &&
388+
keycode >= SDLK_SPACE && keycode <= SDLK_z) {
389+
char buf[2];
390+
if ((modstate & KMOD_SHIFT)) {
391+
if (keycode >= SDLK_0 && keycode <= SDLK_9) {
392+
buf[0] = shift_digits[keycode - '0'];
393+
} else if (keycode >= SDLK_a && keycode <= SDLK_z) {
394+
buf[0] = keycode - ('a' - 'A');
395+
}
396+
} else {
397+
buf[0] = (char)keycode;
398+
}
399+
buf[1] = '\0';
400+
SDL_SendKeyboardText(buf);
401+
}
402+
}
364403
}
365404

366405
static void

0 commit comments

Comments
 (0)