Difference between revisions of "love.keyboard.isDown"

m (Add more see alsos.)
(Examples)
(10 intermediate revisions by 6 users not shown)
Line 10: Line 10:
 
{{param|boolean|down|True if the key is down, false if not.}}
 
{{param|boolean|down|True if the key is down, false if not.}}
 
== Function ==
 
== Function ==
{{newin|[[0.7.2]]|type=variant}}
+
{{newin|[[0.7.2]]|072|type=variant}}
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
anyDown = love.keyboard.isDown( key1, key2, key3, ... )
+
anyDown = love.keyboard.isDown( key, ... )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|KeyConstant|keyN|A key to check.}}
+
{{param|KeyConstant|key|A key to check.}}
 +
{{param|KeyConstant|...|Additional keys to check.}}
 
=== 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.}}
Line 22: Line 23:
 
=== Increase a value while a key is held down ===
 
=== Increase a value while a key is held down ===
 
<source lang="lua">
 
<source lang="lua">
val = 0   -- establish a variable for later use
+
local val = 0;
 
function love.update(dt)
 
function love.update(dt)
if love.keyboard.isDown("up") then
+
    -- We will increase the variable by 1 for every second the key is held down.
val = val + dt   -- we will increase the variable by 1 for every second the key is held down
+
    if love.keyboard.isDown("up") then
end
+
        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
 
end
 
</source>
 
</source>
Line 32: Line 41:
 
* [[parent::love.keyboard]]
 
* [[parent::love.keyboard]]
 
* [[love.keypressed]]  
 
* [[love.keypressed]]  
* [[love.keyreleased]]  
+
* [[love.keyreleased]]
 +
* [[love.keyboard.isScancodeDown]]
 
[[Category:Functions]]
 
[[Category:Functions]]
 
{{#set:Description=Checks whether a certain key is down.}}
 
{{#set:Description=Checks whether a certain key is down.}}

Revision as of 23:51, 14 February 2019

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.

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