Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by CanisLupus · Jul 12, 2014 at 12:11 PM · windows store appwindows 8touch screenmetro

Virtual keyboard in Windows Store Apps (Metro) via the TouchScreenKeyboard class

(This isn't a real "question", as I intend to answer it myself and leave a record for people in need of this.)

How do I use the TouchScreenKeyboard class in Windows Store Apps, in order to show the virtual keyboard on touch devices like Microsoft's Surface? Furthermore, can I test it if I don't have a touch device?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by CanisLupus · Jul 12, 2014 at 12:11 PM

Important Update: According to the release notes, Unity 4.5.3 added support for TouchScreenKeyboard.text in Windows Store Apps, and now shows the virtual keyboard without the need to set keyboard.active = true. This essentially makes the following paragraphs and example usage kind of obsolete, as you should now be able to use the virtual keyboard like in other platforms :) The testing paragraph at the end is still useful, though.

Note: this is about Windows Store Apps; i.e. Metro apps running in desktop Windows 8 or 8.1. In Windows Phone you should be able to use keyboard.text as normal.


As of Unity 4.5, TouchScreenKeyboard works in Windows Store Apps, making it possible to programatically open the virtual keyboard in Metro without the rather problematic solution of including an overlayed XAML text box.

Contrasting with other platforms, you must set keyboard.active = true after calling TouchScreenKeyboard.Open, or the keyboard will not appear. You also can't use the usual keyboard.text to set/get the keyboard text. Instead, you must rely on Unity's Input.inputString, which contains characters written this frame (the backspace key also generates a character, '\b', which you have to detect to delete characters).

The following C# script exemplifies how to use the keyboard in Windows Store Apps. For other platforms (code not included) you should use keyboard.text as normal, and call with an initial text instead of null (I use null because the value is ignored in Metro anyway).

Place this script on an object and drag a TextMesh over the TextObject reference in Unity's Inspector. Press the GUI's "Open Keyboard" button to open the keyboard (will only work on touch devices!). It closes when you write with your physical keyboard or touch anywhere outside the virtual keyboard overlay.

 using UnityEngine;

 public class VirtualKeyboardTester : MonoBehaviour
 {
     // drag an object with a TextMesh here, in Unity's inspector
     public TextMesh TextObject;

     private TouchScreenKeyboard keyboard;
     private string text = "Starting text!";

     void OnGUI()
     {
         // "Open" has several arguments. If you want special functionality, see
         // http://docs.unity3d.com/ScriptReference/TouchScreenKeyboard.Open.html

         if (GUI.Button(new Rect(10, 10, 300, 50), "Open Keyboard"))
         {
             if (keyboard == null) {
                 keyboard = TouchScreenKeyboard.Open(null);
             }
             keyboard.active = true;
         }
     }

     void Update()
     {
         // for each character inserted this frame
         foreach (char c in Input.inputString)
         {
             if (c == '\b' && text.Length > 0)
             {
                 // backspace: remove last char
                 text = text.Substring(0, text.Length - 1);
             }
             else if (c == '\n' || c == '\r')
             {
                 // New line ("Enter" was pressed).
                 // Possibly handle it as "confirm", or do "text += c" for multiline text.
             }
             else
             {
                 // add any other char to the string
                 text += c;
             }
         }

         TextObject.text = text;
     }
 }


If you want to display the text somewhere other than a TextMesh (such as GUI labels), adapt TextObject.text = text. The for cycle should remain in the Update method.

For those that don't have a Microsoft Surface tablet, beware that the virtual keyboard will only open if Windows recognizes a touch screen (unless I'm mistaken and there are other conditions). This is a problem for people without touch screen devices. However, you can simulate this in Visual Studio by running the app in the Simulator (instead of "Local Machine") and then selecting "Basic touch mode" in the simulator icons on the right (below "Mouse mode").

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meltdown · Aug 13, 2014 at 02:29 AM 0
Share

Thanks for the useful info! Did you have any issues when after calling keyboard.active = true, that using NGUI the textbox loses focus? I've tried to set focus back but it doesn't.

avatar image CanisLupus · Aug 14, 2014 at 06:09 PM 0
Share

You're welcome! ;) Do you mean that trying to use the virtual keyboard will remove focus from NGUI's UIInput (thus not allowing you to write in the box)? If so, I didn't have that problem, no. But I should add that I only have NGUI 2.7.0 (the free version), and I manually added support for $$anonymous$$etro's virtual keyboard in UIInput. (By the way, I only saw your comment by accident. For some reason I'm not being notified of comments on answers(?))

avatar image chucky-w · Nov 11, 2014 at 05:20 PM 0
Share

Hi, thanks for this info. I've used it to implement textbox in my WP8.1 app. It works fine, but I am still having one particular problem: the keyboard keeps writing capital letters only. I can even see how keyboard changes to smallcaps after pressing a letter and in a second blinks back to capitals. Do you have this problem too, or have I something bad?

I used basicaly your code, only my reading loop is not in Update() but in Coroutine with yield return null;

Well and to be my question complete, I am using Unity 4.5.5f1 and keyboard.text still is not working.

avatar image CanisLupus · Nov 11, 2014 at 10:44 PM 0
Share

Well, as of today I still haven't had the opportunity to develop for Windows Phone (WP) :) Supposedly, keyboard.text works in every platform that is not Windows Store Apps (i.e. desktop Windows 8 and 8.1), and should work even there after Unity 4.5.3 (I still haven't tested that, though). What I mean is that this post wasn't targeting Windows Phone, so I'm not sure of what the problem might be. I can tell you that I haven't had any problems with caps in other platforms. $$anonymous$$aybe you should create a question and post a bit of your code to see if anybody knows more about it than I do :)

avatar image CanisLupus · Nov 29, 2014 at 10:11 AM 0
Share

Hey @chucky-w, check the bottom of the release notes of Unity 4.6 here. Apparently, they fixed your problem with capital letters :) (just in case you haven't downloaded that version yet).

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Handling the lack of System.IO classes in Windows Store Apps (Windows 8 Metro apps) 2 Answers

Game not Working in Windows 8 but works in Windows 8.1 0 Answers

[Winrt] Hashtable & Arraylist compilation error 1 Answer

XDocument in windows store apps usage. 2 Answers

Windows Store 8 Webview 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges