- Home /
Access to GUI Keyboard Focus
Hey guys,
So I asked a pretty in depth question yesterday, which you can view here. Haven't gotten any hits as of yet, so I thought I'd simplify the question to something I can work with for now.
Question Is there a way to gain script access to which button has keyboard focus?
I can apply a GUI skin to a button that has the functionality of changing text color and background texture while a button has keyboard focus, but I'd like to be able to animate/scale a button while it's focused, not when it's pushed. Anyone know of a way to do this?
EDIT: Didn't think to provide code so you all can see how I do this -.- Like I said to David, I utilize a string array in which I add menu items in the inspector, after which I have a "for" loop that creates a button for every array item. Here's the code:
function OnGUI () {
GUI.BeginGroup (Rect (0, Screen.height / 4, Screen.width / 2, Screen.height / 2)); // Begin group to nest the following GUI elements in
for (i = 0; i < menuOptions.Length; i++) { // For every menu option we have...
GUI.SetNextControlName (menuOptions[i]); // Assign a control name
GUI.Button (Rect (0, 0 + (((Screen.height / 18) * 1.25) * i), Screen.width / 2.5, Screen.height / 18), menuOptions[i]); // Create a scalable button
}
GUI.EndGroup (); // End the group
GUI.FocusControl (menuOptions[selectedIndex]); // Set focus control to the selected menu option
}
NOTE The "selectedIndex" variable within "GUI.FocusControl" is an integer that is determined based upon the up/down selection keys. For example - if selectedIndex = 0, the first button is highlighted, etc. Am I utilizing FocusControl & SetNextControl name properly?
Thanks! Clopay
Answer by Davidovich · Jan 04, 2013 at 09:41 PM
You can use GUIUtility.keyboardControl to get the ID of the focussed element, then compare that against the control ID.
Example code in C# (Unityscript version is almost identical)
GUIContent content = new GUIContent("I'm a button!");
int controlId = GUIUtility.GetControlID(content, FocusType.Keyboard);
if (GUIUtility.keyboardControl == controlId) {
Debug.Log("It's got focus - " + controlId);
}
if (GUI.Button(new Rect(10,10,100,30), content)) {
Debug.Log("Clicked the control button");
}
Thanks for the response, $$anonymous$$! I do have the ability to access WHICH menu item is selected, but I can't figure out how to gain direct access to the actual item, if that makes sense. I utilize a string array for the menu items, and then use a for-loop to create the menu OnGUI. I'll edit my post to provide the code for how I construct the menu.
It does make sense, but to my knowledge there isn't any way to access the actual button. You can access which button has focus, but not the button itself.
I may be wrong, but the way I've found to work with the GUI functions is to largely think of them as draw functions - you set everything up before calling GUI.Button and based on whether the button you're about to draw is the focussed one you can change the data you pass into the function.
As an example (warning: possibly stupid code)
function OnGUI () { GUI.BeginGroup (Rect (0, Screen.height / 4, Screen.width / 2, Screen.height / 2));
for (i = 0; i < menuOptions.Length; i++) {
// Set up all the same stuff as before
GUI.SetNextControlName (menuOptions[i]);
// Create the settings for the defaut setup
var buttonRect = Rect(0, 0 + (((Screen.height / 18) * 1.25) * i), Screen.width / 2.5, Screen.height / 18);
// Deter$$anonymous$$e whether this button has focus. Do this bit however you want.
var content = GUIContent(menuOptions[i]);
var controlId = GUIUtility.GetControlID(content, FocusType.$$anonymous$$eyboard);
if (GUIUtility.keyboardControl == controlId) {
// if this button has focus, change whatever needs to be changed.
// In this case, just make it bigger, but you could do whatever.
buttonRect = Rect((buttonRect.x - 10), (buttonRect.y - 5), (buttonRect.width + 20), (buttonRect.height + 10));
}
// $$anonymous$$now when the button is clicked.
if (GUI.Button (buttonRect, menuOptions[i])) {
// Button interactions go here...
}
}
GUI.EndGroup ();
GUI.FocusControl (menuOptions[selectedIndex]);
}
$$anonymous$$ - Brilliant! Thank you so much. This brings a huge amount of relief to me. I never thought of passing your button specs BEFORE you drew the button - this works perfectly for keyboard focus as desired - the buttons scale up nicely on focus, and upon pressing I can pass even more functions. All that's left is styling.
Alright, so the code is working great - upon focusing a button, I can get it to scale up in width a tad bit, which is just what I was looking for; however, it seems that the code somewhat nullifies the GUISkin hover settings. I'd like to see if I can ALSO change the font text/button background of the focused button, as I had before.
It looks like I can have one or the other - and that's based upon where I place the "SetNextControlName" line. If I place it before all of the established variables in OnGUI, I get the button scale. If I place it after all of the established variables, I'll get the button text/texture changes. Do you know of a solution with which I can attain both?
Your answer
Follow this Question
Related Questions
Changing Font, Background/Style of GUI Buttons 1 Answer
Don't get how to do rounded edges at GUI! 0 Answers
camera zoom using gui button 2 Answers
GUI.Button not responding when drawn in a Rect. 2 Answers
Main menu help needed (C#) 2 Answers