Howdy, Stranger!

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

Saving a string made with keyboard buffer

edited September 2013 in Questions Posts: 577

Hello! I've been trying to make a calculator that uses the keyboard buffer for input, but when I close the keyboard, anything that has been typed is gone. How do I fix this?

Thanks

Comments

  • You have to process the keys before closing the keyboard. You could also batch them up in a string and then process the string.

  • dave1707dave1707 Mod
    Posts: 7,528

    @Prynok Here's a simple keyboard routine.


    displayMode(FULLSCREEN) supportedOrientations(PORTRAIT) function setup()     hideKeyboard()     showKeyboard()     str=""     nbr=0    end function draw()     background(40,40,50)     rectMode(CENTER)     fill(255)     if keyboardBuffer() ~= nil then         str=keyboardBuffer()     end     text("number  "..nbr,300,730)     text("Enter number then press return",300,640)     rect(300,600,200,50)     fill(255,0,0)     text(str,300,600) end function keyboard(k)     if k==RETURN then         hideKeyboard()         nbr=str         if nbr=="" or nbr==nil then             nbr=0         end     end   end function touched(t)     if t.state==BEGAN then         showKeyboard()     end end
  • Posts: 577

    I think I see the problem, the error that pulls up says: bad argument #1 to 'sqrt' (number expected, got string.) So i'm guessing the keyboard buffer turns a integer into a string?

  • dave1707dave1707 Mod
    Posts: 7,528

    @Prynok In my example above, if I replace 


    text("number "..nbr,300,700 with text("number  "..math.sqrt(nbr),300,730)

    the square root calculation works. I'm not sure how you're doing your keyboard routine.

  • Posts: 577

    This is weird.... When I try it in my program it still thinks its a string


    -- Calculator supportedOrientations(PORTRAIT_UPSIDE_DOWN) function setup() str = "" nbr = 0 display = false form1 = false displayMode(FULLSCREEN) -- Name of the app liney = 900 lines = {} for i = 1, 15 do table.insert(lines, vec2(WIDTH, liney)) liney = liney - 60 end Name = "Math Helper" finder = "" end -- This function gets called once every frame function draw() -- This sets a dark background color background(255, 255, 255, 255) strokeWidth(2) stroke(97, 153, 229, 255) --Code for the fancy blue lines for i=1, #lines do line(0, lines[i].y, WIDTH, lines[i].y) end stroke(239, 45, 46, 183) line(60,0,60,HEIGHT) -- Math helper logo fill(25, 25, 25, 183) fontSize(35) font("Noteworthy-Bold") text(Name, WIDTH/2, HEIGHT - 30) if form1 == false and keyboardBuffer() ~= nil then finder = keyboardBuffer() elseif form1 == true and keyboardBuffer() ~= nil then str = keyboardBuffer() end if isKeyboardShowing() == true then fontSize(30) font("Didot") fill(192, 192, 192, 255) background(28, 29, 55, 255) text(finder, WIDTH/2, HEIGHT/2) end if keyboardBuffer() == "sqaure root" or keyboardBuffer() == "sqaureroot" or form1 == true or display == true then form1 = true -- text(math.sqrt(11) "x" math.sqrt(11) "= 11", 400, 200) if display == true then text(""..math.sqrt(nbr).. " X " ..math.sqrt(nbr).." = 11", WIDTH/2, HEIGHT/2) end -- line(WIDTH/2 - 50, HEIGHT/2, WIDTH/2, HEIGHT/2 ) end end function touched(call) if t.tapCount == 1 then form1 = false display = false showKeyboard() else hideKeyboard() end end function keyboard(key) if key == RETURN and form1 == true then hideKeyboard() form1 = false nbr=str if nbr == "" or nbr==nil then nbr = 0 end if key == RETURN and str ~= "" then display = true end end end
  • keyboardBuffer() returns a string of previously typed text. Typing in a number does not make it a number type, it is still a string. To use math functions on it, use tonumber(string).

  • Posts: 577

    @SkyTheCoder then how did @dave1707 make his work? :-/

  • @Prynok No idea. Well, idea. If he didn't type in anything and press enter, it would be fine, since nbr is set to 0. If he typed something in, to would be a string, though.

    Another thing to take into account while using the keyboard is that if someone presses the "hide keyboard" button (bottom right), you need to have a procedure to figure that out, so it doesn't think you're still typing. This can be done with isKeyboardShowing(), and a custom boolean to be set when you showKeyoard() or hideKeyboard(). Then you can just compare the two.

  • Posts: 577

    @SkyTheCoder ok, I've been trying to do the tonumber(nbr) method, but it still wants it to be a string, any ideas?

    (I put it under the keyboard function

  • dave1707dave1707 Mod
    Posts: 7,528

    @Prynok In my code above, as long as I type a number or a decimal point, it works. If I key in a comma (12,345) then I get an error. One thing you can do is when you're expecting just a number, add code in the keyboard routine to only allow a number or decimal point.


  • -- Calculator supportedOrientations(PORTRAIT_UPSIDE_DOWN) function setup() str = "" nbr = 0 display = false form1 = false displayMode(FULLSCREEN) -- Name of the app liney = 900 lines = {} for i = 1, 15 do table.insert(lines, vec2(WIDTH, liney)) liney = liney - 60 end Name = "Math Helper" finder = "" end -- This function gets called once every frame function draw() -- This sets a dark background color background(255, 255, 255, 255) strokeWidth(2) stroke(97, 153, 229, 255) --Code for the fancy blue lines for i=1, #lines do line(0, lines[i].y, WIDTH, lines[i].y) end stroke(239, 45, 46, 183) line(60,0,60,HEIGHT) -- Math helper logo fill(25, 25, 25, 183) fontSize(35) font("Noteworthy-Bold") text(Name, WIDTH/2, HEIGHT - 30) if form1 == false and keyboardBuffer() ~= nil then finder = keyboardBuffer() elseif form1 == true and keyboardBuffer() ~= nil then str = keyboardBuffer() end if isKeyboardShowing() == true then fontSize(30) font("Didot") fill(192, 192, 192, 255) background(28, 29, 55, 255) text(finder, WIDTH/2, HEIGHT/2) end if keyboardBuffer() == "sqaure root" or keyboardBuffer() == "sqaureroot" or form1 == true or display == true then form1 = true -- text(math.sqrt(11) "x" math.sqrt(11) "= 11", 400, 200) if display == true then text(""..math.sqrt(nbr).. " X " ..math.sqrt(nbr).." = 11", WIDTH/2, HEIGHT/2) end -- line(WIDTH/2 - 50, HEIGHT/2, WIDTH/2, HEIGHT/2 ) end end function touched(call) if t.tapCount == 1 then form1 = false display = false showKeyboard() else hideKeyboard() end end function keyboard(key) if key == RETURN and form1 == true then hideKeyboard() form1 = false nbr=str if nbr == "" or nbr==nil then nbr = 0 end nbr = tonumber(nbr) -- Change if key == RETURN and str ~= "" then display = true end end end

    Maybe that'll work? I added one line of code. Also, by the way, it's spelled square root, not sqaure root.

  • Posts: 577

    For my code also, I just put in numbers

  • Posts: 577

    @SkyTheCoder I'm not sure if thats how the tonumber function works, because now we are getting nil

  • dave1707dave1707 Mod
    Posts: 7,528

    tonumber will only convert a string to a number only if the string is a number. If the string contains anything other than a number you'll get nil or inf.

  • Posts: 577

    @dave1707 but the only things I put in are numbers, any other things that might of caused this?

  • dave1707dave1707 Mod
    Posts: 7,528

    @Prynok Exactly what are you keying in, give me an example. Do you key "sqaure root" then return, a number then return.

  • Posts: 577

    Here is what I do, I type in the word square root, so my program knows thats what I want to do, then it switches to a different interger for the keyboard buffer, and it takes in the number. So I don't see how any letters could get into the second one

  • edited September 2013 Posts: 577

    Oh... I found the problem, for some reason the number also copies the operation name... How would I fix this?

    Edit: Got it working, thank you so much guys!

Sign In or Register to comment.