Difference between revisions of "love.keyboard.isDown"

m (Added table variant available since 0.10.2)
 
(2 intermediate revisions by one other user not shown)
Line 9: Line 9:
 
=== Returns ===
 
=== Returns ===
 
{{param|boolean|down|True if the key is down, false if not.}}
 
{{param|boolean|down|True if the key is down, false if not.}}
== Examples ==
 
Move circle when the player press the left and right keys.
 
<source lang="lua">
 
function love.load()
 
  x, y = 20, 20
 
end
 
  
function love.update(dt)
 
  if love.keyboard.isDown("left") then
 
      x = x - 10 * dt
 
  end
 
  if love.keyboard.isDown("right") then
 
      x = x + 10 * dt
 
  end
 
end
 
 
function love.draw()
 
    love.graphics.circle("line", x, y, 10)
 
end
 
</source>
 
 
== Function ==
 
== Function ==
 
{{newin|[[0.7.2]]|072|type=variant}}
 
{{newin|[[0.7.2]]|072|type=variant}}
Line 40: Line 21:
 
=== Returns ===
 
=== Returns ===
 
{{param|boolean|anyDown|True if any supplied key is down, false if not.}}
 
{{param|boolean|anyDown|True if any supplied key is down, false if not.}}
 +
 +
== Function ==
 +
{{newin|[[0.10.2]]|102|type=variant}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
anyDown = love.keyboard.isDown({ key, ... })
 +
</source>
 +
=== Arguments ===
 +
{{param|table|keys|Table of keys to check.}}
 +
{{subparam|KeyConstant|key|A key to check.}}
 +
{{subparam|KeyConstant|...|Additional keys to check.}}
 +
=== Returns ===
 +
{{param|boolean|anyDown|True if any supplied key is down, false if not.}}
 +
 
== Examples ==
 
== Examples ==
 
=== Increase a value while a key is held down ===
 
=== Increase a value while a key is held down ===

Latest revision as of 13:10, 7 December 2022

Checks whether a certain key is down. Not to be confused with love.keypressed or love.keyreleased.

Function

Synopsis

down = love.keyboard.isDown( key )

Arguments

KeyConstant key
The key to check.

Returns

boolean down
True if the key is down, false if not.

Function

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

Synopsis

anyDown = love.keyboard.isDown( key, ... )

Arguments

KeyConstant key
A key to check.
KeyConstant ...
Additional keys to check.

Returns

boolean anyDown
True if any supplied key is down, false if not.

Function

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

Synopsis

anyDown = love.keyboard.isDown({ key, ... })

Arguments

table keys
Table of keys to check.
KeyConstant key
A key to check.
KeyConstant ...
Additional keys to check.

Returns

boolean anyDown
True if any supplied key is down, false if not.

Examples

Increase a value while a key is held down

local val = 0;
function love.update(dt)
    -- We will increase the variable by 1 for every second the key is held down.
    if love.keyboard.isDown("up") then
        val = val + dt
        print(val)
    end

    -- We will decrease the variable by 1/s if any of the wasd keys is pressed. 
    if love.keyboard.isDown('w', 'a', 's', 'd') then
        val = val - dt
        print(val)
    end
end

See Also


Other Languages