Hi,
I’m trying to update some of my works on Codea 3.x to Codea 4.0 and I found with this issue. How I can show/hide the onscreen keyboard? The classic way drops this error:
Error in call to setup():
Documents/Tgb/Main.lua:5: attempt to call a nil value (global ‘showKeyboard’)
stack traceback:
Documents/Tgb/Main.lua:5: in function ‘base.setup’
@Bri_G … yes I tried first the limited built-in help in 4.0… not much information indeed about how to use the keyboard to ask the user for an imput: (input - Codea 4.0 documentation)
In context I need some very simple as:
function setup()
showKeyboard()
inputText = “”
end
function draw()
background(40, 40, 50)
fill(255)
text(“Write something and press ENTER:”, WIDTH/2, HEIGHT/2 + 50)
text(inputText, WIDTH/2, HEIGHT/2)
end
function keyboard(key)
if key == RETURN then
hideKeyboard()
else
inputText = keyboardBuffer
end
end
ok.. not the easy way with Codea 4.0, but I did this (it only works with an external keyboard):
-- Hello User App - Codea 4.0
local name, input, state = "", "", 0 -- 0=start, 1=typing, 2=greeting
function setup()
style.font("Verdana")
style.fontSize(24)
end
function draw()
background(40, 40, 50)
style.fill(255)
style.textAlign(CENTER)
style.textStyle(TEXT_NATIVE)
if state == 0 then
text("Tap screen to enter your name", WIDTH/2, HEIGHT/2 + 25)
text("👆 Tap here", WIDTH/2, HEIGHT/2 - 25)
elseif state == 1 then
text("Enter your name:", WIDTH/2, HEIGHT/2 + 50)
processInput()
if input == "" then
style.fill(150)
else
style.fill(100,200,255)
end
text(input == "" and "(type here)" or input, WIDTH/2, HEIGHT/2)
style.fill(255)
text("Press Enter when done", WIDTH/2, HEIGHT/2 - 50)
else
style.fill(100,255,100)
style.fontSize(32)
text("Hello, " .. (name == "" and "Anonymous" or name) .. "! 👋", WIDTH/2, HEIGHT/2)
style.fontSize(18)
style.fill(200)
text("Tap to restart", WIDTH/2, HEIGHT/2 - 60)
end
end
function touched(t)
if t.state == BEGAN then
state = state == 2 and 0 or (state == 0 and 1 or state)
if state == 1 then input = "" end
if state == 0 then name, input = "", "" end
style.fontSize(24)
end
end
function processInput()
-- Letters
for i = 97, 122 do
local k = key[string.char(i)]
if k and key.wasPressed(k) then
local c = string.char(i)
input = input .. (key.modifiersPressed(key.shift) and string.upper(c) or c)
end
end
-- Numbers & special keys
for i = 0, 9 do
local k = key['num'..i]
if k and key.wasPressed(k) then input = input .. i end
end
if key.wasPressed(key.space) then input = input .. " " end
if key.wasPressed(key.backspace) and #input > 0 then
input = string.sub(input, 1, #input - 1)
end
if key.wasPressed(key.enter) then
name = input:match("^%s*(.-)%s*$")
state = 2
end
end
The showKeyboard and hideKeyboard functions in 3.x are a bit of a hack. We weren’t sure whether to keep them
This API will get re-thought for 4.x. Off the top of my head, something more like:
keyboard.show()
keyboard.hide()
keyboard.textEntered = function (value)
-- `value` is a string
end
keyboard.deleteBackward = function ()
-- Delete
end
-- whatever has been entered so far using the software keyboard
keyboard.text