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

Pull Key Events And Run Shell Concurrently

Started by oeed, 23 August 2013 - 07:10 AM
oeed #1
Posted 23 August 2013 - 09:10 AM
Hi,

I'm currently writing the submission program for my app store (see signature for more info). I'm at a stage where I'd like to take screenshots of the computer screen. I know I can't directly access the 'pixels', but I'll be changing term.write to also write to a table. My problem is, however, trying to capture a key press to fire the screenshot, yet run the shell and another program at the same time. I need to be able to use the shell because some of the programs may be shell based and I'd rather not fake it using read() or a similar process. When ever I try something such as the following only 'Test fired!' is displayed. This may be due to the coroutine.yeild() that is being called. I may not be explaining myself amazingly well, but essentially I'm trying to capture key presses while the shell is running. If you can think of a better way of doing it let me know. Thanks :)/>


function pull()
  while true do
	event = os.pullEvent()
	print(event .. ' was fired!')
  end
end
function test()
  print('Test fired!')
end

parallel.waitForAny(pull, test)
theoriginalbit #2
Posted 23 August 2013 - 09:24 AM
Ok so judging off what you said, you're wanting to capture a screenshot when a certain key is pressed.

The way you're doing it is right in principle, here is a test that I've done…. Press F2 and you'll see that it works…


local function runShell()
  os.run({}, "rom/programs/shell")
end

--# this just shows that it was pressed, the real work is done in the function below
local function printTopLine(msg)
  local sx,sy = term.getCursorPos()
  term.setCursorPos(1, 1)
  term.clearLine()
  term.setTextColor(colors.yellow)
  write(msg)
  term.setTextColor(colors.white)
  term.setCursorPos(sx,sy)
end

local function listenForKey()
  local timeout

  while true do
	local e, p = os.pullEventRaw()
	if e == "key" and p == keys.f2 then
	  printTopLine("Screenshot key pressed...")
	  timeout = os.startTimer(2)
	elseif e == "timer" and p == timeout then
	  printTopLine(os.version())
	end
  end
end

term.clear()
term.setCursorPos(1, 1)
parallel.waitForAll(runShell, listenForKey)
oeed #3
Posted 23 August 2013 - 09:28 AM
Ok so judging off what you said, you're wanting to capture a screenshot when a certain key is pressed.

The way you're doing it is right, here is a test that I've done…. Press F2 and you'll see that it works…


local function runShell()
  os.run({}, "rom/programs/shell")
end

--# this just shows that it was pressed, the real work is done in the function below
local function printTopLine(msg)
  local sx,sy = term.getCursorPos()
  term.setCursorPos(1, 1)
  term.clearLine()
  term.setTextColor(colors.yellow)
  write(msg)
  term.setTextColor(colors.white)
  term.setCursorPos(sx,sy)
end

local function listenForKey()
  local timeout

  while true do
	local e, p = os.pullEventRaw()
	if e == "key" and p == keys.f2 then
	  printTopLine("Screenshot key pressed...")
	  timeout = os.startTimer(2)
	elseif e == "timer" and p == timeout then
	  printTopLine(os.version())
	end
  end
end

term.clear()
term.setCursorPos(1, 1)
parallel.waitForAll(runShell, listenForKey)

Ah, I see. Running shell as a program, didn't really think about that. I haven't really got time to test it now but it looks sound, thanks! :D/>
GopherAtl #4
Posted 23 August 2013 - 10:57 AM
the issue with the code you originally posted was that test printed and immediately exited, which would cause both coroutines to exit immediately with parallel.waitForAny.
NOTUSEDPLEASEDELETE #5
Posted 23 August 2013 - 07:36 PM
Which means parallel.waitForAll is needed.
oeed #6
Posted 23 August 2013 - 08:02 PM
The reason I used waitForAny is because I thought that it when the test function was run the shell would resume, leaving the key capture still running. theoriginalbit's method worked almost perfectly, I just had to pass the current instance of the shell so it wouldn't run startup.