Pushing back button/escape reverts text in Input Field - unity3d

I'm using an Input Field in a Unity 3D game. When I enter text on my Windows 10 Mobile, and push the back button to dismiss the keyboard, Unity thinks I want to clear the Input Field. This behavior is not even mentioned in the documentation and I have not found a way to override it. I'd like to make it so the user can use the back button to dismiss the keyboard without reverting the Input Field. Any suggestions? Is this just a bug with Unity?

You can see the source code of InputField here: https://bitbucket.org/Unity-Technologies/ui/src/0155c39e05ca5d7dcc97d9974256ef83bc122586/UnityEngine.UI/UI/Core/InputField.cs?at=5.2&fileviewer=file-view-default
Apparently, clearing the field on escape is made by design. look at line 980 - 984:
case KeyCode.Escape:
{
m_WasCanceled = true;
return EditState.Finish;
}
What you can try is to create your own subclass of InputField and override the function
protected EditState KeyPressed(Event evt)
Of course it is not really clean, since you'll have to copy everything that the base InputField does in this function, except for lines 980 - 984.

The function KeyPressed cant be overridden.
I just added that in my function that listens on the value changes of the inputField:
if (Input.GetKeyDown (KeyCode.Escape)) {
return;
}
And it does exactly what is necessary :)

Related

Drag button text into button on Click function: Unity Inspector

Hi hope its not too dumb a question. I'm trying to see if i could drag the text in a button onto the on Click method of the button. The drag drop doesn't work.
If not, is the only way on script? Whats the best way to get the text via script?
(The text is added via a script).
Seemingly your DialogueManager.continue method takes a string as parameter
public void continue(string text)
{
...
}
-> No! You can't simply drag in a component into a string field.
Try to make your method rather take a Text/TMP_Text component as parameter
public void continue (Text /* or TMP_Text accordingly*/ text)
{
Debug.Log(text.text);
...
}
You can do something like that. The function you are calling needs to take what you want to pass as a parameter though.
So in this example the function is taking a GameObject.
public void DialogueButtonPressed(GameObject textGameObject)
{
}
You can pass whatever type you want though. Maybe you want to pass a UI.Text component.

Unity Switch between input fields with one click

I'm looking for a way to switch between input fields without having to double click the next input field twice.
What happens now is, for instance I type in something inside the first field, and then when I try to go to another field, I need to click on the other field once(or somewhere else besides the first input field) to remove focus, and then to click again on the other field to open up the keyboard and switch the focus to that field.
What can I do to make the switch between two input fields happen in one click(when clicking on the desired input field)
Thanks!
From your problem, it seems like you are working on Android/iOS devices.You could try calling TouchScreenKeyboard.Open() when the InputField is clicked, like so:
[RequireComponent(typeof(InputField))]
public class ShowKeyboardOnClick : MonoBehaviour {
private InputField inputField;
private void Awake() {
inputField = GetComponent<InputField>();
}
// Or raycast, etc
private void OnMouseDown() {
TouchScreenKeyboard.Open(inputField.text);
}
}
Note that from the documentation, it states:
Only native iPhone, Android, and Windows Store Apps are supported.

How to remove/disable On-Keyboard Textbox when Using UIInput NGUI?

I am using NGUI's UIInput class to accept text input from user. when i start typing in text box in mobile devices. a key board appears and it has an another textbox within it self, with "OK"/"Done" button (Like a keyboard accessory view, if we're talking about iPhone).
Can i disable that text box appearing within keyboard ? Or its not even possible and i am shooting just blanks ?
From what i could gather by search for a while is, the appearance of keyboard is handled buy Unity's "TouchScreenKeyboard" class. but according to Unity Scripting reference there is nothing which could hide the textfield inside the keyboard.
Unity Scripting reference: TouchInputKeyboard
PS:- I should still be able to put input in textbox by directly typing into them, i just want an extra textbox within the key board to be removed.
TO be more clear i have attached images explaining this
This is the screen.
When i start typing in one of the textbox. a keyboard appears like the following.
as you can see the text box just above the keyboard is not the original one.
Did you try checking "Hide Input Check box" in Inspector view of that UIInput Textbox ?
private void OnGUI()
{
TouchScreenKeyboard.hideInput=true;
}
I don't know why it is, but I have had this problem as well and the "hide input" checkbox for some reason doesn't seem to do really anything other then change the keyboard text box from one line to multi line.
I did a little bit of digging and came across a quick lead that will enable that hide input check box.
This fix is Update() in UIInput.cs around 650
else if (inputType == InputType.Password)
{
TouchScreenKeyboard.hideInput = true;
kt = TouchScreenKeyboardType.Default;
val = mValue;
mSelectionStart = mSelectionEnd;
}
else
{
if(hideInput)
{
TouchScreenKeyboard.hideInput = true;
}
else
{
TouchScreenKeyboard.hideInput = false;
}
kt = (TouchScreenKeyboardType)((int)keyboardType);
val = mValue;
mSelectionStart = mSelectionEnd;
}
I added a check in the else statement

SIP prevents EF from auto-detecting object change on WP7

I have an issue trying to persist entities to the DB using code-first technique. For example of what I am doing you may look at the following MSDN Sample. The app generally works as intended except for one case.
If I have an existing entity and I bind it to a page that has a TextBox to hold the Title field and a AppBar icon to Save (similar to the 'New Task' screenshot in the above link, but with values pre-filled with existing entity with Two-Way binding), the following issue occurs. If I have the TextBox selected and I change the title and hit the save button, it updates the entity in-memory so that the full list now displays the new title. But the new title is not persisted to the DB (it does not auto-detect changes). This is weird, not just because the object in-memory has changed, but also because if I deselect the TextBox and then hit save, it will persist the changes to the DB.
I have seen other questions on SO with some change detection issues, they suggest adding this.Focus() or focusing some other element at the beginning of the save method. This does not help in my case. Unless I tap on screen to deselect the TextBox and hide the keyboard (or press Return key on the keyboard, which I bound to do this.Focus()), it won't detect the object as changed.
How can I address this? What exactly is stopping EF from detecting the object change when the keyboard is still visible?
Not sure if I follow exactly what you described but I think the problem is that the property you have bound your textbox to does not get updated until the TextChanged is fired on the textbox, and this is done first when you leave the Textbox, basically it will lose Focus if you tap somewhere else.
There is a simple workaround for this and it behaviors. By making a small behavior you can force the textbox to update the binding on each keystroke - so everything is updated while you type and keyboard is still there.
Behavior:
/// <summary>
/// Update property on every keystroke in a textbox
/// </summary>
public class UpdateTextSourceTriggerBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
this.AssociatedObject.TextChanged += OnTextBoxTextChanged;
}
void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
var bindingExpression = AssociatedObject.ReadLocalValue(TextBox.TextProperty) as BindingExpression;
if (bindingExpression != null)
{
bindingExpression.UpdateSource();
}
}
protected override void OnDetaching()
{
this.AssociatedObject.TextChanged -= OnTextBoxTextChanged;
}
}
Now just attach this behavior to your textbox like this:
<TextBox Text="{Binding YourPropertyName, Mode=TwoWay}">
<i:Interaction.Behaviors>
<UpdateTextSourceTriggerBehavior/>
</i:Interaction.Behaviors>
</TextBox>
This will keep the property on your viewmodel updated all the time, so that when you tap on save directly after typing in the textbox it will save the correct value. Hope it helps!
Cheers,
Anders

PhoneGap + iOS Prevent default scroll action begins from input text field

I've faced up with the following problem:
I have a scroll area which contains list of input text fields.
I use
ontouchmove = function(e){ e. preventDefault(); }
to prevent global scroll of the page. It works fine except cases when gesture begins from input field.
How can I prevent global scroll of the page when first touch traps to the input field?
Thanks.
I believe you want to capture the touchmove event using the addEventListener function so that the even doesn't "bubble". Try this:
/* This code prevents users from dragging the page */
var preventDefaultScroll = function(event) {
event.preventDefault();
window.scroll(0,0);
return false;
};
document.addEventListener('touchmove', preventDefaultScroll, false);
this might help
the section "MORE SPECIALIZED SOLUTION" might be what you are looking for.