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

How to virtually type a key using Lua

Started by houseofkraft, 28 September 2016 - 06:23 PM
houseofkraft #1
Posted 28 September 2016 - 08:23 PM
Hi Guys!

I wanted to virtually type a key using Lua so i can type "h" on a computer and using wireless modems to type "h" on the actual PC.

i tried this:

p1 = "paste"
p2 = "h"
p3 = nil
os.queueEvent("paste", p1, p2, p3)

And it just typed "paste"

Please Help

Thanks!
supernicejohn #2
Posted 28 September 2016 - 09:02 PM
Wouldn't "key" event be better? or just "char" if it's only for typing. Unsure where the pullEvent is, or if it's something else. Let's hope someone knows a bit more about these things than I do…
houseofkraft #3
Posted 28 September 2016 - 09:23 PM
Hmmm. the key event will just say the key number. Did not try char yet. Im trying it now

EDIT: I did os.queueEvent("char", "h") and it wrote h, Thanks!

EDIT2: How do i virtually make it press the enter key, ctrl, backspace, etc.
Edited on 28 September 2016 - 07:29 PM
valithor #4
Posted 28 September 2016 - 09:26 PM
It printed "paste" because you passed the following arguments to os.queueEvent: "paste", "paste", "h", nil

The first argument to os.queueEvent is the event type, for the paste event the second is what was actually in the clipboard when ctrl+v was pressed, so that is where the "h" should be.


os.queueEvent("paste","h")

Although that would probably work I would personally use the char and key event (I would queue both solely because many programs use both events for different things).


str = "hello"
for letter in string.gmatch(".") do
  os.queueEvent("char",letter)
  os.queueEvent("key", keys[letter],false)
end

Keep in mind though that this would break if there was more than 120ish letters in the string.

edit:

Luckily all of the built in scripts support char events that are more than a single char (whether or not this was intentional).

example:

str = "hello"
os.queueEvent("char",str,false)
Edited on 28 September 2016 - 07:29 PM
houseofkraft #5
Posted 28 September 2016 - 09:49 PM
Yeah ik, but how do i make it register backspaces, spaces, etc.
supernicejohn #6
Posted 28 September 2016 - 09:50 PM
"It printed "paste" because you passed the following arguments to os.queueEvent: "paste", "paste", "h", nil". Missed that! :)/>
For backspace and other keys, the key event would likely work. Not sure what the implementation is but that's the only thing I can think of.
houseofkraft #7
Posted 28 September 2016 - 09:56 PM
How could i use the key event to do that? I thought it just says numbers for each key
KingofGamesYami #8
Posted 28 September 2016 - 10:07 PM

os.queueEvent( "key", keys.backspace )
Source
Edited on 28 September 2016 - 08:07 PM
houseofkraft #9
Posted 29 September 2016 - 09:12 PM
Thanks!

Off to coding!
Edited on 29 September 2016 - 07:12 PM