12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16 ■ Binding Keys <strong>and</strong> Clicks to Addon Code 321You have just a couple more clerical matters to attend to be<strong>for</strong>e movingon to the binding functions. Fill in the Capture_OnLoad <strong>and</strong> Capture_OnShowfunctions as follows:function BindingTestCapture_OnLoad(self)self:RegisterForClicks(“AnyUp“)endfunction BindingTestCapture_OnShow(self)self.text:SetText(CAPTURE_TEXT:<strong>for</strong>mat(self.button:GetID()))endNow the frame is prepared to respond to any mouse click <strong>and</strong> also displaysinstructions <strong>for</strong> setting the binding as shown in Figure 16-9.Figure 16-9: Final binding test capture frameUsing SetBinding()With the housekeeping tasks out <strong>of</strong> the way, you can now concentrate onthe bindings themselves. The heart <strong>of</strong> the binding API is the SetBinding()function, which associates a key with a binding action:success = SetBinding(“key“[, “action“])The key parameter is a string representing the desired key press (<strong>for</strong> example,SHIFT-F5) <strong>and</strong>theaction parameter is the name <strong>of</strong> the binding action youwant the key to trigger. If the action is omitted (or nil <strong>for</strong> clarity) the binding isremoved from the key. SetBinding() returns 1 or nil depending on whetherthe binding was successfully set.The capture frame knows what action to use by looking at its buttonfield, which was set by BindingTestButton_OnClick. Sothemainjob<strong>of</strong>BindingTestCapture_OnKeyDown is to build the key parameter <strong>and</strong> callSetBinding(). Now, OnKeyDown captures every key individually, evenmodifier keys. Because you can’t bind modifier keys, you should ignore themby adding the following code to the end <strong>of</strong> the OnKeyDown function:local modifier = key:sub(2)if modifier == “SHIFT“ or

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!