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

Write to current editable line.

Started by LemonyLime, 15 October 2012 - 12:11 AM
LemonyLime #1
Posted 15 October 2012 - 02:11 AM
I want to write a string to the current prompt, meaning that it will be written to the bottom line, and will remain there for the user to edit further. Any ideas how to do this?
Lyqyd #2
Posted 15 October 2012 - 02:23 AM
Take a look at how read() does it.
Lettuce #3
Posted 15 October 2012 - 02:23 AM
Do you mean like a Username thing? Like it would display "Username:" and you could type it out so it looks like "Username: LemonyLime"?
LemonyLime #4
Posted 15 October 2012 - 02:35 AM
Take a look at how read() does it.
read() is a really large function… I don't think I'm quite qualified to understand what I'm looking at. lol

Do you mean like a Username thing? Like it would display "Username:" and you could type it out so it looks like "Username: LemonyLime"?
Sort of, but after I print "Username:", I want to be able to edit "Username:" itself, not just type next to it… and when I use read() to get that line, I want everything on the line, including what I've printed out to be included.
LemonyLime #5
Posted 15 October 2012 - 02:48 AM
Actually, I think I may have gotten it. I started using Lua for the first time like 3 or 4 hours ago, so I almost didn't even try, lol. I didn't realize that you could manually set the cursor position to anywhere and still be able to type from that position… and looking at read(), I don't think it matters what line you're on, so it should still work, no matter where you're writing, right? I'll give it a shot.
Lettuce #6
Posted 15 October 2012 - 02:53 AM
That's a really good question. Be sure to post if you can get it to work, cause I don't know. People would like to read that too, in case they ever have need of it.
LemonyLime #7
Posted 15 October 2012 - 03:02 AM
Ugh, yeah I get how to do it, but it's making my head spin. haha

I'll post my code when I get it working. I'm basically just making notepad. lol
Kingdaro #8
Posted 15 October 2012 - 03:13 AM
I don't think read() can do this, because the backspace in read() can only go to where it once started.

I think the best way of doing this would be to have your text stored in a variable, and allowing the user to add and remove characters from that string with key events.
Lyqyd #9
Posted 15 October 2012 - 03:29 AM
Oh, well if you're trying to make a program like edit, take a look at how edit does it.
Kingdaro #10
Posted 15 October 2012 - 03:51 AM
Oh, well if you're trying to make a program like edit, take a look at how edit does it.
Edit does it in the way I described, while storing multiple lines in a table.
Lyqyd #11
Posted 15 October 2012 - 04:03 AM
Oh, well if you're trying to make a program like edit, take a look at how edit does it.
Edit does it in the way I described, while storing multiple lines in a table.

Yes, it does. That post was for the benefit of the OP, which I should have probably made clearer in my post.
Kingdaro #12
Posted 15 October 2012 - 04:04 AM
Lolno, I was just adding a point to your post.
LemonyLime #13
Posted 15 October 2012 - 05:31 AM
Well, I'm gonna cancel this project for the moment. This was just my first program for learning the language, and I think I've gotten a bit ahead of myself with this one. I haven't even done anything with turtles yet. haha

I'd like to just ask a couple more general questions before heading to bed though. Hopefully that's not a problem, since this thread wasn't about these topics.

1. How can I simulate a keypress? Can't seem to find anything on this at all. =/
2. Is there any way at all to get the position of the mouse, or whether or not it's being clicked?

Thanks again.
Kingdaro #14
Posted 15 October 2012 - 05:51 AM
The first is possible using os.pullEvent:

local ev, p1 = os.pullEvent()
if ev == 'key' then
print(p1) -- this prints the numbered code of the key pressed
end

You can use the keys API to check which key was pressed without having to memorize all of the keyboard codes.

local ev, p1 = os.pullEvent()
if ev == 'key' and p1 == keys.enter then
print 'The user pressed enter!'
end

As for your second question, it's not possible on regular computers, but it might be possible on golden computers.
LemonyLime #15
Posted 15 October 2012 - 06:09 AM
Ah, that's not quite what I meant. I want to make the program press the button, not check when the user presses it.

And speaking of Golden computers… I've seen them on videos, but I don't seem to have them. What's up?
Afaik, I have the latest version of computercraft.
Lettuce #16
Posted 15 October 2012 - 11:43 AM
You can try to do

write "t"
sleep(0.1)
write "e"
sleep(0.1
write "x"
sleep(0.1)
write "t"
to get that effect, but it's tedious. And Kingdaro means the Beta version, which is currently semi-stable.
Kingdaro #17
Posted 15 October 2012 - 11:48 AM
Ah, that's not quite what I meant. I want to make the program press the button, not check when the user presses it.

And speaking of Golden computers… I've seen them on videos, but I don't seem to have them. What's up?
Afaik, I have the latest version of computercraft.

In that case, I haven't really messed around with this yet, but there is os.queueEvent. I think that might accomplish what you need.

(oh god you're adding macros aren't you give me this script)