Page 1 of 5

No Comment

Posted: Thu Nov 25, 2010 2:26 pm
by com_1
Scancodes Picker.

Code: Select all

--//______________________________________________________________________________________________
function love.load()

--// screen options
scrx = 640; scry = 480;
love.graphics.setMode(scrx, scry, false, true, 0); 
love.graphics.setCaption("Scancodes Picker"); love.graphics.setBackgroundColor(10, 30, 50);

--// font options
love.graphics.setFont(14);

--// global
ke_text="a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|" ..
		"0|1|2|3|4|5|6|7|8|9|" ..
		"(space)|!|#|$|&|'|(|)|*|+|,|-|.|/|:|;|<|=|>|?|@|[|\|]|^|_|`|" ..
		"kp0|kp1|kp2|kp3|kp4|kp5|kp6|kp7|kp8|kp9|kp.|kp/|kp*|kp-|kp+|kpenter|kp=|" ..
		"up|down|right|left|home|end|pageup|pagedown|" ..
		"insert|backspace|tab|clear|return|delete|" ..
		"f1|f2|f3|f4|f5|f6|f7|f8|f9|f10|f11|f12|f13|f14|f15|" ..
		"numlock|capslock|scrollock|rshift|lshift|rctrl|lctrl|ralt|lalt|rmeta|lmeta|lsuper|rsuper|mode|compose|" ..
		"pause|escape|help|print|sysreq|break|menu|power|euro|undo|";

ke_len = string.len(ke_text); 
ke_max = 0; ke_n = {}; ke_mid = 0; ke_i = 0; ke_a = ""; ke_m = 0;

for d = 0, ke_len - 1 do--// for >
	--// mid
	ke_mid = string.sub(ke_text, 1 + d, 1 + d);
	--// object
	if ke_mid == "|" then ke_max = ke_max + 1;end;
end;--// for <

for d = 0, ke_max - 1 do--// for >
	--// options
	ke_n[d] = 0;
end;--// for <

for d = 0, ke_len - 1 do--// for >
	--// mid
	ke_mid = string.sub(ke_text, 1 + d, 1 + d);
	--// text
	ke_a = ke_a .. ke_mid; ke_n[ke_i] = ke_a;
	--// text
	if ke_mid == "|" then ke_a = ""; ke_i = ke_i + 1;end;
end;--// for <

for d = 0, ke_max - 1 do--// for >
	if ke_n[d] ~= 0 then
		--// len
		ke_len = string.len(ke_n[d]);
		--// mid
		ke_mid = string.sub(ke_n[d], 1, 1 + (ke_len - 2));
		--// new text
		ke_n[d] = ke_mid;
	end;
end;--// for <

end

--//______________________________________________________________________________________________
function love.draw()

--// text
love.graphics.print("fps: " .. love.timer.getFPS(), 10, 20, 0, 1, 1);
love.graphics.print("Scancodes Picker",10, (scry - 10), 0, 1, 1);

--// options
ke_m = 0;

for d = 0, ke_max - 1 do--// for >
	--// text
	if love.keyboard.isDown(ke_n[d]) then
		ke_m = ke_m + 1;
		love.graphics.print(ke_m .. "  " .. ke_n[d], 10, 40 + (ke_m * 20), 0, 1, 1);
	end;
end;--// for <

end

Re: No Comment

Posted: Thu Nov 25, 2010 10:21 pm
by Mud
No need to do all that parsing yourself. Lua can do it for you (really fast):

Code: Select all

local scrx, scry = 640, 480
local keys = {
   'a','b','c','d','e','f','g','h','i','j','k','l','m','keyn','o','p','q','r','s','t','u','v','w','x','y','z',
   '0','1','2','3','4','5','6','7','8','9','(space)','!','#','$','&','\'','(',')','*','+',',','-','.','/',
   ':','','<','=','>','?','@','[','\\',']','^','_','`','kp0','kp1','kp2','kp3','kp4','kp5','kp6','kp7',
   'kp8','kp9','kp.','kp/','kp*','kp-','kp+','kpenter','kp=','up','down','right','left','home','end',
   'pageup','pagedown','insert','backspace','tab','clear','return','delete','f1','f2','f3','f4','f5','f6',
   'f7','f8','f9','f10','f11','f12','f13','f14','f15','numlock','capslock','scrollock','rshift','lshift',
   'rctrl','lctrl','ralt','lalt','rmeta','lmeta','lsuper','rsuper','mode','compose','pause','escape',
   'help','print','sysreq','break','menu','power','euro','undo'
}

function love.load()
   love.graphics.setMode(scrx, scry, false, true, 0) 
   love.graphics.setCaption("Scancodes Picker")
   love.graphics.setBackgroundColor(10, 30, 50)
   love.graphics.setFont(14)
end

function love.draw()
   love.graphics.print("fps: " .. love.timer.getFPS(), 10, 20, 0, 1, 1)
   love.graphics.print("Scancodes Picker",10, (scry - 30), 0, 1, 1)
   local keyn = 0
   for i,key in ipairs(keys) do
      if love.keyboard.isDown(key) then
         keyn = keyn + 1
         love.graphics.print(keyn .. "  " .. key, 10, 40 + (keyn * 20), 0, 1, 1)
      end
   end
end
Or if all the quotes and commas bother you, you could use the built-in pattern matching library to do the parsing:

Code: Select all

local keynames = [[
   a b c d e f g h i j k l m keyn o p q r s t u v w x y z ' , 0 1 2 3 4 5 6 7 8
   9 (space) ! # $ & \ ( ) * +   - . / :  < = > ? @ [ \\ ] ^ _ ` kp0 kp1 kp2
   kp3 kp4 kp5 kp6 kp7 kp8 kp9 kp. kp/ kp* kp- kp+ kpenter kp= up down right
   left home end pageup pagedown insert backspace tab clear return delete f1 f2
   f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 numlock capslock scrollock
   rshift lshift rctrl lctrl ralt lalt rmeta lmeta lsuper rsuper mode compose
   pause escape help print sysreq break menu power euro undo
]]
local keys = {}
keynames:gsub('%S+', function(key) keys[#keys+1] = key end)
Though there may be a few keys in there that don't work, like space and " (which would be shift+'). I used my editor to refactor it.

Re: No Comment

Posted: Thu Nov 25, 2010 10:28 pm
by Robin
Yeah. But your table is not completely correct. In addition, it is probably simpler to do this:

Code: Select all

local scrx, scry = 640, 480
function love.load()
   love.graphics.setMode(scrx, scry, false, true, 0) -- this is horrible, btw. conf.lua exists for a reason! 
   love.graphics.setCaption("Scancodes Picker")
   love.graphics.setBackgroundColor(10, 30, 50)
   love.graphics.setFont(14)
   key = ''
   u = 0
end

function love.keypressed(key, u)
    key = key
    u = u
end

function love.keyreleased(key, u)
   key = ''
   u = 0
end

function love.draw()
   love.graphics.print("fps: " .. love.timer.getFPS(), 10, 20, 0, 1, 1)
   love.graphics.print("Scancodes Picker",10, (scry - 30), 0, 1, 1)
   love.graphics.print(u .. "  " .. key, 10, 40, 0, 1, 1)
end
It doesn't work exactly the same, but it's probably much more efficient.

Re: No Comment

Posted: Thu Nov 25, 2010 10:39 pm
by thelinx
That wont work, Robin.

Code: Select all

a = "foo"
function f(a)
  a = a
end
f("bar")
print(a) --> foo
You'll need to use different names for the globals and the function arguments:

Code: Select all

a = "foo"
function f(in)
  a = in
end
f("bar")
print(a) --> bar

Re: No Comment

Posted: Fri Nov 26, 2010 12:13 am
by Mud
Robin wrote:But your table is not completely correct.
Well, it's not really my table, I just used a regular expression to convert com_1's string. :) Mostly I just wanted to show him how he could save some work by using Lua more idiomatically.
it is probably simpler to do this:

Code: Select all

love.graphics.print(u .. "  " .. key, 10, 40, 0, 1, 1)
That would show the most recently pressed key, whereas his code showed all currently pressed keys, but your approach is definitely better. We could do this:

Code: Select all

local pressed = {}

function love.keypressed(key)
   pressed[key] = true
end

function love.keyreleased(key)
   pressed[key] = nil
end

function love.draw()
   local keyn = 0
   for key in pairs(pressed) do
      keyn = keyn + 1
      love.graphics.print(keyn .. "  " .. key, 10, 40 + (keyn * 20), 0, 1, 1)
   end
end

Re: No Comment

Posted: Fri Nov 26, 2010 9:40 pm
by Robin
You're right.
thelinx wrote:

Code: Select all

a = "foo"
function f(in)
  a = in
end
f("bar")
print(a) --> bar
That won't work either. "in" is a reserved word in Lua.

Re: No Comment

Posted: Fri Nov 26, 2010 9:45 pm
by thelinx
Well played. :brows:

Re: No Comment

Posted: Sat Nov 27, 2010 5:39 am
by zac352
Com_1, you have an amazing ability to write insanely hard to read code. You don't need to obfuscate, it's already obfuscated enough. :death:

But anyway, seeing as we're on the topic of keyboard stuff, CSV file parser! (Yes, I wrote another one of those scripts just for a post)

Code: Select all

csv=io.open("keys.csv")
local t={}
for l in io.lines(csv) do
table.insert(t,l)
end
function split(t,s)
local m={}
local q=false
local p1,p2=0
for i=1,#t do
if t:sub(i,i)==s and not q then
p2=i-1
table.insert(m,t:sub(p1,p2))
p1,p2=p2
elseif t:sub(i,i)=="\"" then
q=not q
end
end
table.insert(m,t:sub(p1,-1))
return m
end

local db={}
for i=1,#t do db[i]={} end
for i=1,#t do
db[i]=unpack(split(t[i],","))
end
Note that this may error, and that this isn't perfectly RFC compliant. :P

Re: No Comment

Posted: Sat Nov 27, 2010 5:41 am
by Jasoco
What's with the semicolons? Do they even do anything in Löve? This isn't JavaScript. They just make it look ugly.

Re: No Comment

Posted: Sat Nov 27, 2010 5:42 am
by zac352
Jasoco wrote:What's with the semicolons? Do they even do anything in Löve? This isn't JavaScript. They just make it look ugly.
Do you have something against semicolons? Don't make me show you their amazing use in obfuscation.