Skip to content

Keyboard and physical Keys

Miku AuahDark edited this page Feb 14, 2020 · 1 revision

Keyboard and physical Keys

Keyboard

To display the keyboard in an Android device you can use love.keyboard.setTextInput(true) and love.keyboard.setTextInput(false) to hide it.

Unfortunately there's bug in SDL which affects LÖVE for Android where calling love.keyboard.setTextInput() and tapping the "Back" navigation button causes the app to terminate.

Every keypressed calls love.textinput(t) with Unicode character.

You could use this as in the example below

function love.load()
    text = "Type away! -- "
    love.keyboard.setTextInput(true)
end

function love.textinput(t)
    text = text .. t
end

function love.draw()
    love.graphics.printf(text, 0, 0, love.graphics.getWidth())
end

You can also pass additional parameters to set the position and size of the onscreen keyboard:

love.keyboard.setTextInput(enable, x, y, width, height)

NOTE: You should take into account that the orientation of the keyboard is the one specified in the AndoridManifest.xml file and can´t be changed from the inside.

Physical key

The physical keys shown in the picture below trigger love.keypressed and love.keyreleased events.

keys.jpg

The KeyConstants are as follows:

  • The Return key returns "escape"
  • The Menu key returns "menu"
  • The Home key returns "home"
  • The Search key returns "search"

NOTE: This keys are not always physical and sometimes they are shown as part of the GUI (In tablets, and some cellphones), however the Menu key is not displayed in this GUI, if your game needs this key, try to provide alternatives. The keys that will most likely be available are home and escape. Issue #48

Xperia PLAY

The Xperia PLAY Smartphone has a Joystick embedded in, however this joystick has it's own API and is not supported by the SDL 2 Joystick module so it wont appear under the getJoysticks list.

Some of the buttons may trigger keypressed and keyreleased events (the Circle button returns "escape") but it may vary depending on the zone.

There are no plans on supporting this joysticks API but anyone interested in doing so might find the needed information here and here, and should also read the SDL Joystick source code