Difference between revisions of "love.keypressed"

m (Examples)
 
(17 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
Callback function triggered when a key is pressed.
 
Callback function triggered when a key is pressed.
 +
 
== Function ==
 
== Function ==
{{newin|[[0.9.0]]|090|type=variant}}
+
{{newin|[[0.10.0]]|100|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.keypressed( key, scancode, isrepeat )
 +
</source>
 +
=== Arguments ===
 +
{{param|KeyConstant|key|Character of the pressed key.}}
 +
{{param|Scancode|scancode|The scancode representing the pressed key.}}
 +
{{param|boolean|isrepeat|Whether this keypress event is a repeat. The delay between key repeats depends on the user's system settings.}}
 +
=== Returns ===
 +
Nothing.
 +
=== Notes ===
 +
[[Scancode]]s are keyboard layout-independent, so the scancode "w" will be generated if the key in the same place as the "w" key on an [https://en.wikipedia.org/wiki/British_and_American_keyboards#/media/File:KB_United_States-NoAltGr.svg American keyboard] is pressed, no matter what the key is labelled or what the user's operating system settings are.
 +
 
 +
Key repeat needs to be enabled with [[love.keyboard.setKeyRepeat]] for repeat keypress events to be received. This does not affect [[love.textinput]].
 +
 
 +
== Examples ==
 +
Exit the game when the player presses the Escape key, using [[love.event.quit]].
 +
<source lang="lua">
 +
function love.keypressed(key, scancode, isrepeat)
 +
  if key == "escape" then
 +
      love.event.quit()
 +
  end
 +
end
 +
</source>
 +
 
 +
== Function ==
 +
{{newinoldin|[[0.9.0]]|090|[[0.10.0]]|100|type=variant}}
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 13: Line 41:
 
=== Notes ===
 
=== Notes ===
 
Key repeat needs to be enabled with [[love.keyboard.setKeyRepeat]] for repeat keypress events to be received.
 
Key repeat needs to be enabled with [[love.keyboard.setKeyRepeat]] for repeat keypress events to be received.
 +
 
== Function ==
 
== Function ==
 
{{oldin|[[0.9.0]]|090|type=variant|text=Unicode text input is now handled separately via [[love.textinput]]}}
 
{{oldin|[[0.9.0]]|090|type=variant|text=Unicode text input is now handled separately via [[love.textinput]]}}
Line 25: Line 54:
 
Nothing.
 
Nothing.
 
== Examples ==
 
== Examples ==
Exit the game when the player presses the Escape key, using [[love.event.quit]].  
+
 
 +
A movement key input system, where for one step need to press the key once.
 
<source lang="lua">
 
<source lang="lua">
function love.keypressed(key)
+
function love.keypressed( key, scancode, isrepeat )
   if key == "escape" then
+
  local dx, dy = 0, 0
       love.event.quit()
+
   if scancode == "d" then -- move right
 +
      dx = 1
 +
  elseif scancode == "a" then -- move left
 +
       dx = -1
 +
  elseif scancode == "s" then -- move down
 +
      dy = 1
 +
  elseif scancode == "w" then -- move up
 +
      dy = -1
 
   end
 
   end
 +
  move (dx, dy) -- need to be some function to move the active object
 
end
 
end
 
</source>
 
</source>
----
 
  
{{oldin|[[0.9.0]]|090|type=example|text=Text input is now handled separately via love.textinput.}}
+
{{oldin|[[0.9.0]]|090|type=example|text=Text input is now handled separately via [[love.textinput]]}}
 
Record and print text the user writes (0.8.0 and below.)
 
Record and print text the user writes (0.8.0 and below.)
 
<source lang="lua">
 
<source lang="lua">
Line 58: Line 95:
 
* [[love.keyreleased]]  
 
* [[love.keyreleased]]  
 
* [[love.keyboard.isDown]]
 
* [[love.keyboard.isDown]]
 +
* [[love.keyboard.isScancodeDown]]
 
* [[love.textinput]]
 
* [[love.textinput]]
 
[[Category:Callbacks]]
 
[[Category:Callbacks]]
 
{{#set:Description=Callback function triggered when a key is pressed.}}
 
{{#set:Description=Callback function triggered when a key is pressed.}}
{{#set:Subcategory=General}}
+
{{#set:Subcategory=Keyboard}}
 
{{#set:Since=000}}
 
{{#set:Since=000}}
 +
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.keypressed}}
 
{{i18n|love.keypressed}}

Latest revision as of 15:32, 22 September 2024

Callback function triggered when a key is pressed.

Function

Available since LÖVE 0.10.0
This variant is not supported in earlier versions.

Synopsis

love.keypressed( key, scancode, isrepeat )

Arguments

KeyConstant key
Character of the pressed key.
Scancode scancode
The scancode representing the pressed key.
boolean isrepeat
Whether this keypress event is a repeat. The delay between key repeats depends on the user's system settings.

Returns

Nothing.

Notes

Scancodes are keyboard layout-independent, so the scancode "w" will be generated if the key in the same place as the "w" key on an American keyboard is pressed, no matter what the key is labelled or what the user's operating system settings are.

Key repeat needs to be enabled with love.keyboard.setKeyRepeat for repeat keypress events to be received. This does not affect love.textinput.

Examples

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

function love.keypressed(key, scancode, isrepeat)
   if key == "escape" then
      love.event.quit()
   end
end

Function

Available since LÖVE 0.9.0 and removed in LÖVE 0.10.0
This variant is not supported in earlier or later versions.

Synopsis

love.keypressed( key, isrepeat )

Arguments

KeyConstant key
Character of the key pressed.
boolean isrepeat
Whether this keypress event is a repeat. The delay between key repeats depends on the user's system settings.

Returns

Nothing.

Notes

Key repeat needs to be enabled with love.keyboard.setKeyRepeat for repeat keypress events to be received.

Function

Removed in LÖVE 0.9.0
Unicode text input is now handled separately via love.textinput.

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

A movement key input system, where for one step need to press the key once.

function love.keypressed( key, scancode, isrepeat )
   local dx, dy = 0, 0
   if scancode == "d" then -- move right
      dx = 1
   elseif scancode == "a" then -- move left
      dx = -1
   elseif scancode == "s" then -- move down
      dy = 1
   elseif scancode == "w" then -- move up
      dy = -1
   end
   move (dx, dy) -- need to be some function to move the active object
end
Removed in LÖVE 0.9.0
Text input is now handled separately via love.textinput.

Record and print text the user writes (0.8.0 and below.)

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 < 127 then
        text = text .. string.char(unicode)
    end
end

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

See Also



Other Languages