fix: handle paste events in wasm backend

Listen for browser paste events and directly inject clipboard text via
AddInputCharactersUTF8. Also suppress character input when Ctrl/Meta is
held to prevent 'v' from being typed on Cmd+V.
This commit is contained in:
DaniPopes
2026-02-11 23:42:45 -05:00
parent ea8eca6a45
commit 7d27fe6ff9
2 changed files with 14 additions and 2 deletions

View File

@@ -299,7 +299,7 @@ if(NOT EMSCRIPTEN)
endif()
if(EMSCRIPTEN)
target_link_options(${PROJECT_NAME} PRIVATE -pthread -sASSERTIONS=0 -sINITIAL_MEMORY=384mb -sALLOW_MEMORY_GROWTH=1 -sMAXIMUM_MEMORY=4gb -sSTACK_SIZE=1048576 -sWASM_BIGINT=1 -sPTHREAD_POOL_SIZE=8 -sEXPORTED_FUNCTIONS=_main,_nativeOpenFile -sEXPORTED_RUNTIME_METHODS=ccall -sENVIRONMENT=web,worker --preload-file embed.tracy)
target_link_options(${PROJECT_NAME} PRIVATE -pthread -sASSERTIONS=0 -sINITIAL_MEMORY=384mb -sALLOW_MEMORY_GROWTH=1 -sMAXIMUM_MEMORY=4gb -sSTACK_SIZE=1048576 -sWASM_BIGINT=1 -sPTHREAD_POOL_SIZE=8 -sEXPORTED_FUNCTIONS=_main,_nativeOpenFile,_tracy_paste_clipboard -sEXPORTED_RUNTIME_METHODS=ccall -sENVIRONMENT=web,worker --preload-file embed.tracy)
file(DOWNLOAD https://share.nereid.pl/i/embed.tracy ${CMAKE_CURRENT_BINARY_DIR}/embed.tracy EXPECTED_MD5 ca0fa4f01e7b8ca5581daa16b16c768d)
file(COPY ${CMAKE_CURRENT_LIST_DIR}/wasm/index.html DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

View File

@@ -26,6 +26,12 @@ static uint64_t s_time;
static const char* s_prevCursor = nullptr;
static std::string s_clipboard;
extern "C" void tracy_paste_clipboard( const char* text )
{
s_clipboard = text;
ImGui::GetIO().AddInputCharactersUTF8( text );
}
static void SetClipboard( ImGuiContext*, const char* text )
{
s_clipboard = text;
@@ -240,7 +246,7 @@ Backend::Backend( const char* title, const std::function<void()>& redraw, const
const auto code = TranslateKeyCode( e->code );
if( code == ImGuiKey_None ) return EM_FALSE;
ImGui::GetIO().AddKeyEvent( code, true );
if( e->key[0] && !e->key[1] ) ImGui::GetIO().AddInputCharacter( *e->key );
if( e->key[0] && !e->key[1] && !e->ctrlKey && !e->metaKey ) ImGui::GetIO().AddInputCharacter( *e->key );
return EM_TRUE;
} );
emscripten_set_keyup_callback( EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_TRUE, [] ( int, const EmscriptenKeyboardEvent* e, void* ) -> EM_BOOL {
@@ -249,6 +255,12 @@ Backend::Backend( const char* title, const std::function<void()>& redraw, const
ImGui::GetIO().AddKeyEvent( code, false );
return EM_TRUE;
} );
EM_ASM( {
document.addEventListener( 'paste', function( e ) {
var text = ( e.clipboardData || window.clipboardData ).getData( 'text' );
if( text ) ccall( 'tracy_paste_clipboard', 'void', ['string'], [text] );
} );
} );
s_time = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
}