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

How To Use Back Space?

Started by Zenon, 29 June 2015 - 12:36 AM
Zenon #1
Posted 29 June 2015 - 02:36 AM
Hi There All, I Was Looking At The Coroutine Guide Earlier And It Has This Code:

local function getChars()
local termX, termY = term.getCursorPos()
while true do
  local e = {os.pullEvent()}
  if e[1] == "key" and e[2] == keys.enter then
   return true
  elseif e[1] == "char" then
   term.setCursorPos(termX, termY)
   term.write(e[2])
   termX = termX + 1
  end
end
end
local function saveChars()
local file = fs.open("test_file", "w")
while true do
  local e = {os.pullEvent()}
  if e[1] == "key" and e[2] == keys.enter then
   term.clear() term.setCursorPos(1,1) term.write("Saved the file.")
   file.close()
  elseif e[1] == "char" then
   file.write(e[2])
  end
end
end

But I Noticed You Cant BackSpace? How Would One Achieve That?


[EDIT]
Actaully, I Think I Could Fix This By Just Not Constantly Writing :P/> Thanks Anyway Lol
Edited on 29 June 2015 - 01:15 AM
Bomb Bloke #2
Posted 29 June 2015 - 03:18 AM
On getting a "key" event equals to keys.backspace, get the current cursor position, move it one column to the left, write a space, then move the cursor one column to the left again.

As you cannot do this within your file handle (there's no file seeking available in ComputerCraft), you'll want to save each character typed to a variable instead (which you can remove characters from). When the user hits enter, then save the full contents of the string in one go.

The example you posted serves to demonstrate how you might run a pair of functions side-by-side. It does not aim to be a good demonstration of how to handle text input. If you take a look in bios.lua (link is for CC 1.71's copy), you can see the source for the much more capable read() function there - it handles things like text scrolling etc.
Zenon #3
Posted 29 June 2015 - 03:28 AM
On getting a "key" event equals to keys.backspace, get the current cursor position, move it one column to the left, write a space, then move the cursor one column to the left again.

As you cannot do this within your file handle (there's no file seeking available in ComputerCraft), you'll want to save each character typed to a variable instead (which you can remove characters from). When the user hits enter, then save the full contents of the string in one go.

The example you posted serves to demonstrate how you might run a pair of functions side-by-side. It does not aim to be a good demonstration of how to handle text input. If you take a look in bios.lua (link is for CC 1.71's copy), you can see the source for the much more capable read() function there - it handles things like text scrolling etc.

Yeah, I Was Looking At It While Using The Program On The Coroutine Guide And Just Noticed That Haha. Thanks For The Help!
InDieTasten #4
Posted 29 June 2015 - 12:03 PM
Slight hint: In English, you start every word in lower-case, except at the beginning of a sentence and names(which in some cases are hard to tell) ;)/>

Example: My dog Snoopy is always biting my guests.
Example: Your English skills are otherwise pretty good. Note, that English is the name of the language.
I think you get the idea. If you come from a language, where there are a lot more upper-case words, it's difficult to get into that.

As I said. Don't feel offended or anything. Just trying to give you some advise on how you can improve your linguistic skills :)/>
You can now call me grammar nazi or whatevaar :P/>
Zenon #5
Posted 29 June 2015 - 06:59 PM
Slight hint: In English, you start every word in lower-case, except at the beginning of a sentence and names(which in some cases are hard to tell) ;)/>

Example: My dog Snoopy is always biting my guests.
Example: Your English skills are otherwise pretty good. Note, that English is the name of the language.
I think you get the idea. If you come from a language, where there are a lot more upper-case words, it's difficult to get into that.

As I said. Don't feel offended or anything. Just trying to give you some advise on how you can improve your linguistic skills :)/>
You can now call me grammar nazi or whatevaar :P/>

My First Language Is English, Aswell As Being My Best Subject (Straight-A Student In Said Subject). Its Just Force Of Habit Sorry :P/>

[Edit]
Im Also A Grammar Nazi On Almost Everything But Capitals Haha
Edited on 29 June 2015 - 05:00 PM
InDieTasten #6
Posted 29 June 2015 - 07:52 PM
I know this is really off-topic, but is there a particular reason you do this? I mean, I'm from Germany and have never seen such horrible looking sentences xD It looks really weird to me^^
Zenon #7
Posted 09 July 2015 - 10:31 PM
I know this is really off-topic, but is there a particular reason you do this? I mean, I'm from Germany and have never seen such horrible looking sentences xD It looks really weird to me^^

Yeah, Haha, I Started Doing It As A Kid -I Didnt Know Better Back Then- And Its Just Stuck