## [LIBRARY] tt.lua - Text triggers for lua!

veethree
Inner party member
Posts: 845
Joined: Sat Dec 10, 2011 7:18 pm

### [LIBRARY] tt.lua - Text triggers for lua!

I'm sure there is a more proper name for what this does, I just don't know what it is, Or how to look it up. So i'm just calling it text triggers. This library helps you call a certain function when a certain sequence of keys is pressed. Just like old video game cheat codes.

Proper documentation on github, Bad documentation below.

Code: Select all

tt = require "tt"
Update buffer

Code: Select all

function love.keypressed(key)
tt:updateBuffer(key)
end
tt:updateBuffer() can also go in textinput or mousepressed. Or wheverever you want, Just pass it something that makes sense.

Update internal timer

Code: Select all

function love.update(dt)
tt:update(dt)
end
This is optional, But required if you want to take advantage of the 'buffer timeout' and 'wait for timeout' features.

Define some triggers

Code: Select all

tt:new("escape", love.event.push, "quit")
tt:new("hello", print, "Hello world!")
tt:new("upupdowndownleftrightleftrightba", something_neat)
Now when you type "hello" or do the konami code, Something might happen if you've set it up right.

Demo: It's just a single file so i'll just drop it here

Code: Select all

function love.load()
tt = require "tt"
tt:setWaitForTimeout(true)
tt:setBufferTimeout(0.5)

-- Defining some triggers
tt:new("escape", love.event.push, "quit")
tt:new("hello", print, "Hello world!")
tt:new("mouse1mouse1", print, "Double click")
end

function love.update(dt)
-- Updating the internal timer
tt:update(dt)
end

function love.draw()
-- Printing out the buffer
love.graphics.print("Current buffer: "..tt:getBuffer(), 12, 12)
end

function love.keypressed(key)
-- Updating the buffer
tt:updateBuffer(key)
end

function love.textinput(t)
-- In löve, You can also use 'textinput' to update the buffer.
-- That way you can have triggers with capital letters, but a trigger like "escape"
-- wont be triggered when the escape key is pressed. Only when you type it out.
-- tt:updateBuffer(t)
end

function love.mousepressed(x, y, b)
-- You could also update the buffer in mousepressed, For example to detect a double click
-- Here i send the buffer a string like "mouse1" instead of just passing it "b" directly
-- This is so the "double click" trigger doesnt also trigger if you just type "11"
local char = "mouse"..b
tt:updateBuffer(char)
end
Attachments
tt.lua
v1.1
Last edited by veethree on Tue Oct 12, 2021 10:50 pm, edited 1 time in total.
Gunroar:Cannon()
Party member
Posts: 563
Joined: Thu Dec 10, 2020 1:57 am

### Re: [LIBRARY] tt.lua - Text triggers for lua!

I like it? Could the key arguments be in a table or something instead of one long string?
Why can't pirates code in lua? Because they spend to much time at C!

-It might look like nonsense but there's so much proof.
-Some more if you pls... .
It has to be real then, all that talk about the end .
-How to be saved
Gunroar:Cannon()
Party member
Posts: 563
Joined: Thu Dec 10, 2020 1:57 am

### Re: [LIBRARY] tt.lua - Text triggers for lua!

I like it? Could the key arguments be in a table or something instead of one long string?
Why can't pirates code in lua? Because they spend to much time at C!

-It might look like nonsense but there's so much proof.
-Some more if you pls... .
It has to be real then, all that talk about the end .
-How to be saved
veethree
Inner party member
Posts: 845
Joined: Sat Dec 10, 2011 7:18 pm

### Re: [LIBRARY] tt.lua - Text triggers for lua!

Gunroar:Cannon() wrote: Tue Oct 12, 2021 7:53 pm I like it? Could the key arguments be in a table or something instead of one long string?
Sure. I've updated it. Now the tt:new() function can also take a table of strings

Code: Select all

tt:new({"w", "w"}, print, "double w")
Also added an input filter function, So you can easily set it up to ignore input under certain conditions

Code: Select all

tt:setInputFilter(function() if someCondition then return false end)
Also renamed tt:update() to tt:updateTimer.
WAWAWA
Prole
Posts: 19
Joined: Fri Aug 13, 2021 5:10 pm

### Re: [LIBRARY] tt.lua - Text triggers for lua!

This is great, keep up the good work fellow lua user.
Nikki
Prole
Posts: 47
Joined: Wed Jan 25, 2017 5:42 pm

### Re: [LIBRARY] tt.lua - Text triggers for lua!

I'm sure there is a more proper name for what this does,
My first thought would be 'konami code'

### Who is online

Users browsing this forum: No registered users and 32 guests