Howdy, Stranger!

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

Keyboard input to Variable?

edited November 2013 in Questions Posts: 37

I am trying to say that what the return key is pressed, set the variable to whatever is typed into the keyboard.
I would also like for the writing to show up on the screen as the user types it, and I don't know how.

I know his can be done because I've seen it before, but I would like some help if possible. Thanks : )

Comments

  • Posts: 577

    function keyboard(k)
    if k == RETURN then
    text = keyboardBuffer()
    end
    end

    Also put this in the draw function.
    text(text, WIDTH/2, HEIGHT/2)

  • @Prynok thanks! How do I set the variable that I want to put there? Is it in the brackets after keyboardBuffer?

  • Posts: 577

    @MistReaper That is all you do.

  • @Prynok So sorry, after looking at the code I figured out what I needed to do. However, when I run this, I get an error telling me the string value is nil. Why isn't it what I'm typing? Even if I try to define it as a variable, it still comes back with a nil value. Any advice?

  • edited November 2013 Posts: 577
    function setup()
    text = ""
    end
    
    function draw()
    background(255,143,255)
    text(text, WIDTH/2,HEIGHT/2)
    end
    
    function keyboard(k)
    if k == RETURN then
    text = keyboardBuffer()
    end
    end
    
  • @Prynok thanks so much. It doesn't show what you're typing but it still works. Thanks for all the help! : )

  • dave1707dave1707 Mod
    Posts: 7,657

    @MistReaper If you want to show what's being typed, try this code.


    function setup()     showKeyboard()     clr=false     str=""     str1="" end function draw()     background(40,40,50)     fill(255)     if clr then    -- clear keyboard buffer         hideKeyboard()         showKeyboard()         clr=false     end     str=keyboardBuffer()     if str ~= "" then         text(str,100,500)    -- show keyed input     else         text(str1,100,500)    -- show input after return pressed     end   end function keyboard(k)     if k==RETURN then         str1=keyboardBuffer()         clr=true         output.clear()         print(str1)     end end
Sign In or Register to comment.