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

[1.31]Simulating a keypress

Started by coolblockj, 13 March 2012 - 09:09 PM
coolblockj #1
Posted 13 March 2012 - 10:09 PM
Im trying to make a program for monitors, in which you can kind of connect a computer and monitor together.
My code so far is like this:
mon = peripheral.wrap("back")
while true do
input = read()
term.redirect(mon)
term.setCursorBlink(true)
shell.run(input)
term.restore()
end

But what im trying to do is get it so everytime i press a key, it shows it on both the monitor and the actual computer.
So i thought maybe i can wait for the key event, like so
mon = peripheral.wrap("back")
while true do
event, key == os.pullEvent()
if event == "key" then
term.redirect(mon)
presskey(key) – This isnt real, but thats what i am trying to accomplish
term.restore()
presskey(key)
end
end

And then press that key on both computers.
So im wondering if theres a way to simulate a key press or anything like it.
Thanks!
Advert #2
Posted 13 March 2012 - 10:13 PM
You can use os.queueEvent().


os.queueEvent("key", 123)
coolblockj #3
Posted 13 March 2012 - 11:14 PM
But how do i get past the problem of it not showing up on the computer, but it does on the monitor?
Advert #4
Posted 13 March 2012 - 11:21 PM
But how do i get past the problem of it not showing up on the computer, but it does on the monitor?

The monitor isn't an actual computer; just the display of your computer will be redirected to it.

You could try overwriting term.*, but it'd be complicated-ish.

Basically:


local term = term
local redir = {}
rawset(_G, "term", redir)

setmetatable(redir, {__index = function(s, k)
 local f
 if k == "redirect" then
  f =  term.redirect
 elseif k == "restore" then
  f = term.restore
 else
  f = function(...)
   local arg = {...}
   term.redirect(mon)
   term[k](unpack(arg))
   term.restore()
   return term[k](unpack(arg))
  end
 end
 s[k] = f
 return f
end})

Something like this; I haven't had time to test it.
mon would be the wrapped monitor.
Wolvan #5
Posted 25 March 2012 - 08:29 PM
On a program I made I did it like this

text = read()
term.redirect(peripheral.wrap("top"))
print(text)
term.restore()
print(text)