Help with using SDK Keyboard

Hey everyone,

I’m very new to using the SDK after previously making a small game in Pulp.

I’m finding answers for most of what I need so far, but I’m a bit confused by how to use the built-in keyboard.

My goal is to allow the player to set their name to be used elsewhere in the game, but I’m not really sure the best way to do this.

My most recent attempt was to create an image (a simple black rectangle), and draw the keyboard input to it via pushcontext and popcontext. This led to the first letter being written to the image, but it doesn’t update further.

If anyone has advice on how to properly use the keyboard it would be very appreciated!

I can provide my code if it helps as well, but I think my ideas so far were not really accomplishing anything.

Thank you for reading, and for any help you can offer!

Recently learned how to use the keyboard for a project. So happy to help. I've pulled out just the important bits to try and keep the example simple. But this should open the keyboard, and keep the sprite updated with the keyboard entry.

It's nothing fancy, but serves as a simple example of updating a sprite with keyboard input. There's a number of other useful methods you can set on the keyboard to detect it opening/closing it's width etc. But I'll leave that to you to experiment with.

import "CoreLibs/graphics";
import "CoreLibs/sprites";
import "CoreLibs/keyboard";
import "CoreLibs/timer";

local mySprite = playdate.graphics.sprite.new();
mySprite:setImage(playdate.graphics.image.new(200,100));
mySprite:add();
mySprite:setCenter(0,0);
mySprite:moveTo(0,0)

local updateSprite = function(text)
	playdate.graphics.pushContext(mySprite:getImage());
	playdate.graphics.clear();
	playdate.graphics.drawTextInRect(text, 0,0, 200,100);
	playdate.graphics.popContext();
	mySprite:markDirty();
end

local showKeyboard = function()
	updateSprite("");
	playdate.keyboard.show("");
	playdate.keyboard.textChangedCallback = function()
		updateSprite(playdate.keyboard.text);
	end
end

function playdate.update()
	playdate.graphics.sprite.update();
	playdate.timer.updateTimers();
	playdate.drawFPS(2, 224);
end

showKeyboard();
1 Like

This was massively helpful, thank you so much!

1 Like