Skip to content

Instantly share code, notes, and snippets.

@luizbills
Last active September 19, 2025 14:25
Show Gist options
  • Save luizbills/68df8ba84b21625cd6a9fc6021b79eeb to your computer and use it in GitHub Desktop.
Save luizbills/68df8ba84b21625cd6a9fc6021b79eeb to your computer and use it in GitHub Desktop.
Simple Litecanvas plugin to handle buttons/actions. Useful to multiple controls.
/**
* @version 1.1.0
*/
function pluginButtons(engine) {
let _btns
const buttons = (data) => {
_btns = data || {}
for (const [key, values] of Object.entries(_btns)) {
_btns[key] = values.map((v) => v.toLowerCase())
}
}
const isbtndown = (id) => {
if (_btns[id]) {
for (let key of _btns[id]) {
if (engine.iskeydown(key)) {
return true
}
}
}
return false
}
const isbtnpressed = (id) => {
if (_btns[id]) {
for (let key of _btns[id]) {
if (engine.iskeypressed(key)) {
return true
}
}
}
return false
}
const lastbtn = () => {
const key = engine.lastkey()
for (const [btn, keys] of Object.entries(_btns)) {
if (keys.includes(key)) {
return btn
}
}
return null
}
return {
buttons,
isbtndown,
isbtnpressed,
lastbtn
}
}
@luizbills
Copy link
Author

luizbills commented Sep 17, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment