Lua Code:
-- The form should use SecureHandlerShowHideTemplate
-- so we can handle the key binding when it's shown
MyForm = CreateFrame("Frame", "MyForm", UIParent, "SecureHandlerShowHideTemplate")
MyForm:Hide()
MyForm:SetPoint("CENTER")
MyForm:SetSize(100, 100)
MyForm:SetBackdrop{
edgeFile = "Interface\\Buttons\\WHITE8x8",
edgeSize = 1,
}
-- Create a secure button for key binding
MyToggle = CreateFrame("CheckButton", "MyFormToggle", MyForm, "SecureActionButtonTemplate")
MyToggle:Hide()
SecureHandlerSetFrameRef(MyForm, "toggle", MyToggle)
SecureHandlerExecute(MyForm, [[toggle = self:GetFrameRef("toggle")]])
SecureHandlerSetFrameRef(MyToggle, "form", MyForm)
SecureHandlerExecute(MyToggle, [[form = self:GetFrameRef("form")]])
-- Bind the BUTTON1(left-mouse) to the button when form is shown
MyForm:SetAttribute("_onshow", [[
toggle:SetBindingClick(true, "BUTTON1", "MyFormToggle", "LeftButton")
]])
-- Left-click trigger the toggle to hide the form and clear the binding
MyToggle:SetAttribute("type", "toggle")
MyToggle:SetAttribute("_toggle", [[print(123) form:Hide() self:ClearBinding("BUTTON1")]])
-- Test
MyForm:Show()