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

Press any key except ESC to exit screensaver

Started by ctyking, 29 September 2012 - 12:09 PM
ctyking #1
Posted 29 September 2012 - 02:09 PM
Hello everyone!

I made a simple screensaver for the monitor in which an "o" appears randomly on the screen, then disappears and appears somewhere randomly again.

http://pastebin.com/0XpP3mv2


What I would like to implement is that if I press any key except ESC, then the screensaver stops and the console prompt comes back.

(I want it to exclude ESC, so I can run the screensaver, press ESC to exit the terminal view and watch it on the monitor, then if I want to resume to prompt, I right click the computer to bring up the terminal view and press some other key to exit it.)

Do you know how I could make this work?
Lettuce #2
Posted 29 September 2012 - 02:23 PM
You can use os.pullEvent("char") which will look for any character key press, which excludes anything that's not a letter. Such as Enter or ESC.
ctyking #3
Posted 29 September 2012 - 02:42 PM
I tried adding that char event, and now the following happens:

I can start the screensaver and press ESC to exit the terminal, but after that the "o" only appears once on the monitor, after that nothing appears, the monitor remains blank. If I enter the terminal, press a button, then press ESC, the "o" again appears once, then the monitor remains blank again. And pressing a character still doesn't exit the screensaver program.

http://pastebin.com/p1tqLfYa
PonyKuu #4
Posted 29 September 2012 - 02:47 PM
I think, you should use parallel api. I'm not sure about details, so maybe somepony who is more experienced can give you an advise… But standard turtle program "dance" uses this:


parallel.waitForAll(
	function()
		while not bEnd do
			local event, key = os.pullEvent("key")
			if key ~= keys.escape then
				bEnd = true
			end
		end	  
	end,
	function()
		while not bEnd do
			local fnMove = tMoves[math.random(1,#tMoves)]
			fnMove()
		end
	end
)
The second function is the dance function, and the first one is the key-handling function
Clockturn #5
Posted 29 September 2012 - 03:03 PM
For specifically anything other than escape:
local e,s = os.pullEvent("key")
if s ~= 1 then
  -- It's not escape
  return
end

To run this with the screensaver, you'd want to use the parallel.waitForAny function.
Essentially, you can supply that with any number of other functions, and it will run them all until one of them returns.
If you didn't use this, it would only continue running the screensaver when a key event is received.

What I came up with: http://pastebin.com/51e2UyTg
I hope you learn from it rather than just taking it as ready code :)/>/>
Note: In the parallel.waitForAny call, I used function() screensaver() end - you can just enter 'screensaver' instead, since that points to the function.
ctyking #6
Posted 29 September 2012 - 03:17 PM
Thanks, your program is very good, because it explains the parallel API well, which was rather obscure to me.

I've also added a quick fix to your code:

At the end, at the "– Something returned if we got here" part,
I added the command "mon.clear()" so if you turn off the screensaver while an "o" is currently displayed on the monitor, then it will erase that "o" from there.