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

@@ -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();
}