Difference between revisions of "love.keypressed"

m (Add more see alsos.)
(Add example usage of the unicode argument.)
Line 17: Line 17:
 
       love.event.push("q")  -- actually causes the app to quit
 
       love.event.push("q")  -- actually causes the app to quit
 
   end
 
   end
 +
end
 +
</source>
 +
 +
Record and print text the user writes.
 +
<source lang="lua">
 +
function love.load()
 +
    text = "Type away! -- "
 +
end
 +
 +
function love.keypressed(key, unicode)
 +
    -- ignore non-printable characters (see http://www.ascii-code.com/)
 +
    if unicode > 31 and unicode < 256 then
 +
        text = text .. string.char(unicode)
 +
    end
 +
end
 +
 +
function love.draw()
 +
    love.graphics.printf(text, 0,0,800)
 
end
 
end
 
</source>
 
</source>

Revision as of 22:20, 7 December 2011

Callback function triggered when a key is pressed.

Function

Synopsis

love.keypressed( key, unicode )

Arguments

KeyConstant key
Character of the key pressed.
number unicode
The unicode number of the key pressed.

Returns

Nothing.

Examples

Exit the game when the player presses the Escape key, using love.event.push.

function love.keypressed(key)   -- we do not need the unicode, so we can leave it out
   if key == "escape" then
      love.event.push("q")   -- actually causes the app to quit
   end
end

Record and print text the user writes.

function love.load()
    text = "Type away! -- "
end

function love.keypressed(key, unicode)
    -- ignore non-printable characters (see http://www.ascii-code.com/)
    if unicode > 31 and unicode < 256 then
        text = text .. string.char(unicode)
    end
end

function love.draw()
    love.graphics.printf(text, 0,0,800)
end

See Also


Other Languages