diff --git a/src/base/UJoystick.pas b/src/base/UJoystick.pas index 72e5cba15..b463185fe 100644 --- a/src/base/UJoystick.pas +++ b/src/base/UJoystick.pas @@ -285,6 +285,7 @@ TJoy = class procedure InitializeJoystick; procedure FinalizeJoyStick; function HasJoyStick: boolean; +function ShouldSimulateJoystickKeyInput(Event: TSDL_event): boolean; procedure OnJoystickPollEvent(Event: TSDL_event); @@ -794,12 +795,17 @@ function TJoyController.SimulateKeyboard(Key: TSDL_KeyCode; Pressed: boolean; No JoyEvent.key.keysym.sym := Key; JoyEvent.key.keysym.scancode := SDL_GetScancodeFromKey(Key); - // always send empty/zero unicode char as workaround. Check UMain.CheckEvents - JoyEvent.key.keysym.unicode := 0; + // set un-defined unicode char to distinguish keyboard simulated events from real input events as workaround. Check UMain.CheckEvents and ShouldSimulateJoystickKeyInput + JoyEvent.key.keysym.unicode := SizeOf(Uint32); SDL_PushEvent(@JoyEvent); end; +function ShouldSimulateJoystickKeyInput(Event: TSDL_event): boolean; +begin + Result := HasJoyStick() and (Event.key.keysym.unicode = SizeOf(Uint32)); +end; + // TODO: Move to Joystick manager procedure TJoyController.SimulateMouseSend(); var diff --git a/src/base/UMain.pas b/src/base/UMain.pas index 640fd250b..a29f346a1 100644 --- a/src/base/UMain.pas +++ b/src/base/UMain.pas @@ -530,6 +530,14 @@ procedure CheckEvents; KeyCharUnicode:=UnicodeStringToUCS4String(UnicodeString(UTF8String(Event.text.text)))[0]; //KeyCharUnicode:=UnicodeStringToUCS4String(UnicodeString(Event.key.keysym.unicode))[1];//Event.text.text)[0]; except + end + // TODO: hacky workaround for enabling keyboard simulation of controllers. use SDL2 new input handling SDL_StartTextInput and SDL_StopTextInput( + else if (Event.type_ <> SDL_TEXTINPUT) and + ShouldSimulateJoystickKeyInput(Event) // verify if the event is not a valid keyboard-text-input (and originating from keyboard simulation) + then + begin + s1 := SDL_GetScancodeName(Event.key.keysym.scancode); + if Length(s1) = 1 then KeyCharUnicode := Ord(s1[1]) end; // if print is pressed -> make screenshot and save to screenshot path