Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How Does Touchscreen.Status Work?

Discussion in 'iOS and tvOS' started by John-B, Dec 9, 2019.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    I need to call a function when the keyboard is dismissed, and this (in Update) does not work:

    Code (CSharp):
    1. if (TouchScreenKeyboard.Status.Visible && TouchScreenKeyboard.Status.Done) {
    2.      OnKeyboardDismiss();
    3.      return;
    4.  }
    5.  
    I don't understand how TouchScreenKeyboard.Status is supposed to be used, and there is no sample code in the scripting docs. How do I detect when the keyboard is dismissed?
     
    gareth_untether likes this.
  2. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,259
    I found something like this in some sample code, but it doesn't work either. Looks like there's no way to get a reference to the current keyboard without opening it. If I do an Open on Start, it shows the keyboard, which is bad. But it doesn't matter where I get the reference, it never returns Done or Canceled. Any help, or some more current sample code, would be greatly appreciated.

    Code (CSharp):
    1. function Update () {
    2.     TouchScreenKeyboard keyboard;
    3.  
    4.     theFocus = EventSystem.current.currentSelectedGameObject;
    5.  
    6.     if (theFocus == null)
    7.         return;
    8.        
    9.     keyboard = TouchScreenKeyboard.Open("");
    10.            
    11.     if (keyboard.status == TouchScreenKeyboard.Status.Done || keyboard.status == TouchScreenKeyboard.Status.Canceled) {
    12.         OnKeyboardDismiss();
    13.         return;
    14.     }
    15.  
    I'm updating from an older project that used EZGUI where it correctly identified when the keyboard was dismissed. I'm trying to do the equivalent with TMPro and UGUI. I have some text input in a panel near the bottom of the screen, and that panel slides up when the keyboard appears. I need to know when the keyboard is dismissed so I can slide the panel back down to be able to see the top of it.
     
    Last edited: Dec 9, 2019
  3. gareth_untether

    gareth_untether

    Joined:
    Jan 5, 2018
    Posts:
    69
    Did you have any luck getting an instance of the touchscreenkeyboard?
    I tried the following, but still not able to get data from touchScreenKeyboard.status

    Code (CSharp):
    1. TouchScreenKeyboard touchScreenKeyboard;
    2.  
    3. Awake()
    4. {
    5. touchScreenKeyboard = inputField.touchScreenKeyboard;
    6. }
     
  4. mullenator

    mullenator

    Joined:
    Jun 30, 2022
    Posts:
    3
    I'm having a similar problem with the touchScreenKeyboard :(. I wanted to check if the currently selected input field's keyboard is active so that I can turn on the keyboard's caps lock key when it first pulls up. However, when I try:


    Code (CSharp):
    1. if (inputField.touchScreenKeyboard.active)
    2.         {
    3.             Debug.Log("Woah! Your keyboard is showing!");
    4.             // Insert other logic here
    5.         }
    I get a null reference exception. I keep googling "inputField.touchScreenKeyboard examples", but I can't find any examples on how you are supposed to initialize it.