This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FuuuAInfiniteLoop(F.A.I.L)'s profile picture

Detect if a key is being holded

Started by FuuuAInfiniteLoop(F.A.I.L), 19 May 2013 - 01:27 PM
FuuuAInfiniteLoop(F.A.I.L) #1
Posted 19 May 2013 - 03:27 PM

function shold()
  sleep(0.1)
  timer = os.startTimer(0.1)
  while true do
   evt, _timer = os.pullEvent("timer")
   if _timer==timer then
	event = "sleep"
	return
   end
end
end
function _hold()
  os.pullEvent("key")
  os.pullEvent("key")
  event = "key"
end
function hold()
  pararell.waitForAny(shold, _hold)
  if event=="sleep" then return false else return true end
end
Explanation:
function shold:
It sleeps 0.1 seconds to give _hold opportunity to start first
then it start a timer and waits till it finish
function _hold:
it detects two key presses and finish
function hold:
run imunstaniosly the two functions and return

This can be used to say how many seconds has been holded a key:



function shold()
  sleep(0.1)
  timer = os.startTimer(0.1)
  while true do
   evt, _timer = os.pullEvent("timer")
   if _timer==timer then
	event = "sleep"
	return
   end
end
end
function _hold()
  os.pullEvent("key")
  os.pullEvent("key")
  event = "key"
end
function hold()
  pararell.waitForAny(shold, _hold)
  if event=="sleep" then return false else return true end
end
local start = os.clock()
while hold() do
  --nothing
end
local end = os.clock()
print("You holded the key for "..start-end.." seconds!")
event = nil
jesusthekiller #2
Posted 19 May 2013 - 04:54 PM
It's not tutorial, It's code snippet…

At least put those comments in code
theoriginalbit #3
Posted 20 May 2013 - 12:16 AM
It's not tutorial, It's code snippet…
That doesn't even work…..

Variable names are not allowed to be Lua keywords, you spelt parallel wrong ("pararell") and many other problems I can see with the code before even attempting to run it…
Lyqyd #4
Posted 20 May 2013 - 12:58 AM
Please fix this and provide a write-up to go along with it. This isn't much of a tutorial at this point.
diegodan1893 #5
Posted 20 May 2013 - 02:43 PM
This line:

if event=="sleep" then return false else return true end
can be:

return event~="sleep"

Edit: The code I posted didn't work so I changed it, you can see why in the post below.
Kingdaro #6
Posted 20 May 2013 - 04:54 PM
This line:

if event=="sleep" then return false else return true end
can be:

return not event=="sleep"
That wouldn't work, it'd always evaluate to false. "not event" is false, and false is not equal to "sleep".


return event ~= "sleep"

I've actually done that mistake many times in the past and had my ass kicked by LÖVE because of it. :lol:/>
Shnupbups #7
Posted 20 May 2013 - 05:22 PM
Also, held*
diegodan1893 #8
Posted 21 May 2013 - 11:12 AM
That wouldn't work, it'd always evaluate to false. "not event" is false, and false is not equal to "sleep".


return event ~= "sleep"

I've actually done that mistake many times in the past and had my ass kicked by LÖVE because of it. :lol:/>

Yes, I don't know what I was thinking when I wrote that xd.
theoriginalbit #9
Posted 21 May 2013 - 12:48 PM
Yes, I don't know what I was thinking when I wrote that xd.
Don't kick yourself too hard about it, you may have been thinking of this

return not (event == "sleep")

which would work fine. Although completely pointless given you have the ~=
rhyleymaster #10
Posted 11 July 2013 - 07:32 PM
Good snippet. I will remember how to detect if my keys are being holded, because 'held' is too mainstream.