Learning Lua

I’m looking to learn a bit and have hit a roadblock. I want to create a frame that listens to my S key and prints “You’re backpedaling, it better be an emergency!” But I don’t want to rebind keys just yet. I have the frame created, textured, displayed, and with a script set to “type1” which is the left click. It works when it’s left clicked, but there doesn’t seem to be a way to click that SecureActionButtonTemplate frame without doing hacky stuff with keybinds, which definitely feels like I’m approaching this wrong.

Without binding then “listening” for a particular key press means constantly checking to see if it’s pressed or not.

This code should allow for “quick” clicks of S (stepping back to get a target in front of you) but trigger the message if the key is pressed for longer (the timing can be adjusted).

local f = CreateFrame("Frame", "ImmortalmageFrame", UIParent)
f:SetSize(30, 30)
f:SetPoint("LEFT", 20, 0)
local UpdateSpeed = 0.5
f.Elapsed = UpdateSpeed
f.Pressed = false
f:SetScript("OnUpdate", function(self, elapsed)
	if not IsKeyDown("S") then
		self.Pressed = false
		self.Elapsed = UpdateSpeed
		return
	end
	self.Elapsed = self.Elapsed - elapsed
	if self.Pressed or self.Elapsed > 0 then return end
	self.Pressed = true
	print("|cffff0000You're backpedaling, it better be an emergency!|r")
end)

1 Like

Fizzlemizz’s solution is perfect if you wan to continually update.

But for a simple “do something once when S key goes down”:

local f = CreateFrame("Button")
f:SetScript("OnKeyDown",function(self,key)

  if key=="S" then
    print("You're backpedalling.")
  end

  self:SetPropagateKeyboardInput(true)
end)

The SetPropagateKeyboardInput(true) will pass any keystrokes down through without eating them like a binding would. (If you ever use false to eat keystrokes, be careful to make the logic solid or you risk preventing the user from using the keyboard if your event handler has an error or behaves unexpectedly.)

1 Like