View Single Post
01-30-12, 07:22 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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:
  1. local replace = string.gsub
  2.  
  3. local function updatehotkey(self, actionButtonType)
  4.     local hotkey = _G[self:GetName() .. 'HotKey']
  5.     local text = hotkey:GetText()
  6.    
  7.     text = replace(text, 's%-', 'S')
  8.     text = replace(text, 'a%-', 'A')
  9.     text = replace(text, 'c%-', 'C')
  10.     text = replace(text, 'Mouse Button ', 'M')
  11.     text = replace(text, 'Mouse Wheel Up', 'MU')
  12.     text = replace(text, 'Mouse Wheel Down', 'MD')
  13.     text = replace(text, 'Middle Mouse', 'M3')
  14.     text = replace(text, 'Num Pad ', 'N')
  15.     text = replace(text, 'Page Up', 'PU')
  16.     text = replace(text, 'Page Down', 'PD')
  17.     text = replace(text, 'Spacebar', 'SpB')
  18.     text = replace(text, 'Insert', 'Ins')
  19.     text = replace(text, 'Home', 'Hm')
  20.     text = replace(text, 'Delete', 'Del')
  21.    
  22.     if hotkey:GetText() == RANGE_INDICATOR then
  23.         hotkey:SetText('')
  24.     else
  25.         hotkey:SetText(text)
  26.     end
  27. end
  28.  
  29. 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.

Last edited by Phanx : 01-30-12 at 07:24 PM.
  Reply With Quote