Jump to content

How to suspend control when in a text entry field?


Recommended Posts

I'm pretty sure this is a KSP bug.  I had a request from a user:

Quote

Right now if I'm in editor and want to rename my vessel to something long (e.g. Cool Massive Interplanetary Exploration Rocketship) it opens and sometimes then closes a half dozen windows and changes my symmetry count/changes mode/moves the camera/etc. This mod was doing the same while I was in-flight testing the identifiers. Don't even remember what I typed, but deactivated SASRCS, turned the ship a little, and switched off my persistent camera focus mod ("o") all in the span of less than a seconds while typing the name in the box.

 

I'm wondering if there is any way to generalize this, so that a patch/mod can be written to fix this issue for all mods.

any ideas, suggestions?

Link to comment
Share on other sites

This again? I discontinued my ControlLock mod when the new GUI system hit because this was supposed to no longer be an issue.

I really haven't looked at the new system yet, what do we have for hooks/events on text boxes?

(On mobile, best I can do at the moment is look up the API docs once I know the Class Name.)

Will try and poke at this when I get home, the new GUI will require coding something from scratch probably though.

D.

Edited by Diazo
Link to comment
Share on other sites

It's easy for the new UI. 

The Text Input Field (and I assume the Text Mesh Pro input field) has an isFocused flag that is true whenever the input field is accepting text input.

So you can attach an OnPointerClick event to the field to activate the control lock, then monitor the focus flag to check for when the mouse is clicked somewhere else. You can also deactivate the control lock whenever the text input is submitted, either with another button, or by setting up "Enter" to submit the text.

The latest version of SCANsat uses this method and it seems to work fine:

https://github.com/S-C-A-N/SCANsat/blob/dev/SCANsat.Unity/Unity/SCAN_SettingsData.cs#L120-L129

https://github.com/S-C-A-N/SCANsat/blob/dev/SCANsat.Unity/Unity/SCAN_SettingsData.cs#L30-L40

https://github.com/S-C-A-N/SCANsat/blob/dev/SCANsat/SCAN_Unity/SCAN_UI_Settings.cs#L397-L409

Link to comment
Share on other sites

1 hour ago, Diazo said:

This again? I discontinued my ControlLock mod when the new GUI system hit because this was supposed to no longer be an issue.

I really haven't looked at the new system yet, what do we have for hooks/events on text boxes?

(On mobile, best I can do at the moment is look up the API docs once I know the Class Name.)

Will try and poke at this when I get home, the new GUI will require coding something from scratch probably though.

D.

Yes, unfortunately.

 

7 minutes ago, DMagic said:

It's easy for the new UI. 

The Text Input Field (and I assume the Text Mesh Pro input field) has an isFocused flag that is true whenever the input field is accepting text input.

So you can attach an OnPointerClick event to the field to activate the control lock, then monitor the focus flag to check for when the mouse is clicked somewhere else. You can also deactivate the control lock whenever the text input is submitted, either with another button, or by setting up "Enter" to submit the text.

The latest version of SCANsat uses this method and it seems to work fine:

https://github.com/S-C-A-N/SCANsat/blob/dev/SCANsat.Unity/Unity/SCAN_SettingsData.cs#L120-L129

https://github.com/S-C-A-N/SCANsat/blob/dev/SCANsat.Unity/Unity/SCAN_SettingsData.cs#L30-L40

https://github.com/S-C-A-N/SCANsat/blob/dev/SCANsat/SCAN_Unity/SCAN_UI_Settings.cs#L397-L409

What I'm wondering is, is it possible to write a single (new) mod which would take care of this for all  mods?

And for me, right now, I have the problem in a dialog box:

PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f),
                    new Vector2(0.5f, 0.5f),
                    new MultiOptionDialog("",
                        "Janitor's Toolbar",
                        HighLogic.UISkin,
                        new Rect(0.5f, 0.5f, 150f, 60f),
                        new DialogGUIFlexibleSpace(),
                        new DialogGUIVerticalLayout(
                            new DialogGUIFlexibleSpace(),
                            new DialogGUIButton("Confirm blacklisting button",
                                delegate
                                {
                                    string s = buttonId(ClickedButton);
                                    Log.Info("blacklistIconHash: " + s);
                                    blacklistIcons.Add(s, s);
                                    saveBlacklistData(blacklistIcons);
                                }, 140.0f, 30.0f, true),
                           
                            new DialogGUIButton("Cancel", () => { }, 140.0f, 30.0f, true)
                            )),
                    false,
                    HighLogic.UISkin);

 

Link to comment
Share on other sites

Well you could do what I said, but just in a generic way.

Find every text input field (regular Unity or TMP), add a OnPointerClick event handler, and turn on control locks whenever an input field is activated, then disable the locks when it loses focus. Of course this would be difficult to handle because there is probably no good way to detect when another mod or KSP opens up any given window.

And it's not going to do anything to fix OnGUI input fields, and there probably isn't any way to handle them from outside the mod in question. It also probably won't be possible to interrupt other mods that use keyboard shortcuts.

I haven't tried using the PopupDialog for input fields, but I assume it just creates a TMP input field, so you could probably manually add a listener to that object when you create the dialog.

Link to comment
Share on other sites

I've been thinking on this and I'm not sure how to proceed.

The issue is that there is no way to lock out the keyboard, mods check the raw Unity keyDown method and there is no way to disable that. (At least mine do, I've never heard of another way to detect keypresses.)

Even unbinding the key doesn't stop this, every mod that detects keystrokes needs to be locked out individually in its own code.

I've actually been worried about this previously, http://forum.kerbalspaceprogram.com/index.php?/topic/151312-preventing-keystroke-fallthrough-on-text-field-usage-between-different-modsinputlockmanager/ but nothing really came of it at the time.

I still have no better idea than the community agreeing to all use the ControlInputTypes.KeyboardInput as the agreed on method of telling each other if a textbox has focus.

To make this work would require the entire community to agree that:

1) Any mod that has a text box must set InputLockManager.AddLock(ControlInputTypes.KeyboardInput) to true when a textbox gains focus and removes the lock when the text box loses focus.

2) Any mod that detects key presses must gate the keypress behind a check against InputLockManger and if the KeyboardInput lock is set, ignore the keypress because a text box has focus.

But even a community of this size getting 100% compliance with this is next to impossible.

 

But the only way for this to work in a single mod that takes care of it is to find some way in Unity to disable the Key(), KeyDown() and KeyUp() methods so they don't return values when keyboard keys are pressed and I have no clue how to go about that.

 

So, I have no good answers to how to do this.

D.

Link to comment
Share on other sites

6 hours ago, Diazo said:

The issue is that there is no way to lock out the keyboard, mods check the raw Unity keyDown method and there is no way to disable that. (At least mine do, I've never heard of another way to detect keypresses.)

Never mind mods, stock game even does this... like try moving the arrow keys to edit while typing into the description box on top of the VAB/SPH and the camera will move (and other stuff).  Drives me nuts.

 

6 hours ago, Diazo said:

But the only way for this to work in a single mod that takes care of it is to find some way in Unity to disable the Key(), KeyDown() and KeyUp() methods so they don't return values when keyboard keys are pressed and I have no clue how to go about that.

I expect you'd have to preempt the key detection at the OS level, which is of course not a cross-platform solution, and also a huge headache given Unity's design.

Even if there were some way for Unity to prioritise and cancel events, all mods would just move towards asking for priority 1 anyhow, because mod makers would get tired of bugs due to other mods out-prioritising them.  :)  So cooperation would be needed one way or another.

Link to comment
Share on other sites

Well, assuming that Squad added checks on all their inputs to ignore them, there is some hope using InputLockManger.

Notably, the TextMesh Pro that KSP now uses does in fact have OnFocus and OnFocusLost events that could be hooked into that (in theory) you could hook an event to so that all text input fields in the game automatically set InputLockManager(ControlLockType.KeyboardInput) which should cause stock commands to not execute.

They might be called OnSelect and OnDeselect in the version KSP actually uses? http://digitalnativestudios.com/forum/index.php?topic=111.msg9409#msg9409

That would still require every mod maker to gate their own keychecks behind a check against InputLockManager still, but it's part of a solution?

Out of time for tonight, see if sleeping on it helps.

D.

Edited by Diazo
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...