playdate.keyPressed(key) is working on Simulator Version 2.4.0 for me. I'm on a 2020 M1 Mac Mini with Sonoma 14.2.1 using a Logitech MX Keys Mini keyboard.
Getting closer to the heart of the issue: conditionals inside it no longer work as expected/previously.
Testing key == "m", even though both are strings, returns false.
This is because in 2.4.0 the string value passed to playdate.keyPressed(key) is length 2.
Repro
import "CoreLibs/graphics"
function playdate.keyPressed(key)
print("|m|", type("m"), string.len("m"), "|"..key.."|", type(key), string.len(key), "==", key == "m")
if key == "m" then
print("test", "OK")
else
print("test", "bad")
end
end
function playdate.update()
playdate.graphics.drawText("*PRESS m KEY*",150,110)
end
I did try trimming everything but alphanumerics using a regex but it didn't make any difference. But I was so short on time and tired I can't say for sure that I did it correctly.
It's interesting that the trailing pipe character is also discarded in the actual output. Maybe that implies there's a null byte in-between.
import "CoreLibs/graphics"
function playdate.keyPressed(key)
print("keyPressed:", #key, "characters long.")
for i = 1, #key do
print(string.format(" 0x%02x", string.byte(string.sub(key, i)) .. " "))
end
end
function playdate.update() end
playdate.graphics.drawText("*PRESS A KEY*",150,110)
When I added the serial message handler I refactored the keyPressed/Released code to handle arbitrary strings, naively assumed I needed to give it a null terminated string and didn't think to test it.
Here's the relevant bit from the release notes about why the arbitrary strings:
Added callbacks for receiving messages via the serial port msg command
Lua: playdate.serialMessageReceived(message)
C: playdate->system->setSerialMessageCallback(void (*callback)(const char* data));
As a convenient way to get data into the Playdate runtime I've added a msg command that takes a string at a time and sends it to a callback. This is much easier than sending it one character at a time with btn or using eval which takes compiled Lua bytecode.
They go to the playdate.serialMessageReceived() callback instead of keyPressed/Released, but work the same. Or maybe I misunderstood your question?