This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
How do i capture both key and a char event in the os.pullevent
Started by roosterhat, 18 February 2013 - 03:23 PMPosted 18 February 2013 - 04:23 PM
i want it to capture the char mainly but i also want to capture the enter and backspace keys too
Posted 19 February 2013 - 03:41 AM
Split into new topic.
Posted 19 February 2013 - 03:51 AM
It goes a little something like this:
while true do
local event, param1 = os.pullEvent()
if event == "key" then
-- key was pressed
elseif event == "char" then
-- key with a char was pressed
end
end
Posted 19 February 2013 - 04:58 PM
it prints both though
Posted 19 February 2013 - 04:59 PM
im trying to make a text box thats has edges and i need to use char over io.read() and i need to be able to capture enter and delete
Posted 19 February 2013 - 05:00 PM
Not if you do it write. Post the code you have done. :)/>it prints both though
function read() do
local input = ""
while true do
local event, p = os.pullEvent()
if event == "char" then
input = input..p
elseif event == "key" then
if p == keys.enter then
return input
elseif p == keys.backspace then
input = input:sub(1, #input - 1)
end
end
end
error( "should never get here" )
end
Edited on 19 February 2013 - 04:03 PM
Posted 19 February 2013 - 05:02 PM
- oops thought i hit edit -
Edited on 19 February 2013 - 04:03 PM
Posted 19 February 2013 - 05:07 PM
i could do
keys = {}
while true do
keys[1]=""
keys[2]=""
local event, param1 = os.pullEvent()
if event == "key" then
key[1] = param1
elseif event == "char" then
key[2] = param2
end
end
no that prints the key ID then two spaces followed by the Chari could dokeys = {} while true do keys[1]="" keys[2]="" local event, param1 = os.pullEvent() if event == "key" then key[1] = param1 elseif event == "char" then key[2] = param2 end end
Posted 19 February 2013 - 05:08 PM
I don't even see how it does that. you have no print in there. Id suggest using a modification of the code I posted above.no that prints the key ID then two spaces followed by the Char
Posted 19 February 2013 - 05:14 PM
ok thanks that works
Posted 19 February 2013 - 05:15 PM
no problems
Posted 19 February 2013 - 05:15 PM
ill just have to modify your coded so that it prints out the key every time instead of after the enter
Posted 19 February 2013 - 05:17 PM
For that you would doill just have to modify your coded so that it prints out the key every time instead of after the enter
function read() do
local input = ""
local cx,cy = term.getCursorPos()
while true do
local event, p = os.pullEvent()
if event == "char" then
input = input..p
elseif event == "key" then
if p == keys.enter then
return input
elseif p == keys.backspace then
input = input:sub(1, #input - 1)
end
end
term.setCursorPos(cx,cy)
write(input.." ")
end
error( "should never get here" )
end
then you use it just the same as normal read.
write("Your name: ")
local name = read()
could even add a password mask just like the real read. would call it like this:
write("Your pass: ")
local pass = read("*")
and in the read code you would do this
function read( mask )
-- .... other code here
term.setCursorPos(cx,cy)
write( ( mask and string.rep(mask, #input) or input ).." ")
end
Edited on 19 February 2013 - 04:21 PM
Posted 19 February 2013 - 05:29 PM
i debated using the read but i want the text to stop at the end of a text box for the login and for the chat box and i dont know of a way to abort read with the enter key
Posted 19 February 2013 - 05:30 PM
oh and the the backspace is not working for me
Posted 19 February 2013 - 05:35 PM
oh never mind i fixed it
missing the #
missing the #
Posted 19 February 2013 - 05:37 PM
thanks so much now i can move on to more important things in my program :)/>