IsMouseDown
bool IsMouseDown(ImGuiMouseButton button);
Check if mouse button is being held.
Mouse button (0=left, 1=right, 2=middle)
True if button is currently down
Example:
if (ImGui::IsMouseDown(ImGuiMouseButton_Left))
{
ImGui::Text("Left mouse button is held");
}
IsMouseClicked
bool IsMouseClicked(ImGuiMouseButton button, bool repeat = false);
Check if mouse button was clicked (went from !Down to Down). Same as GetMouseClickedCount() == 1.
If true, returns true on repeat clicks
True if button was just clicked
Example:
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left))
{
ProcessClick();
}
IsMouseReleased
bool IsMouseReleased(ImGuiMouseButton button);
Check if mouse button was released (went from Down to !Down).
True if button was just released
IsMouseDoubleClicked
bool IsMouseDoubleClicked(ImGuiMouseButton button);
Check if mouse button was double-clicked. Same as GetMouseClickedCount() == 2.
True if button was double-clicked
A double-click will also report IsMouseClicked() == true.
Example:
if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
{
OpenItem();
}
GetMouseClickedCount
int GetMouseClickedCount(ImGuiMouseButton button);
Return the number of successive mouse clicks at the time where a click happened.
Number of clicks (0=not clicked, 1=single click, 2=double click, 3=triple click, etc.)
Example:
int clicks = ImGui::GetMouseClickedCount(ImGuiMouseButton_Left);
if (clicks == 1) SingleClick();
else if (clicks == 2) DoubleClick();
else if (clicks == 3) TripleClick();
Mouse Position
GetMousePos
Get mouse position. Shortcut to ImGui::GetIO().MousePos.
Mouse position in screen coordinates
Example:
ImVec2 mouse_pos = ImGui::GetMousePos();
ImGui::Text("Mouse Position: (%.1f, %.1f)", mouse_pos.x, mouse_pos.y);
ImVec2 GetMousePosOnOpeningCurrentPopup();
Retrieve mouse position at the time of opening popup we have BeginPopup() into. Useful to avoid user backing that value themselves.
Mouse position when popup was opened
IsMousePosValid
bool IsMousePosValid(const ImVec2* mouse_pos = NULL);
Check if mouse position is valid. By convention, we use (-FLT_MAX, -FLT_MAX) to denote no mouse available.
mouse_pos
const ImVec2*
default:"NULL"
Mouse position to check (NULL = use current mouse position)
True if position is valid
Mouse Dragging
IsMouseDragging
bool IsMouseDragging(ImGuiMouseButton button, float lock_threshold = -1.0f);
Check if mouse is dragging. Uses io.MouseDraggingThreshold if lock_threshold < 0.0f.
Distance threshold before considering dragging. Use -1.0f for default threshold.
True if mouse is dragging
Example:
if (ImGui::IsMouseDragging(ImGuiMouseButton_Left))
{
ImVec2 delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Left);
ImGui::Text("Dragging: (%.1f, %.1f)", delta.x, delta.y);
}
GetMouseDragDelta
ImVec2 GetMouseDragDelta(ImGuiMouseButton button = 0, float lock_threshold = -1.0f);
Return the delta from the initial clicking position while the mouse button is pressed or was just released. This is locked and returns 0.0f until the mouse moves past a distance threshold at least once.
button
ImGuiMouseButton
default:"0"
Mouse button to check
Distance threshold. Use -1.0f for io.MouseDraggingThreshold.
Example:
if (ImGui::IsMouseDragging(ImGuiMouseButton_Left))
{
ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Left);
object_position.x += drag_delta.x;
object_position.y += drag_delta.y;
ImGui::ResetMouseDragDelta(ImGuiMouseButton_Left);
}
ResetMouseDragDelta
void ResetMouseDragDelta(ImGuiMouseButton button = 0);
Reset drag delta for a mouse button.
button
ImGuiMouseButton
default:"0"
Mouse button to reset
Mouse Hovering
IsMouseHoveringRect
bool IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true);
Check if mouse is hovering given bounding rect in screen space. Clipped by current clipping settings, but disregarding of other consideration of focus/window ordering/popup-block.
Top-left corner of rectangle
Bottom-right corner of rectangle
Whether to respect current clipping rectangle
True if mouse is within rectangle
Example:
ImVec2 min = ImVec2(100, 100);
ImVec2 max = ImVec2(200, 200);
if (ImGui::IsMouseHoveringRect(min, max))
{
ImGui::GetWindowDrawList()->AddRectFilled(min, max, IM_COL32(255, 0, 0, 64));
}
Mouse Cursor
GetMouseCursor
ImGuiMouseCursor GetMouseCursor();
Get desired mouse cursor shape. Important: reset in ImGui::NewFrame(), updated during the frame. Valid before Render().
SetMouseCursor
void SetMouseCursor(ImGuiMouseCursor cursor_type);
Set desired mouse cursor shape.
Example:
if (IsItemHovered())
{
ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
}
Mouse Cursor Types
enum ImGuiMouseCursor_
{
ImGuiMouseCursor_None = -1,
ImGuiMouseCursor_Arrow = 0, // Standard arrow
ImGuiMouseCursor_TextInput, // When hovering over InputText, etc.
ImGuiMouseCursor_ResizeAll, // Unused by ImGui
ImGuiMouseCursor_ResizeNS, // When hovering horizontal border
ImGuiMouseCursor_ResizeEW, // When hovering vertical border
ImGuiMouseCursor_ResizeNESW, // When hovering bottom-left corner
ImGuiMouseCursor_ResizeNWSE, // When hovering bottom-right corner
ImGuiMouseCursor_Hand, // Unused by ImGui (e.g., for hyperlinks)
ImGuiMouseCursor_Wait, // When waiting for something to process/load
ImGuiMouseCursor_Progress, // When waiting but application still interactive
ImGuiMouseCursor_NotAllowed, // When hovering disallowed interaction
ImGuiMouseCursor_COUNT
};
enum ImGuiMouseButton_
{
ImGuiMouseButton_Left = 0,
ImGuiMouseButton_Right = 1,
ImGuiMouseButton_Middle = 2,
ImGuiMouseButton_COUNT = 5
};
These values are guaranteed to be stable. You can use 0/1 directly.
Mouse Capture
SetNextFrameWantCaptureMouse
void SetNextFrameWantCaptureMouse(bool want_capture_mouse);
Override io.WantCaptureMouse flag next frame. Equivalent to setting io.WantCaptureMouse after the next NewFrame() call.
Common Patterns
ImGui::InvisibleButton("drag_area", ImVec2(200, 200));
if (ImGui::IsItemActive() && ImGui::IsMouseDragging(ImGuiMouseButton_Left))
{
ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Left);
object_pos.x += drag_delta.x;
object_pos.y += drag_delta.y;
ImGui::ResetMouseDragDelta(ImGuiMouseButton_Left);
}
Click Detection with Double-Click
if (ImGui::IsItemClicked())
{
if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
OpenItem();
else
SelectItem();
}
if (ImGui::IsMouseClicked(ImGuiMouseButton_Right) &&
ImGui::IsMouseHoveringRect(item_min, item_max))
{
ImGui::OpenPopup("context_menu");
}
See Also
- Keyboard - Keyboard input functions
- Input - General input and ImGuiIO functions
- Shortcuts - Keyboard shortcuts