- Home /
Detecting when Enter Key is Pressed IntField
Hi, I think this question has been asked before but couldn't find answers again. I'm displaying list in a custom inspector and but would like to be able to copy list values when changing the list size. Unfortunately I can't see that it's going to be possible without changing the size only when the enter/return key is pressed. Is there a way to detect when the enter key is pressed in a IntField? Heres a code snippet.
_showTags = EditorGUILayout.Foldout(_showTags, "Target Tags");
if (_showTags)
{
_tagsArraySize = _target.tags.Count;
_tagsArraySize = EditorGUILayout.IntField("Tag Count", _tagsArraySize);
if (GUI.changed)
{
if (_tagsArraySize != _target.tags.Count)
{
//implement copying old values here if new arraysize was smaller than the last
_tagsTemp = new List<String>(_tagsArraySize);
for (int tagIndex = 0; tagIndex < _tagsArraySize; tagIndex++)
{
_tagsTemp.Add("");
}
_target.tags = new List<String>(_tagsTemp);
EditorUtility.SetDirty(_target);
}
}
for(int tagIndex = 0; tagIndex < _target.tags.Count; tagIndex++)
{
_target.tags[tagIndex] = EditorGUILayout.TextField(tagIndex.ToString(), _target.tags[tagIndex]);
EditorUtility.SetDirty(_target);
}
Answer by Bunny83 · Jul 09, 2011 at 05:41 PM
Take a look at the Event class. It contains all information on the current event that causes OnGUI / OnInspectorGUI or similar event handlers to execute.
Check if Event.current.type is EventType.KeyDown and Event.current.keyCode is KeyCode.Return .
Thanks for the links. I've tried a few things but I can't seem to get it to work. I thought I would have been able to just replace if(GUI.changed) with something like if(Event.current.keyCode == $$anonymous$$eycode.Return) but that doesn't seem to work.
Answer by Blankzz · Jul 09, 2011 at 08:30 PM
Ok so I've got it to work but there's one problem. I seem to be getting an exception error and I cant seem to make it out. Heres the new code and the error. Can anybody make it out?
if (_showTags)
{
_tagsArraySize = EditorGUILayout.IntField("Tag Count", _tagsArraySize);
Event e = Event.current;
if (e.type != null && e.isKey== true && e.keyCode==KeyCode.Return)
{
if (_tagsArraySize != _target.tags.Count)
{
//implement copying old values here if new arraysize was smaller than the last
_tagsTemp = new List<String>(_tagsArraySize);
for (int tagIndex = 0; tagIndex < _tagsArraySize; tagIndex++)
{
_tagsTemp.Add("");
}
_target.tags = new List<String>(_tagsTemp);
EditorUtility.SetDirty(_target);
}
}
for(int tagIndex = 0; tagIndex < _target.tags.Count; tagIndex++)
{
_target.tags[tagIndex] = EditorGUILayout.TextField(tagIndex.ToString(), _target.tags[tagIndex]);
EditorUtility.SetDirty(_target);
}
}
Error
ArgumentException: GUILayout: Mismatched LayoutGroup.KeyUp UnityEngine.GUILayoutUtility.BeginLayoutGroup (UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options, System.Type LayoutType) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUILayoutUtility.cs:153) UnityEngine.GUILayout.BeginVertical (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUILayout.cs:192) UnityEngine.GUILayout.BeginVertical (UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUILayout.cs:184) UnityEditor.MaterialEditor.OnInspectorGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/Inspector/MaterialEditor.cs:107) UnityEditor.InspectorWindow.OnGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/Inspector/InspectorWindow.cs:364) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
If I comment out the line "_target.tags = new List(_tagsTemp);" I no longer get the error but obviously doesn't work how it should. At least I know the problems lies after assigning the new list.
Your answer

Follow this Question
Related Questions
Inspector button for custom class 1 Answer
Custom Inspector - rearranging control columns. 0 Answers
Custom Editor problems 2 Answers
Custom inspector: uniformly display all instances of a custom class 2 Answers
WebPlayer Custom Params Inspector 0 Answers