1
0
Fork 0
mirror of https://github.com/ocornut/imgui.git synced 2026-01-18 01:14:19 +00:00

special handling for event capture for tab and enter

This commit is contained in:
SlowRiot 2024-12-06 17:23:22 +00:00
parent 9fd9bf9f50
commit a1decdc274

View file

@ -448,8 +448,18 @@ void ImGui_ImplEmscripten_Init() {
nullptr, // userData
false, // useCapture
[](int /*event_type*/, EmscriptenKeyboardEvent const *key_event, void */*data*/){ // callback, event_type == EMSCRIPTEN_EVENT_KEYDOWN
ImGui::GetIO().AddKeyEvent(translate_key(key_event->code), true);
return false; // the event was not consumed
auto const key{translate_key(key_event->code)};
auto &imgui_io{ImGui::GetIO()};
imgui_io.AddKeyEvent(key, true);
switch(key) { // special cases for certain key events that need to be consumed to prevent unwanted behaviour
case ImGuiKey_Tab: // consuming tab prevents the user tabbing to other parts of the browser interface outside the window content
return imgui_io.WantCaptureKeyboard; // the event was consumed only if imgui wants to capture the keyboard
case ImGuiKey_Enter: // consuming enter prevents the word "Enter" appearing in text input via the keypress callback
return imgui_io.WantTextInput; // the event was consumed only if we're currently accepting text input
default:
return false; // the event was not consumed
}
}
);
emscripten_set_keyup_callback(
@ -467,15 +477,15 @@ void ImGui_ImplEmscripten_Init() {
false, // useCapture
[](int /*event_type*/, EmscriptenKeyboardEvent const *key_event, void */*data*/){ // callback, event_type == EMSCRIPTEN_EVENT_KEYPRESS
auto &imgui_io{ImGui::GetIO()};
ImGui::GetIO().AddInputCharactersUTF8(key_event->key);
return true; // the event was consumed
imgui_io.AddInputCharactersUTF8(key_event->key);
return imgui_io.WantCaptureKeyboard; // the event was consumed only if imgui wants to capture the keyboard
}
);
emscripten_set_resize_callback(
EMSCRIPTEN_EVENT_TARGET_WINDOW, // target
nullptr, // userData
false, // useCapture
[](int /*event_type*/, EmscriptenUiEvent const *event, void */*data*/) { // event_type == EMSCRIPTEN_EVENT_RESIZE
[](int /*event_type*/, EmscriptenUiEvent const *event, void */*data*/) { // event_type == EMSCRIPTEN_EVENT_RESIZE
auto &imgui_io{ImGui::GetIO()};
imgui_io.DisplaySize.x = static_cast<float>(event->windowInnerWidth);
imgui_io.DisplaySize.y = static_cast<float>(event->windowInnerHeight);