This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mmmmmmmm's profile picture

[Question] "isKeyDown"?

Started by Mmmmmmmm, 27 July 2012 - 09:16 PM
Mmmmmmmm #1
Posted 27 July 2012 - 11:16 PM
Would it be possible to do something like this?

local function isKeyDown(key)
  -- Check if key is being pressed
end
I don't mean something like os.pullEvent(), which would detect if the key is pressed once, but rather something that would detect if the key is being pressed.
brett122798 #2
Posted 28 July 2012 - 01:06 AM

local function isKeyDown(keynumber)
local e,key = os.pullEventRaw("key")
if key == keynumber then
<Execute Code>
end
end
Not tested, but I think it might work.
ComputerCraftFan11 #3
Posted 28 July 2012 - 01:10 AM
Try this, I didn't test it yet:

local function isKeyDown(keyinput)
local timer112 = os.startTimer(0)
event, key = os.pullEvent()
if event == timer and key == timer112 then
  return false
elseif event == "char" or event == "key" then
  if keyinput == key then
   return true
  else
   return false
  end
else
  return false
end
end
Xtansia #4
Posted 28 July 2012 - 01:23 AM

local function isKeyDown(keynumber)
local e,key = os.pullEventRaw("key")
if key == keynumber then
<Execute Code>
end
end
Not tested, but I think it might work.
Try this, I didn't test it yet:

local function isKeyDown(keyinput)
local timer112 = os.startTimer(0)
event, key = os.pullEvent()
if event == timer and key == timer112 then
  return false
elseif event == "char" or event == "key" then
  if keyinput == key then
   return true
  else
   return false
  end
else
  return false
end
end

Both of those don't do what the person wants.

@EternalFacepalm Currently no, CC is very event driven with no static checking like that.
Cloudy #5
Posted 28 July 2012 - 11:09 AM
As more than one person can use a computer at any one time this would not be trivial to add. However I'll try and think of a solution for the next version.