Here is the code, if it helps anyone. Still have one problem tough. When the user touches a key and drags his finger away from the keyboard no key detects any event (not even a cancelled event) so the last key touched stays highlighted.
Is there an elegant way to reset all the buttons in this situation?
P.S
I’m intending to expand this into a full class of different style keyboards and text fields and will publish it in the community code area when its done.
cheers
Amir
[code]
–function to construct keboard buttons
local function newBtn(btnWidth,btnHeight,txt)
local btn = display.newGroup();
local bg = display.newRect(0,0,btnWidth,btnHeight);
bg:setStrokeColor(100,100,100);
bg.strokeWidth = 1;
local label = display.newText(txt,0,0,native.systemFontBold, btnHeight*.4 * 2);
label:scale(.5,.5) --retina display trick to make sure text is sharp
bg.x, bg.y = bg.contentWidth*.5, bg.contentHeight*.5
label.x, label.y = bg.contentWidth*.5, bg.contentHeight*.5
label:setTextColor(255,255,255)
btn:insert(bg);
btn:insert(label);
–public functions
function btn:reset()
local gradient = graphics.newGradient( { 20, 20, 60 }, { 10,10,30 }, “down” );
bg:setFillColor(gradient);
end
function btn:over()
bg:setFillColor(0,0,255);
end
function btn.getTag()
return txt;
end
return btn;
end
–function to assemble the keyboard
local function newKeyboard (keyboardWidth,keyboardHeight)
local newKeyboard = display.newGroup();
local numOfHorizontalBtns = 3;
local numOfVerticalBtns = 4;
local btnWidth = keyboardWidth / numOfHorizontalBtns;
local btnHeight = keyboardHeight / numOfVerticalBtns;
–create keypad keys
local keys = {};
for i=1,12 do
keys[i] ={};
end
–setup num 1-9 keys
for i=1,9 do
keys[i].tag = i;
end
–setup “.” , “0” and “Del” keys
keys[10].tag = “.”;
keys[11].tag = “0”;
keys[12].tag = “del”;
–private functions
local function resetAllKeys()
–removes the over state highlights from all keys
for i=1,#keys do
keys[i].btn:reset();
end
end
local function keyTouched(event)
–responds to key touches
local t = event.target
if event.phase == “began” then
–highlight the touched key
t:over();
elseif event.phase == “moved” then
–remove all key over highlight
resetAllKeys();
t:over();
elseif event.phase == “ended” or event.phase == “cancelled” then
resetAllKeys();
–do something with the last touched key
print(t.getTag()…" was pressed")
end
end
–initialize keypad
for i=1,#keys do
keys[i].btn = newBtn(btnWidth,btnHeight,keys[i].tag);
newKeyboard:insert(keys[i].btn);
keys[i].btn:setReferencePoint(display.TopLeftReferencePoint);
–position keys on the keyboard
local row = math.floor((i-1) / numOfHorizontalBtns)
local column = (i-1) % numOfHorizontalBtns;
keys[i].btn.x = column * btnWidth
keys[i].btn.y = row * btnHeight
–add an event touch listener to each key
keys[i].btn:addEventListener(“touch”,keyTouched)
end
resetAllKeys();
return newKeyboard;
end
–Main program
local function main()
local myKeypad = newKeyboard(display.contentWidth , display.contentHeight*.6);
myKeypad.x = 0
myKeypad.y = display.contentHeight - myKeypad.contentHeight
end
main();
[/code] [import]uid: 33608 topic_id: 16106 reply_id: 60072[/import]