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
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
You have to process the keys before closing the keyboard. You could also batch them up in a string and then process the string.
@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
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?
@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.
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).
@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.
@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
@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.
For my code also, I just put in numbers
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.
@Prynok Exactly what are you keying in, give me an example. Do you key “sqaure root” then return, a number then return.
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
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!