-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to capture mouse wheel in custom widget? #2704
Comments
It's not easily achievable at the moment, other than using the window flags to make the parent window itself not react to mouse wheel. |
Okay, thanks for the quick response. I came up with two workarounds:
struct MyWidget {
float wheel_counter = 0.0f;
void Draw(const char *label, ImVec2 frame_size = ImVec2(0,0)) {
ImGuiWindow* window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return;
const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);
if (frame_size.x == 0.0f)
frame_size.x = ImGui::CalcItemWidth();
if (frame_size.y == 0.0f)
frame_size.y = frame_size.x;
ImGui::BeginChild(label, frame_size);
window = ImGui::GetCurrentWindow();
window->ScrollMax.y = 1.0f;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
const ImGuiID id = window->GetID(label);
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size);
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0));
ImGui::RenderFrame(frame_bb.Min, frame_bb.Max, ImGui::GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);
ImGui::InvisibleButton(label, frame_size);
char buffer[256];
snprintf(buffer, sizeof(buffer), "Counter: %.3f", wheel_counter);
window->DrawList->PushClipRect(inner_bb.Min, inner_bb.Max, true);
window->DrawList->AddText(inner_bb.GetCenter(), ImGui::GetColorU32(ImGuiCol_Text), buffer);
window->DrawList->PopClipRect();
if (ImGui::IsItemHovered()) {
wheel_counter += ImGui::GetIO().MouseWheel;
}
ImGui::EndChild();
}
}; |
Update: I have now pushed a more generic system for input ownership, and as I result obsoleted You can now use: SetItemKeyOwner(ImGuiKey_MouseWheelY); Note that Also note that using SetKeyOwner() and SetItemKeyOwner() flags allow for variety of other behaviors outside the scope of this thread. |
Version/Branch of Dear ImGui:
Version: v1.72
Branch: master
My Issue/Question:
I am trying to implement a custom widget which captures the mouse scroll wheel when hovered. When hovered the window should not scroll (similar to when hovered over a listbox or textarea widget). Is this easily achievable?
Screenshots/Video

Video showing undesired behavior of window scrolling while mouse wheeling over the custom widget:
Standalone, minimal, complete and verifiable example: (see #2261)
The text was updated successfully, but these errors were encountered: