Android-compatible Keyboard Screen

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
qirien
Miko-Class Veteran
Posts: 538
Joined: Thu Jul 31, 2003 10:06 pm
Organization: Metasepia Games
Deviantart: qirien
Github: qirien
itch: qirien
Location: New Mexico, USA
Discord: qirien
Contact:

Android-compatible Keyboard Screen

#1 Post by qirien » Tue Jan 13, 2015 2:50 pm

So one of the few things that doesn't port over to android is the input function (like for typing in a name). So here's a screen you can include if you would like to automatically use an on-screen keyboard for renpy.input. (based off of shuen's android keyboard, but using screen language and styles). Here's what it looks like:
screenshot0011.png
To test it, click on "Android" in Ren'Py and then under Emulation choose Phone or Tablet. You can call it the same way you use regular renpy.input, like this:

Code: Select all

    $ his_name = renpy.input("What is your name?")
    "You picked the name [his_name]."
And here's the screen code (you might want to put it in its own file, like inputter.rpy).

Code: Select all


########################################################
#   Android keyboard input
init python:
    text_list1=[["Q","W","E","R","T","Y","U","I","O","P"],
                ["A","S","D","F","G","H","J","K","L"],
                ["Z","X","C","V","B","N","M"]]
    text_list2=[["q","w","e","r","t","y","u","i","o","p"],
                ["a","s","d","f","g","h","j","k","l"],
                ["z","x","c","v","b","n","m"]]
    input_text = ""
    text_list=text_list1
    text_group=1
    initial_caps=True
    caps_lock = False
    
style inputter_text:
    size 30
    
style inputter_button_text:
    size 30
    
style inputter_keys:
    size 40    

# Screen that Ren'Py will use for input if we are using a touch screen
screen input(prompt):
    variant "touch"    
    default caps_lock = False
    default input_text = ""
    if initial_caps:
        $ caps_lock = True
        $ initial_caps = False
    if caps_lock:
        $text_list=text_list1
    else:
        $text_list=text_list2
         
    frame:
        style_group "inputter"        
        xpos 0.5
        ypos 0.1
        xanchor 0.5
        yanchor 0
        xminimum 450

        vbox:
            $ display_text = prompt+"\n"+input_text
            text display_text
            text ""
            hbox:
                xalign 0.5
                textbutton "Backspace" action SetScreenVariable("input_text", input_text[:-1])
                textbutton "Delete all" action [ SetScreenVariable("input_text", ""),
                                                 SetScreenVariable("initial_caps", True) ]
                if (not caps_lock):
                    textbutton "Caps (On)" action SetScreenVariable("caps_lock", True)
                else:
                    textbutton "Caps (Off)" action SetScreenVariable("caps_lock", False)
            text ""
            vbox:
                xalign 0.5
                for text_row in text_list:
                    hbox:
                        xalign 0.5
                        for text_code in text_row:
                            textbutton text_code text_style "inputter_keys" action [ SetScreenVariable("input_text", input_text + text_code),
                                                          SetScreenVariable("caps_lock", False) ]
                text ""
                textbutton "Done" xalign 0.5 action Return(input_text)

Last edited by qirien on Tue Jan 13, 2015 3:54 pm, edited 1 time in total.
Finished games:
Image
Image
Image

User avatar
xavimat
Eileen-Class Veteran
Posts: 1458
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Android-compatible Keyboard Screen

#2 Post by xavimat » Tue Jan 13, 2015 3:32 pm

Thanks, qirien!
May I suggest something?
Screen language allows to define variants for touch screens, etc. You could use the same name "input" for your screen instead of "inputter", and the variable "prompt", instead of "input_header". This way, Ren'Py uses the correct screen and there is no need for the "if" statement in the script:

Code: Select all

# definition of your screen:
screen input(prompt):
    variant "touch"

    # change this line:
    #$ display_text = input_header+" "+input_text
    # to
    $ display_text = prompt+"\n"+input_text
In your script: $ his_name = renpy.input("What is your name?", "Jack", length=20) will choose the screen according to the device (the default value "Jack" and the maximum length won't be used, but maybe they could be included too).
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
qirien
Miko-Class Veteran
Posts: 538
Joined: Thu Jul 31, 2003 10:06 pm
Organization: Metasepia Games
Deviantart: qirien
Github: qirien
itch: qirien
Location: New Mexico, USA
Discord: qirien
Contact:

Re: Android-compatible Keyboard Screen

#3 Post by qirien » Tue Jan 13, 2015 3:55 pm

Thanks, xavimat! I wasn't sure how to use "variant", so I appreciate you showing me that; it's much more elegant that way. :-) I've changed the code above, also.
Finished games:
Image
Image
Image

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Android-compatible Keyboard Screen

#4 Post by PyTom » Tue Jan 13, 2015 3:57 pm

This is very nice, but I added support for the Android keyboard on new years. It's in the nightly, and will be in the next release.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
qirien
Miko-Class Veteran
Posts: 538
Joined: Thu Jul 31, 2003 10:06 pm
Organization: Metasepia Games
Deviantart: qirien
Github: qirien
itch: qirien
Location: New Mexico, USA
Discord: qirien
Contact:

Re: Android-compatible Keyboard Screen

#5 Post by qirien » Tue Jan 13, 2015 9:17 pm

Alright, you're light years ahead of me as usual. Thanks, Tom. :-)
Finished games:
Image
Image
Image

Post Reply

Who is online

Users browsing this forum: No registered users