Make keyboard event work with SDL2

This commit is contained in:
Thomas Bernard 2018-06-19 13:32:55 +02:00
parent 22e0bcc01f
commit 501ce2a4ad
3 changed files with 51 additions and 15 deletions

View File

@ -42,7 +42,9 @@
#include "loadsave.h"
#if defined(USE_SDL)
#define RSUPER_EMULATES_META_MOD
#endif
// Keyboards with a Super key never seem to have a Meta key at the same time.
// This setting allows the right 'Super' key (the one with a 'Windows' or
// 'Amiga' label to be used as a modifier instead of a normal key.
@ -391,7 +393,6 @@ int Handle_mouse_release(SDL_MouseButtonEvent event)
// Keyboard management
#if defined(USE_SDL)
int Handle_key_press(SDL_KeyboardEvent event)
{
//Appui sur une touche du clavier
@ -399,8 +400,10 @@ int Handle_key_press(SDL_KeyboardEvent event)
Key = Keysym_to_keycode(event.keysym);
Key_ANSI = Keysym_to_ANSI(event.keysym);
#if defined(USE_SDL)
Key_UNICODE = event.keysym.unicode;
if (Key_UNICODE == 0)
#endif
Key_UNICODE = Key_ANSI;
switch(event.keysym.sym)
{
@ -420,8 +423,13 @@ int Handle_key_press(SDL_KeyboardEvent event)
modifier=MOD_ALT;
break;
#if defined(USE_SDL2)
case SDLK_RGUI:
case SDLK_LGUI:
#else
case SDLK_RMETA:
case SDLK_LMETA:
#endif
modifier=MOD_META;
break;
@ -484,7 +492,6 @@ int Handle_key_press(SDL_KeyboardEvent event)
}
return 0;
}
#endif
int Release_control(int key_code, int modifier)
{
@ -560,7 +567,6 @@ int Release_control(int key_code, int modifier)
}
#if defined(USE_SDL)
int Handle_key_release(SDL_KeyboardEvent event)
{
int modifier;
@ -591,8 +597,13 @@ int Handle_key_release(SDL_KeyboardEvent event)
break;
#endif
#if defined(USE_SDL2)
case SDLK_RGUI:
case SDLK_LGUI:
#else
case SDLK_RMETA:
case SDLK_LMETA:
#endif
modifier=MOD_META;
break;
@ -601,7 +612,6 @@ int Handle_key_release(SDL_KeyboardEvent event)
}
return Release_control(released_key, modifier);
}
#endif
// Joystick management
@ -939,19 +949,12 @@ int Get_input(int sleep_time)
#endif
case SDL_KEYDOWN:
#if defined(USE_SDL)
Handle_key_press(event.key);
#else
//TODO SDL2
#endif
user_feedback_required = 1;
break;
case SDL_KEYUP:
#if defined(USE_SDL)
Handle_key_release(event.key);
//TODO SDL2
#endif
break;
// Start of Joystik handling

View File

@ -23,7 +23,7 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#if defined(USE_SDL)
#if defined(USE_SDL) || defined(USE_SDL2)
#include <SDL.h>
#endif
#include "global.h"
@ -346,8 +346,15 @@ word Get_Key_modifiers(void)
return modifiers;
}
#endif
#if defined(USE_SDL) || defined(USE_SDL2)
#if defined(USE_SDL)
word Keysym_to_keycode(SDL_keysym keysym)
#elif defined(USE_SDL2)
word Keysym_to_keycode(SDL_Keysym keysym)
#endif
{
word key_code = 0;
word mod;
@ -356,23 +363,39 @@ word Keysym_to_keycode(SDL_keysym keysym)
if (keysym.sym == SDLK_RSHIFT || keysym.sym == SDLK_LSHIFT ||
keysym.sym == SDLK_RCTRL || keysym.sym == SDLK_LCTRL ||
keysym.sym == SDLK_RALT || keysym.sym == SDLK_LALT ||
#if defined(USE_SDL2)
keysym.sym == SDLK_RGUI || keysym.sym == SDLK_LGUI ||
#else
keysym.sym == SDLK_RMETA || keysym.sym == SDLK_LMETA ||
#endif
keysym.sym == SDLK_MODE) // AltGr
return 0;
// Les touches qui n'ont qu'une valeur unicode (très rares)
// seront codées sur 11 bits, le 12e bit est mis à 1 (0x0800)
if (keysym.sym != 0)
key_code = keysym.sym;
key_code = K2K(keysym.sym);
else if (keysym.scancode != 0)
{
key_code = (keysym.scancode & 0x07FF) | 0x0800;
}
#if defined(USE_SDL)
// Normally I should test keysym.mod here, but on windows the implementation
// is buggy: if you release a modifier key, the following keys (when they repeat)
// still name the original modifiers.
mod = Get_Key_modifiers();
#else
mod = 0;
if (keysym.mod & KMOD_CTRL)
mod |= MOD_CTRL;
if (keysym.mod & KMOD_SHIFT )
mod |= MOD_SHIFT;
if (keysym.mod & (KMOD_ALT|KMOD_MODE))
mod |= MOD_ALT;
if (keysym.mod & (KMOD_GUI))
mod |= MOD_META;
#endif
// SDL_GetModState() seems to get the right up-to-date info.
key_code |= mod;
@ -764,7 +787,13 @@ word Keysym_to_ANSI(SDL_keysym keysym)
return keysym.sym;
}
#elif defined(USE_SDL2)
// SDL2 TODO
word Keysym_to_ANSI(SDL_Keysym keysym)
{
if (keysym.sym < 128)
return (word)keysym.sym;
return K2K(keysym.sym);
}
word Key_for_scancode(word scancode)
{
return scancode;

View File

@ -51,6 +51,8 @@
*/
#if defined(USE_SDL)
word Keysym_to_ANSI(SDL_keysym keysym);
#elif defined(USE_SDL2)
word Keysym_to_ANSI(SDL_Keysym keysym);
#endif
/*!
@ -62,6 +64,8 @@ word Keysym_to_ANSI(SDL_keysym keysym);
*/
#if defined(USE_SDL)
word Keysym_to_keycode(SDL_keysym keysym);
#elif defined(USE_SDL2)
word Keysym_to_keycode(SDL_Keysym keysym);
#endif
/*!