Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Newbie Asks: keyboard input?

edited January 2012 in Questions Posts: 28

Just got the new version of Codea and am trying some ultra-novice coding. Got as far as "io.read," which does not seem to work. Can anyone tell me how (or if) Codea accepts input from the keyboard? Can't seem to find any info on this. Many thanks for any help.

Tagged:

Comments

  • Posts: 118

    The info is under Display and keyboard in the help (http://twolivesleft.com/Codea/Reference/#functions/index/display)

    Here is the (slightly adapted to enable newlines) example:

    function setup()
        print("Tap to start editing")
        textWrapWidth(40000)
    end
    
    function touched(touch)
        --Show keyboard when the screen is touched
        showKeyboard()
    end
    
    function draw()
        background(40,40,50)
        fill(255)
        textMode(CORNER)
        buffer = keyboardBuffer()
    
        _,bufferHeight = textSize(buffer)
        if buffer then
            text( buffer, 10, HEIGHT - 30 - bufferHeight )
        end
    end
    
  • Posts: 2,820
    1. Codea does not have everything the full lua documentation has like file io. Much of it is coming soon :-)
    2. Here is some code I made that uses keyboard input:

    function setup()     displayMode(FULLSCREEN)     print("Hello World!")     showKeyboard() end function draw()     background(255, 255, 255, 255)     strokeWidth(5)            textMode(CORNER)     buffer = keyboardBuffer()     bufferHeight = textSize(buffer)     if buffer then         text(buffer, 10, 400)     end      end

    Sorry for not responding earlier.

  • Posts: 2,820

    Oh. I didn't see@Herwig's respons.

  • Thanks to both Zoyt and Herwig! This is very helpful, and I'll try out the code asap.

  • SimeonSimeon Admin Mod
    Posts: 4,940

    @raferguzman keyboardBuffer() is a convenience method to get the text typed with the keyboard so-far, but it may be flushed at any time. If you want a true and accurate representation of what was typed with the keyboard you can use the global keyboard( key ) callback, like this:

    function setup()
        showKeyboard()
    
        -- Use a table for the buffer
        -- Faster than a string
        myBuffer = {}
    end
    
    function draw()
        background(0)
    end
    
    function readBuffer()
        -- Read the buffer as a string
        return table.concat( myBuffer, "" )
    end
    
    function keyboard(key)
        -- This function gets called every time a key is pressed
        if key == BACKSPACE then
            table.remove( myBuffer, #myBuffer )
        else
            myBuffer[#myBuffer+1] = key
        end
    end
    

    This is a bit more overhead to get a buffer of keys typed, but it gives you full control over when and how it is flushed.

  • Simeon, thanks -- I'll give this a try as well!

  • edited March 2018 Posts: 117

    How can i make the PGM respond to only “t” when t is pressed and not any key ?
    This prints to compiler only...

    -- Main
    
    -- Make a PGM take a keybord input
    
     showKeyboard()
    
    Time=t
    
    print("Press any key to continue, then press return")
    
    io.read()        -- Will print done when a key  is pressed
    
    print("Done")
    
Sign In or Register to comment.