779 posts
Location
Kerbin
Posted 29 March 2015 - 07: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
756 posts
Posted 29 March 2015 - 07:48 PM
Can you elaborate more? what is this supposed to do?
Tip: You can also have 'keys.enter' instead of 28.
Edited on 29 March 2015 - 05:49 PM
1140 posts
Location
Kaunas, Lithuania
Posted 29 March 2015 - 08: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 ;)/>
1029 posts
Location
Missouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension
Posted 29 March 2015 - 08:32 PM
No, I'd do:
while not(e=="key" and key == 28) do
end
957 posts
Location
Web Development
Posted 29 March 2015 - 10:30 PM
How I do blinking?
term.setCursorBlink(true)
term.setCursorBlink(false)
Can I do the same in an easier way?
Why not just use the default 'read()' function?
779 posts
Location
Kerbin
Posted 30 March 2015 - 07: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 on 30 March 2015 - 05:13 PM
779 posts
Location
Kerbin
Posted 30 March 2015 - 04: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 on 30 March 2015 - 02:51 PM
1140 posts
Location
Kaunas, Lithuania
Posted 30 March 2015 - 06: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.
779 posts
Location
Kerbin
Posted 30 March 2015 - 07:05 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.
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 on 30 March 2015 - 05:09 PM