From d9653a26e29a39226d4c993027ddb0e5930311d5 Mon Sep 17 00:00:00 2001 From: Justin Mitchell Date: Tue, 7 Oct 2025 01:53:48 -0400 Subject: [PATCH] feat: Add mobile keyboard support for Emscripten builds Add JavaScript hooks when text input is enabled/disabled. This allows love.keyboard.setTextInput(true) to trigger mobile keyboards on iOS/Android browsers. JavaScript implementation in love.js handles the browser security requirement for synchronous focus during touch events. Usage: Call love.keyboard.setTextInput(true) in your game and the mobile keyboard will appear when user taps the canvas. --- CMakeLists.txt | 2 +- src/modules/keyboard/sdl/Keyboard.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59a379920..7f93b116d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR}) message(FATAL_ERROR "Prevented in-tree build.") endif() -cmake_minimum_required(VERSION 3.1) +cmake_minimum_required(VERSION 3.10) project(love) diff --git a/src/modules/keyboard/sdl/Keyboard.cpp b/src/modules/keyboard/sdl/Keyboard.cpp index 51150e7fe..95cf0bea5 100644 --- a/src/modules/keyboard/sdl/Keyboard.cpp +++ b/src/modules/keyboard/sdl/Keyboard.cpp @@ -21,6 +21,10 @@ #include "Keyboard.h" #include "window/Window.h" +#ifdef __EMSCRIPTEN__ +#include +#endif + namespace love { namespace keyboard @@ -112,9 +116,29 @@ Keyboard::Scancode Keyboard::getScancodeFromKey(Key key) const void Keyboard::setTextInput(bool enable) { if (enable) + { SDL_StartTextInput(); +#ifdef __EMSCRIPTEN__ + // Call JavaScript function to show mobile keyboard + EM_ASM({ + if (typeof window.SDL_StartTextInput === 'function') { + window.SDL_StartTextInput(); + } + }); +#endif + } else + { SDL_StopTextInput(); +#ifdef __EMSCRIPTEN__ + // Call JavaScript function to hide mobile keyboard + EM_ASM({ + if (typeof window.SDL_StopTextInput === 'function') { + window.SDL_StopTextInput(); + } + }); +#endif + } } void Keyboard::setTextInput(bool enable, double x, double y, double w, double h)