Jump to content




cRead()?


  • You cannot reply to this topic
8 replies to this topic

#1 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 29 March 2015 - 06:34 PM

local function cRead()
local w,h = term.getSize()
local _,y = term.getCursorPos()
local s = ""
while key ~= 28 do
local e,key = os.pullEvent()
if e == "char" then
s=s..key
--clearing line y and cPrinting the s to line y

end
end
return s
end

My questions are:
Can I do the same in an easier way?
How I do blinking?

Note: 28 is enter


#2 Anavrins

  • Members
  • 775 posts

Posted 29 March 2015 - 06:48 PM

Can you elaborate more? what is this supposed to do?
Tip: You can also have 'keys.enter' instead of 28.

Edited by Anavrins, 29 March 2015 - 06:49 PM.


#3 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 29 March 2015 - 07:01 PM

Looks like a way to make a standalone read function. Without more code to see, I don't have any comments except one: I would use a 'while true do' loop instead of 'while key ~= 28 do'. The reason behind this is because if another event fires with a second parameter being 28 the read will end. This may happen with a timer event. Even if it's very unlikely to happen, I'd still put it into an if and use the 'break' keyword to exit the loop. Also, don't forget to add the keys.numPadEnter (the number pad enter, people use it) as another 'enter' key ;)

#4 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 29 March 2015 - 07:32 PM

No, I'd do:
while not(e=="key" and key == 28) do


end


#5 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 29 March 2015 - 09:30 PM

View PostKonlab, on 29 March 2015 - 06:34 PM, said:

How I do blinking?

term.setCursorBlink(true)

term.setCursorBlink(false)


View PostKonlab, on 29 March 2015 - 06:34 PM, said:

Can I do the same in an easier way?

Why not just use the default 'read()' function?

#6 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 30 March 2015 - 06:36 AM

thx for ur awesome replies
I think I created a cRead function with backspace, arrowkeys and home/end support:
local function clearLine(y)
term.setCursorPos(1,y)
for i=1,51 do
term.write(" ")
  end
end
local function cRead()
term.setCursorBlink(true)
local w,h = term.getSize()
local _,y = term.getCursorPos()
local s = ""
local p = 0 -- position of cursor
while true do
term.setCursorPos(w/2-math.floor(#s/2)+p,y)
local e,key = os.pullEvent()
if e == "char" then

s = table.concat({s:sub(1,p),key,s:sub(p+1,#s)})

  p=p+1

end
   if e == "key" and key == 14 and p > 0 then --backspace
	 s = table.concat({s:sub(1,p-1),s:sub(p+1,#s)})
	 p = p -1
   end
   if e == "key" and key == 28 then --enter
	 break
   end
   if e == "key" and key == 203 and p > 0 then --left arrow k.
	  p = p-1
   end
   if e == "key" and key == 205 and p < #s then --right arrow key
	   p = p+1
   end
   if e == "key" and key == 199 then --home
	   p = 0
   end
   if e == "key" and key == 207 then -- end
	  p = #s
   end
   if p > #s then --I had some issues, this is a fix for that
	 p = #s
   end
   clearLine(y)
term.setCursorPos(w/2-math.floor(#s/2),y)
term.write(s)

end
term.setCursorBlink(false)
return s
end
EDIT: I JUST QUICKLY ADDED KEYS IN COMMENTS

Edited by Konlab, 30 March 2015 - 06:13 PM.


#7 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 30 March 2015 - 03:49 PM

Anyone?
The new code is working but not sure if it's 100% bugless and it's a bit laggy.
Is there a way to be less laggy and buggy?

Edited by Konlab, 30 March 2015 - 03:51 PM.


#8 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 30 March 2015 - 05:37 PM

Use term.clearLine instead of your own implementation of the clearLine function, which depends on the screen size. Also, I'd suggest to use the 'keys' table to get the key codes, it makes the code a lot more readable.

#9 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 30 March 2015 - 06:05 PM

View PostMKlegoman357, on 30 March 2015 - 05:37 PM, said:

Use term.clearLine instead of your own implementation of the clearLine function, which depends on the screen size. Also, I'd suggest to use the 'keys' table to get the key codes, it makes the code a lot more readable.
Term.clearLine - a new function Or an old that I missed
edit: took a look at the term API documentation - wow there are a lot of new functions :o
ok i gonna use the key api instead

Edited by Konlab, 30 March 2015 - 06:09 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users