Jump to content




How To Use Back Space?


6 replies to this topic

#1 Zenon

  • Members
  • 50 posts

Posted 29 June 2015 - 01: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 by theMCcm, 29 June 2015 - 02:15 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 29 June 2015 - 02: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.

#3 Zenon

  • Members
  • 50 posts

Posted 29 June 2015 - 02:28 AM

View PostBomb Bloke, on 29 June 2015 - 02:18 AM, said:

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!

#4 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 29 June 2015 - 11:03 AM

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

#5 Zenon

  • Members
  • 50 posts

Posted 29 June 2015 - 05:59 PM

View PostInDieTasten, on 29 June 2015 - 11:03 AM, said:

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 by theMCcm, 29 June 2015 - 06:00 PM.


#6 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 29 June 2015 - 06: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^^

#7 Zenon

  • Members
  • 50 posts

Posted 09 July 2015 - 09:31 PM

View PostInDieTasten, on 29 June 2015 - 06:52 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users