From 239077e8e1eaf2ab9c324bbc732c349d62aedb26 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 19 Nov 2023 10:57:10 -0800 Subject: [PATCH] bugfix: initial textinput event on iOS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `love.textinput` is fragile on iOS. Various things can cause an app to stop receiving textinput events. Resizing the window is one reliable way, but there's also another ghost, something that's triggering on every frame of LÖVE. Fortunately, it looks like `love.keyboard.setTextInput(true)` reliably resubscribes the app to textinput events, regardless of their cause. https://github.com/love2d/love/issues/1959 The one remaining open question here is why the call in `App.keychord_press` (equivalent to `love.keypressed`) doesn't fix the breakage caused by the initial window resize (that happens before any keys are pressed). I've confirmed that `keypressed` comes before `textinput` on iOS just like everywhere else. --- app.lua | 10 +++++++++- main.lua | 9 ++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app.lua b/app.lua index bbdd86d..c8aed1b 100644 --- a/app.lua +++ b/app.lua @@ -68,7 +68,15 @@ function love.run() App.fake_mouse_press = nil App.fake_mouse_release = nil -- other methods dispatch to real hardware - App.screen.resize = love.window.setMode + + App.screen.resize = function(width, height, flags) + love.window.setMode(width, height, flags) + if OS == 'iOS' then + love.keyboard.setTextInput(true) -- magic. iOS seems to lose textinput events after calls to setMode. + -- https://github.com/love2d/love/issues/1959 + end + end + App.screen.size = love.window.getMode App.screen.move = love.window.setPosition App.screen.position = love.window.getPosition diff --git a/main.lua b/main.lua index eab78d1..25f5281 100644 --- a/main.lua +++ b/main.lua @@ -1,6 +1,8 @@ utf8 = require 'utf8' json = require 'json' +OS = love.system.getOS() + require 'app' require 'test' require 'live' @@ -8,8 +10,6 @@ require 'live' require 'keychord' require 'button' -OS = love.system.getOS() - -- delegate most business logic to a layer that can be reused by other projects require 'edit' Editor_state = {} @@ -216,7 +216,10 @@ function App.keychord_press(chord, key) end return end - love.keyboard.setTextInput(true) -- magic. keeps iOS from losing textinput events after switching apps. + if OS == 'iOS' then + love.keyboard.setTextInput(true) -- magic. iOS is prone to losing textinput events. + -- https://github.com/love2d/love/issues/1959 + end -- ignore events for some time after window in focus (mostly alt-tab) if Current_time < Last_focus_time + 0.01 then return