(Code of the day) Key Up/KeyDown Triggered Events.

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

(Code of the day) Key Up/KeyDown Triggered Events.

Post by romero126 »

Are you tired of trying to figure out which key isnt currently being pushed down and keys not being pushed down? To allow for multiple button pressed events? Look no farther! Here is a simple process to add event driven key commands to your LUA code!

Taken from input.lua

Code: Select all

-- Coded by romero126
GlassEyeInput = {
	["KeyList"] = { }
}

table.insert(GlassEyeInput["KeyList"], {Controls.selectMask, "Select", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.startMask, "Start", nil})

table.insert(GlassEyeInput["KeyList"], {Controls.upMask, "Up", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.rightMask, "Right", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.downMask, "Down", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.leftMask, "Left", nil})

table.insert(GlassEyeInput["KeyList"], {Controls.ltriggerMask, "LTrigger", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.rtriggerMask, "RTrigger", nil})

table.insert(GlassEyeInput["KeyList"], {Controls.triangleMask, "Triangle", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.circleMask, "Circle", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.crossMask, "Cross", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.squareMask, "Square", nil})

table.insert(GlassEyeInput["KeyList"], {Controls.homeMask, "Home", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.holdMask, "Hold", nil})
table.insert(GlassEyeInput["KeyList"], {Controls.noteMask, "Note", nil})

function GlassEyeInput:CheckInput(key)
	result = nil
	for k, v in GlassEyeInput["KeyList"] do
		if (key:buttons() & v[1] > 0) then
			if (not v[3]) then
				v[3] = 1
				if (not result) then result = { } end
				result[v[2]] = 1
			end
		else
			if (v[3]) then
				if (not result) then result = { } end
				result[v[2]] = 0
				v[3] = nil
			end
		end
	end
	return result
end
This allows a user to get input from on an event basis.

See:

Code: Select all

-- Coded by romero126
if (GlassEyeInput) then
	local check = GlassEyeInput:CheckInput(key)
	if (check) then
		for k,v in check do
			if (k == "Start") and (v == 0) then
				print("You lifted your finger on the start key")
			end
			if (k == "Start") and (v == 1) then
				print("You pressed your finger on the start key")
			end
		end
	end
end
Please note glass eye is a current project of mine. With standard libraries for an event driven based system.

Please give props to the rightfull owner in your code.

Comments, questions Concerns? Let me know!
Last edited by romero126 on Thu Jan 05, 2006 7:36 am, edited 1 time in total.
modsyn
Posts: 28
Joined: Tue Sep 27, 2005 6:02 am

Post by modsyn »

nice! very helpful stuff. i hate writing controls code. this might make it simpler.
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Edited because of a bug I noticed.
Post Reply