Run when the button is clicked. By default, this script is only run for the left mouse button's "up" action; the :RegisterForClicks() method can be called to enable the button to respond to other buttons and actions.

Using or hooking the OnClick handler may not always be useful or desirable; the PreClick and PostClick scripts are provided for such purposes.

Moving the mouse away from the button before releasing it will not run the PreClick/OnClick/PostClick handlers, but will still run the OnMouseUp handler.

Signature:

OnClick(self, "button", down)

Arguments:

  • self - Reference to the widget for which the script was run (button)

  • button - Name of the mouse button responsible for the click action (string)

    • Button4
    • Button5
    • LeftButton
    • MiddleButton
    • RightButton

  • down - True for a mouse button down action; false for button up or other actions (boolean)

Examples:

-- Illustrates the timing of mouse script handlers when clicking a button
local b = CreateFrame("Button", "TestButton", UIParent, "UIPanelButtonTemplate2")
b:SetPoint("CENTER")
b:RegisterForClicks("AnyUp", "AnyDown")
local upDown = { [false] = "Up", [true] = "Down" }
local function show(text, color)
  DEFAULT_CHAT_FRAME:AddMessage(text, color, color, color)
end
local color
b:SetScript("OnMouseDown", function(self, button)
  color = .60
  show(format("OnMouseDown: %s", button), color, color, color)
end)
b:SetScript("OnMouseUp", function(self, button)
  color = .60
  show(format("OnMouseUp: %s", button), color, color, color)
end)
b:SetScript("OnClick", function(self, button, down)
  color = color + 0.1
  show(format("OnClick: %s %s", button, upDown[down]), color, color, color)
end)
b:SetScript("PreClick", function(self, button, down)
  color = color + 0.1
  show(format("PreClick: %s %s", button, upDown[down]), color, color, color)
end)
b:SetScript("PostClick", function(self, button,down)
  color = color + 0.1
  show(format("PostClick: %s %s", button, upDown[down]),  color, color, color)
end)