- Home /
How can I release gui focus via code?
I am working on a game which has a GUI Text Field to capture chat text. The chat system works fine and it outputs what you type in after you press enter. The problem is that I can't find a way to release the focus from the Text Field and give keyboard input back to the game. This is incredibly frustrating because every time you are done chatting, you have to click on the game window to manually release focus.
I have already tried the following "fixes":
GUIUtility.keyboard = 0;
//This technically does release focus from the Text Field,
//but the game does not regain focus.
GUI.FocusControl("");
//This does the same thing as the above method.
//GUI.FocusControl cannot give the game focus back, merely change it, at least as
//far as I know.
//I've also tried messing around with enabling/disabling controls,
//the entire GUI interface, and individual GUI calls to see if it'll reset focus.
//No dice.
Does anyone know of a way to give focus back to the main game window so that the Input class can receive input without needing to click on the game window again?
Never actually worked with focusing windows, but you could try this thread http://forum.unity3d.com/threads/18109-how-to-get-GUI.Window-focus?p=123442#post123442
Answer by _met44 · Apr 07, 2014 at 03:45 PM
Hi, in case someone reads this one day, GUI.FocusControl("") works fine to remove focus of gui elements. It either got fixed or people's logic behind its use was wrong but anyways it does work. (unity 4.3.4)
ps: I use C#, maybe it's a problem with JS
Best answer. Just tested it and works perfectly, one line of code...
Answer by gooncorp · Apr 10, 2013 at 12:18 AM
whats the confusion? the focus/unfocus question is a simple one, one that everyone who writes code should encounter, and one that everyone will encounter when building a video game. thanks peladovii for the answer. :)
Answer by peladovii · Mar 16, 2013 at 12:16 AM
I dont know if you still need the answer but you can focus any gui element as long as you have previously defined a name for it:
GUI.SetNextControlName ("Input");
submittedName = GUI.TextField (Rect (x, y, a, b), submittedName, 12);
GUI.FocusControl ("Input");
As my original post shows, I've already tried that method. $$anonymous$$y issue is giving input focus back to the game itself, not a defined GUI element. If the game itself was somehow registered as a named GUI element, then that method would be perfect, but sadly it doesn't seem to work that way.
Oh, yes, I know what you mean, I had a similar issue but decided to just disable the textfield, because I found no other solution...
GUI.skin = submissionSkin;
GUI.SetNextControlName ("Input");
if(showChat) {
submittedName = GUI.TextField (Rect (Screen.width / 2 - inputSize.x / 2, Screen.height / 2 - inputSize.y / 2, inputSize.x, inputSize.y), submittedName, 12);
GUI.FocusControl ("Input");
}
//IF ENTER
if (Event.current.type == EventType.$$anonymous$$eyDown && Event.current.character == '\n' && GUI.GetNameOfFocusedControl () == "Input" && donePlaying) {
Game$$anonymous$$aster.playerName = submittedName;
showChat = false;
Answer by Matthew A · Mar 16, 2013 at 12:41 AM
I had the same issue a while ago. I solved it like this (offscreen control for focussing on):
static var hackControlName : String = "hackery129578835432342";
// call this function in OnGUI before calling TextField (only needs to be called once)
static function FocusHack() {
// fake control used for unfocussing things, since we can't call
// FocusControl with no arguments and an empty string doesn't work
GUI.SetNextControlName(hackControlName);
GUI.Button(Rect(-10000,-10000, 0,0), GUIContent.none);
}
Your answer
Follow this Question
Related Questions
speech / chat bubbles above character or game object 0 Answers
Focus on InputField upon key press? 3 Answers
How to prevent ignored input after typing in a GUI? 0 Answers
C# GUI Button 2 Answers