Page 1 of 1

Keyboard

Posted: Sat May 23, 2020 8:03 pm
by Blunzzy
Hello, I was wondering what was the command I need to use if I want an action when I press a key on the keyboard but when I release it, the action stops. Anyone know the answer?

Re: Keyboard

Posted: Sat May 23, 2020 10:21 pm
by randomnovice
Hi,

By no means an expert but I think you could...

a) Test in love.update() using love.keyboard.isDown("key").

b) Activate a flag in love.keypressed(key) and deactivated it in love.keyreleased(key).

Re: Keyboard

Posted: Sun May 24, 2020 2:02 am
by sphyrth
Let's translate that into code:

Method 1:

Code: Select all

function love.update()
...
   if love.keyboard.isDown('a') then -- Let's press 'A' for example
     -- Do Action
   end
end
* You can put an "else <Stop Action>" if you really need it.

Method 2:

Code: Select all

function love.keypressed(key)
  if key == 'a' then
    -- Do Action
  end
end

function love.keyreleased(key)
  if key == 'a' then
    -- Stop Action
  end
end