pull char event while also getting other keys.

Started by milo, Jun 16, 2023, 05:10 AM

Previous topic - Next topic

milo

EDIT: found a solution after some tinkering. it turns out that you don't have to specify a filter for os.pullEvent.

here is my code to store what the user is typing to a string called `written_str`, while also displaying it onscreen.

term.setCursorBlink(true)
local tx, ty = term.getCursorPos(x,y)
local written_str = ""

while true do
    local event, a, b = os.pullEvent()
    local redraw = true

    if event == "key" then
        if a == keys.backspace then
            written_str = string.sub(written_str, 1, -2)
        end
    elseif event == "char" then
        write(a)
        written_str = written_str .. a
    else
        redraw = false
    end

    if redraw then
        term.setCursorPos(tx,ty)
        io.write(written_str, "  ")
        io.flush()
        term.setCursorPos(#written_str+1,ty)
    end
end



I'm trying to make a program where the user can type like usual, but the program recieves the input immediately as it's typed (without consuming it), as opposed to when the line ends like with `io.read`

while true do
    local _, ch = os.pullEvent("char")
    io.write(ch)
    io.flush()
end


the `char` event seems perfect for this as I can simply write the character to the screen whenever the event is pulled, but backspace doesn't work.

To get the backspace key I also need to pull the `key` event.

how do I simultaneously listen for both events?