Thread Tools Display Modes
10-28-12, 06:33 PM   #1
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Frame to show on modifier key down. Need help.

Hi, I want a frame that I created in Lua to show when I hold down the alt key but not sure how to go about doing it. And then I want it to hide when I am not holding down the alt key. Can anyone help me?

I've tried this:
Code:
Frame:SetScript("OnUpdate",
	if IsAltKeyDown() and not IsControlKeyDown() and not IsShiftKeyDown() then
	Frame:Show()
	else
	Frame:Hide()
end)
But I am pretty sure that is completely wrong.
Thank you!
  Reply With Quote
10-28-12, 06:54 PM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
Code:
local eventFrame = CreateFrame("Frame")
eventFrame.OnEvent = function(self,event,...)
  return self[event] and self[event](...)
end
eventFrame:SetScript("OnEvent",eventFrame.OnEvent)
eventFrame:RegisterEvent("MODIFIER_STATE_CHANGED")
eventFrame.MODIFIER_STATE_CHANGED = function(...)
  local key,pressed = ...
  -- doesn't check the state of other modifier keys. Add 
  -- 'and not (IsControlKeyDown() or IsShiftKeyDown())'
  -- (without the quotes) before 'then' if you need that
  if key == "LALT" or key == "RALT" and pressed == 1 then
    Frame:Show()
  else
    Frame:Hide()
  end
  -- alternatively
  -- if IsAltKeyDown() and not (IsControlKeyDown() or IsShiftKeyDown()) then
  --   Frame:Show()
  -- else
  --   Frame:Hide()
  -- end
end
That's assuming Frame is created somewhere else ofc.

Last edited by Dridzt : 10-28-12 at 06:59 PM.
  Reply With Quote
10-28-12, 08:46 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Your original code would work if you added the missing "end" near the end, but using an OnUpdate is terribly inefficient. Dridzt's method is better, though I would suggest passing the frame reference to the event functions so you can keep it consistent with method (colon) notation and use self inside the function:

Code:
Frame:SetScript("OnEvent", function(self, event, ...)
    return self[event] and self[event](self, ...)
end)

Frame:RegisterEvent("MODIFIER_STATE_CHANGED")

function Frame:MODIFIER_STATE_CHANGED(key, state)
    if state == 1 and (key == "LALT" or key == "RALT") then
        self:Show()
    else
        self:Hide()
    end
end
If your frame is already responding to other events, just move the code for each event into its own function, with the same name as the event.

Also, WoW used to get confused by alt-tabbing into thinking that Alt was still pressed when it wasn't; I'm not sure if this bug still exists, but if your frame gets stuck in the wrong state, that's probably why.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
10-29-12, 05:40 AM   #4
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Thank you both that worked perfectly! Thought it would have worked similar to the KgPanels script I use so I would have never of got that.

Also not sure why my OnClick script is not working:

Code:
chatButton:SetScript("OnClick", function (self, button, down)
if pressed then
	if GetMouseButtonClicked() == "LeftButton" then
	chatBox:Hide()
	end
end
end)
If that is very wrong then I do not expect anyone to solve this for me but if you have any tips that may help me that would be fantastic
Thank you for your help!
  Reply With Quote
10-29-12, 06:11 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You are checking to see if the value of a variable named pressed is something other than nil/false, but since that variable is not defined (at least not in the code you posted) it is always nil, so the check always fails, so the code inside the check never executes.

Also, which button was clicked is passed into the script; you don't need to call GetMouseButtonClicked() to get that information.

Finally, unless your button is registered for both up-clicks and down-clicks (eg. kgPanels) you don't need to verify that the click is a down-click, and you only need to verify that the button is the LeftButton if you want to do something else on other types of clicks and registered your button for other types of clicks.

Code:
chatButton:SetScript("OnClick", function(self, button, down)
	if down and button == "LeftButton" then
		chatBox:Hide()
	end
end)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
10-29-12, 07:59 AM   #6
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Thank you, that makes sense. For some reason the code still does not hide the chatBox frame but I think maybe that is to do with something else that I still need to figure out. Thank you though for your help Phanx
  Reply With Quote
10-29-12, 03:12 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, without seeing the rest of your code there’s nothing I can do to help you with that. >_<
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Frame to show on modifier key down. Need help.


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off