The code posted does nothing by itself. As per stako's post, you need to run that function using a secure hook on the ActionButton_UpdateHotkeys function:
Lua Code:
local replace = string.gsub
local function updatehotkey(self, actionButtonType)
local hotkey = _G[self:GetName() .. 'HotKey']
local text = hotkey:GetText()
text = replace(text, 's%-', 'S')
text = replace(text, 'a%-', 'A')
text = replace(text, 'c%-', 'C')
text = replace(text, 'Mouse Button ', 'M')
text = replace(text, 'Mouse Wheel Up', 'MU')
text = replace(text, 'Mouse Wheel Down', 'MD')
text = replace(text, 'Middle Mouse', 'M3')
text = replace(text, 'Num Pad ', 'N')
text = replace(text, 'Page Up', 'PU')
text = replace(text, 'Page Down', 'PD')
text = replace(text, 'Spacebar', 'SpB')
text = replace(text, 'Insert', 'Ins')
text = replace(text, 'Home', 'Hm')
text = replace(text, 'Delete', 'Del')
if hotkey:GetText() == RANGE_INDICATOR then
hotkey:SetText('')
else
hotkey:SetText(text)
end
end
hooksecurefunc("ActionButton_UpdateHotkeys", updatehotkey)
This will cause your function to be attached to the end of the default UI's hotkey update function, so every time that runs, your function is run immediately afterward. The "secure" part means that your function doesn't interact with the default UI's function, so it doesn't break anything or cause taint.
A few more technical observations:
- You only need to upvalue string.gsub once, not every time the function runs, so I moved that local declaration outside of the function.
- It's not necessary to wrap the search patterns in parentheses; those are only needed (or useful) if you only wish to match a subset of the given pattern. Since that's not the case here, they're not needed.
- It's not necessary to look up RANGE_INDICATOR in the _G table explicitly; this is assumed, and specifying _G adds another global lookup for no reason. Just compare to RANGE_INDICATOR directly. I also changed this in the above code.