Button:RegisterForMouse

From Warcraft Wiki
Jump to navigation Jump to search

Registers the button widget to receive OnMouse script events.

Button:RegisterForMouse([button1, ...])

Arguments

button1, ...
string? - A list of combinations of mouse buttons and their click action.
AnyUp - Responds to the up action of any mouse button.
AnyDown - Responds to the down action of any mouse button.
LeftButtonUp, LeftButtonDown
RightButtonUp, RightButtonDown
MiddleButtonUp, MiddleButtonDown
Button4Up, Button4Down
Button5Up, Button5Down

Details

  • The default buttons registered on new button objects are AnyUp, AnyDown.
  • 📝 In order for the OnMouseUp script event to fire, the respective OnMouseDown button needs to be registered as well.
  • Pass multiple arguments to register several buttons, or none to remove any previous registrations. Subsequent calls replace any prior ones.
  • Controls the OnMouseDown, OnMouseUp script events.

Example

Creates a button that only responds to right mouse up/down clicks.

local btn = CreateFrame("Button", nil, UIParent, "UIPanelButtonTemplate")
btn:SetPoint("CENTER")
btn:SetSize(100, 40)
btn:SetText("Click me")

-- "AnyDown", "AnyUp" is the default behavior
btn:RegisterForMouse("RightButtonDown", "RightButtonUp")

-- only the OnMouseUp/Down scripts are affected by .RegisterForMouse
btn:SetScript("OnMouseDown", function(self, button)
	print("OnMouseDown", button)
end)

btn:SetScript("OnMouseUp", function(self, button)
	print("OnMouseUp", button)
end)