I need clarification on how to make user input from the keyboard into a variable. My father put me up to creating a code that calculates QB ratings for quarterbacks in the NFL. So I need to figure out how to shove input from the user (from a keyboard) into a variable. I already have the keyboard working. Thanks!
Easy way is use a parameter. Or just save the keyboard buffer after enter is pressed into a variable. You may want to look at Cider 7 for creating a UI if you chose not to use parameters.
function setup() --MyVar will store user input. parameter.text("MyVar","QB name") end function draw() background(0, 0, 0, 255) text(MyVar,WIDTH/2,HEIGHT/2) end
@JessicGriffin The response I gave you in your other post ( Need Help: Keyboard Input ) shows how to get keyboard input into a variable. See the keyboard(k) routine.
@JessicGriffin Yes. You would “ask the question” and save the input into different variables. Then use those variables for what you want.
@JessicGriffin Here’s a quick example of inputting data using the screen instead of the output area. It’s set up for 2 questions and just outputs those input values. There will be a lot of changes for what you want, but the majority of what you need is here.
displayMode(FULLSCREEN) function setup() inp=1 var1="" var2="" str = "" bcolor= 0 x=300 y=500 w=200 h=50 l=x-w/2 --button left r=x+w/2 --right b=y-h/2 --bottom t=y+h/2 --top end function draw() background(30,30,0,255) strokeWidth(6) rectMode(CENTER) textMode(CENTER) fill(255) text("Tap button then input data.",x,450) text("Press RETURN to show input.",x,400) if bcolor == 0 then fill(255, 0, 0, 255) else fill(128,0,0,255) end rect(x,y,w,h) -- input area fill(255) if keyboardBuffer() ~= nil then text(keyboardBuffer(),x,500) -- show data as it is keyed else text(str,x,500) -- show data in input area end if inp==1 then text("Enter number of passes attempted.",300,700) elseif inp==2 then text("Enter number of passes completed.",300,700) end if inp==1 and str~="" then var1=str str="" inp=2 end if inp==2 and str~="" then var2=str str="" inp=1 end rect(x,HEIGHT-100,150,50) rect(x,HEIGHT-200,150,50) fill(255,0,0) text(var1,x,HEIGHT-100) text(var2,x,HEIGHT-200) end function touched() -- check if input area was touched if CurrentTouch.x > l and CurrentTouch.x < r then if CurrentTouch.y > b and CurrentTouch.y < t then showKeyboard() bcolor = 1 end end end function keyboard(key) -- move keyboardBuffer to str when return is pressed if key == RETURN then str = keyboardBuffer() hideKeyboard() bcolor = 0 end end
So would it be able to store information from each thing the user inputs? I need to make inputs to see how many passes completed, attempted, yards gained, touchdowns, and interceptions. There will be a button to switch to the next thing (At the beginning it will ask for how many passes attempted, then the user would hit a button and it would ask for the next thing…etc)
I never use the keyboardBuffer() function, just something like string = string … key in function keyboard(key).
@SkyTheCoder Using the keyboardBuffer() allows the use of the backspace key and other things without having to code for it. I don’t like the fact that the buffer can’t be cleared unless you do a hideKeyboard, showKeyboard.
Thanks everyone for the information. I’m hoping I can figure this out