Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix controller keyboard simulation #243

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/base/UJoystick.pas
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ TJoy = class
procedure InitializeJoystick;
procedure FinalizeJoyStick;
function HasJoyStick: boolean;
function ShouldSimulateJoystickKeyInput(Event: TSDL_event): boolean;

procedure OnJoystickPollEvent(Event: TSDL_event);

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/base/UMain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down