From 915bb40c2437d2ba0ed81d1e065cc53bce844475 Mon Sep 17 00:00:00 2001 From: RattleSN4K3 Date: Sat, 25 Feb 2017 17:04:17 +0100 Subject: [PATCH 1/2] Fix controller keyboard simulation by reverting 2180f0bc4dec45942470cb505f819b8fdef67953. Also added additional comment about that specific if-check. --- src/base/UMain.pas | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/base/UMain.pas b/src/base/UMain.pas index 640fd250b..93405a23d 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 + (Event.key.keysym.unicode = 0) // 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 From 417937ce181b4692b8373bfb1341010b8431714f Mon Sep 17 00:00:00 2001 From: RattleSN4K3 Date: Mon, 27 Feb 2017 13:07:35 +0100 Subject: [PATCH 2/2] Attempt to prevent duplicate characters caused by keyboard-input simulation By using an undefined unicode value (here: max Uint) to distinguish real character input and simulated ones. On some systems the additional KeyDown event was also translated into a character which this change should fix by comparing some event fields. Also moved the check from the main code to the modular Joystick file/implementation --- src/base/UJoystick.pas | 10 ++++++++-- src/base/UMain.pas | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) 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 93405a23d..a29f346a1 100644 --- a/src/base/UMain.pas +++ b/src/base/UMain.pas @@ -533,7 +533,7 @@ procedure CheckEvents; 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 - (Event.key.keysym.unicode = 0) // verify if the event is not a valid keyboard-text-input (and originating from keyboard simulation) + 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);