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

How to detect how many characters are in a line?

Started by Astrophylite, 21 August 2015 - 10:26 AM
Astrophylite #1
Posted 21 August 2015 - 12:26 PM
Hello!

So I was just coding a typing program (don't ask, I was bored), and I just wondered how I could make the delete, home and end keys work.
Here is the pastebin: http://pastebin.com/sAA8CvNK

Also, how do I make it detect capsLock and Shift. As you can see in the code, I have tried but to no avail..

Any help will be appreciated,
_zircon_
MKlegoman357 #2
Posted 21 August 2015 - 01:51 PM
You don't use "key" event for characters, you'd use the "char" event, which automatically deals with shift and caps lock. Also, you could use the key constants in the 'keys' table to check:


print("Press [Enter] to exit the program.")

while true do
  local event, key = os.pullEvent("key")

  if key == keys.enter or key == keys.numPadEnter then
    break
  end
end

For actual cursor. You aren't even saving the typed in characters anywhere. You should be putting them in, for example, a string. After each change, when you type or delete something, you'd redraw the string. Take a look at how read() is doing everything.
Edited on 21 August 2015 - 11:52 AM
Astrophylite #3
Posted 21 August 2015 - 02:57 PM
-snip-

Thanks for your quick reply! I will try to use the char event and if it doesn't work, I'll post here :P/>
Astrophylite #4
Posted 21 August 2015 - 03:34 PM
Well then… It made my code a hell of a lot shorter.. The only thing is, how do I make the end and delete keys work? I have got the backspace, home and enter working (using the key event) but end I am still stuck on…
KingofGamesYami #5
Posted 21 August 2015 - 03:52 PM


end should be 207, delete 211.
Astrophylite #6
Posted 21 August 2015 - 04:05 PM
I am using
 keys.getName(p1) 
p1 being the key that was detected. You know how your typing in Notepad or something, you press END then it goes to the end of the line? That is what I want it to do but I don't know how..
KingofGamesYami #7
Posted 21 August 2015 - 04:51 PM
Uhh… you need to remember the stuff being written somewhere. Like the edit program does.

Then you have to save where the person is writing in the file, and how much the file has been scrolled, etc. Also if the cursor would move offscreen you should fix that.
Astrophylite #8
Posted 21 August 2015 - 05:09 PM
Ugh… I was hoping that that wasn't going to be the response. Oh well, I will attempt to work on it …