How to detect key input

Hello all! I have a question pertaining to keyboard input. Right now, I’m coding a simple platforming demo, just to get my feet wet with physics and basic game design. I’m having some trouble getting key input to work, so could I please have some assistance? Here’s the code I have so far. It’s based a bit on the tutorial code, except I’m using solid shapes instead of sprites.

Thanks for your help!

local blocky = display.newRect(display.contentCenterX, display.contentCenterY, 64, 64) blocky:setFillColor(1, 0, 0) local platform = display.newRect(display.contentCenterX, display.contentHeight-25, display.contentWidth-15, 32) blocky:setFillColor(0, 0, 1) local physics = require("physics") physics.start() physics.addBody(blocky, "dynamic") physics.addBody(platform, "static") local function playerMovement() if(event.keyName == "w") then blocky:applyLinearImpulse(0, -0.75, blocky.x, blocky.y) elseif(event.keyName == "a") then blocky:applyForce(-0.75, 0, blocky.x, blocky.y) elseif(event.keyName == "d") then blocky:applyForce(0.75, 0, blocky.x, blocky.y) end end blocky:addEventListener("key", playerMovement)

A useful piece of code to do this was put out by Ponywolf. It includes keyboard monitoring in a way thats easy to see and use.

https://github.com/ponywolf/ponyblitz

Thank you Graham, I’ll check it out today!

A useful piece of code to do this was put out by Ponywolf. It includes keyboard monitoring in a way thats easy to see and use.

https://github.com/ponywolf/ponyblitz

Thank you Graham, I’ll check it out today!