Jump to content




Pull Key Events And Run Shell Concurrently


  • You cannot reply to this topic
5 replies to this topic

#1 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 23 August 2013 - 08: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)


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 August 2013 - 08: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)


#3 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 23 August 2013 - 08:28 AM

View Posttheoriginalbit, on 23 August 2013 - 08:24 AM, said:

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

#4 GopherAtl

  • Members
  • 888 posts

Posted 23 August 2013 - 09: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.

#5 NOTUSEDPLEASEDELETE

  • Members
  • 70 posts

Posted 23 August 2013 - 06:36 PM

Which means parallel.waitForAll is needed.

#6 oeed

    Oversimplifier

  • Members
  • 2,095 posts
  • LocationAuckland, New Zealand

Posted 23 August 2013 - 07: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users