key or mouse input with hold

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
mxmlnbndsmnn
Prole
Posts: 37
Joined: Mon Feb 11, 2019 4:17 pm

key or mouse input with hold

Post by mxmlnbndsmnn »

Hi, does anyone know a (preferably lightweight) lib for mouse/keyboard input that can do the following:
Assume my player can cast an ability, say throw a fireball. To do so, the user can press the 'q' key.
He can also hold down the 'q' key to charge the fireball before throwing it so that it gains more damage or size etc.
I am looking for a lib that can differentiate between 'q' is briefly pressed - fire immediately and 'q' is hold - do NOT fire immediately, wait for the user to release 'q' to throw a charged fireball.

Yes, I am aware that there are libraries that let me use "press" and "hold" events, but I have not found one that does NOT fire the "press" event when only the "hold" event should be triggered. While searching I inspected "boipushy" and "lynput" which have some cool features and are fairly simple, but sadly do not solve the described problem.

In case I did not made the problem clear enough, just let me know and I will upload a simple love file to showcase what I mean*.
And yes, I am aware that I could change the charging mechanism to not have the described problem, but I am hooked already - so either I find a lib or I will code it myself :)

* if you know the Witcher 3 - the same principle applies to Quen and alternate Quen sign for instance. Briefly press button = cast Quen (passive) but hold button = cast active Quen sign.
User avatar
Nikki
Citizen
Posts: 84
Joined: Wed Jan 25, 2017 5:42 pm

Re: key or mouse input with hold

Post by Nikki »

Code: Select all


function love.keypressed(key)
   if key == 'escape' then love.event.quit() end
end

function love.keyreleased(key)
   if key == 'q' then
      table.insert(projectiles, {400,300,fireballCharge,0})
      fireballCharge = 0
   end
end

function love.load()
   fireballCharge = 0
   projectiles = {}
end

function love.update(dt)
   if love.keyboard.isDown('q') then
      fireballCharge = fireballCharge + (dt*100)
   end

   for i = #projectiles, 1 , -1 do
      local p = projectiles[i]
      p[2] = p[2] - (dt*p[3]*10)
      p[4] = p[4] + dt
      if p[2] < -1000 then
         table.remove(projectiles, i)
      end
   end

end

function love.draw()
   love.graphics.setColor(1,1,1,1)

   love.graphics.print('press Q to charge, release Q to release')

   if fireballCharge > 0 then
      love.graphics.circle('fill', 400,300,fireballCharge)
   end

   for i =1 , #projectiles do
      local p = projectiles[i]
      love.graphics.setColor(1,1,1, 1.0 - p[4])
      love.graphics.circle('fill', p[1],p[2],p[3])
   end
   
   
end

tldr, i just used love.keyboard.isDown('q')
mxmlnbndsmnn
Prole
Posts: 37
Joined: Mon Feb 11, 2019 4:17 pm

Re: key or mouse input with hold

Post by mxmlnbndsmnn »

Well, one can call this working for this particular task, but to my mind it does not solve the proposed problem. Your code does not differentiate between an immediate action (short press) and the charge/hold functionality. But this is exactly what I am looking for. Nonetheless, this approach might be a good start for me. In the key released function I might check the duration of the hold and if it surpasses a threshold (say 0.1s) the charge is initiated, or if the key is released before the time threshold is reached, I will fire the immediate key press action ... I'll look into it.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: key or mouse input with hold

Post by pgimeno »

Yeah, basically start a timer on key press and check it on key release. Another World used this technique for the three different functions of the pistol: shooting, building a barrier, or power shooting.
User avatar
dusoft
Party member
Posts: 634
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: key or mouse input with hold

Post by dusoft »

pgimeno wrote: Tue Oct 26, 2021 1:06 pm Another World used this technique for the three different functions of the pistol: shooting, building a barrier, or power shooting.
Yass!!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests