I have a GUI.TextField that is for time input.
The format is HH:mm for hours and minutes.
I have been searching for and wrestling with a way to control user input so it is only in this format.
I have looked at TextEditor as well as Event.current.character and both seem to have the elements of what I need but I am just not seeing through to the way to make it work.
Anyone know of a simple method or can point me to a source I haven’t encountered.
I am willing to bet that I am making this problem much more difficult than it need be.
Much thanks in advance.
I went with a date entry routine first and found the answer.
The time entry will just be a modification of this.
Appologies if the format of this code is weird.
dosString = GUI.TextField (new Rect(25, 115, 100, 24), dosString, 10, textFieldStyle); // MM/dd/yyyy
chr = Event.current.character;
if (GUI.GetNameOfFocusedControl() == “dateOfStopString”){
print ("TOP charCount = " + charCount);
print (“TOP dosString = " + dosString);
print (“TOP chr = " + chr.ToString());
// Establish control of cursor with the following line
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
if(chr == ‘0’ || chr == ‘1’ || chr == ‘2’ || chr == ‘3’ || chr == ‘4’ || chr == ‘5’ || chr == ‘6’ || chr == ‘7’ || chr == ‘8’ || chr == ‘9’){
if (charCount>9) charCount = 0; // reset count increment to zero
if(charCount == 2 || charCount == 5){
dosStringArray[charCount] = “/”;
charCount++;
}
dosStringArray[charCount] = chr.ToString();
//print (”###################################dosStringArray.length = " + dosStringArray.Length);
//print (”@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@charCount = " + charCount+ " dosString = " + dosString);
charCount++;
editor.selectPos = charCount-1;
editor.pos = charCount-1;
Event.current.character = ‘k’; // SET THIS TO PREVENT FOCUS DROPPING THROUGH NUMBER TEST ABOVE
chr = ‘k’;
}
else{
Event.current.character = ‘\0’;
dosString = “”;
for(int x=0; x<dosStringArray.Length;x++){
dosString = dosString + dosStringArray;
}
editor.selectPos = charCount; // setting cursor postision
editor.pos = charCount; // setting cursor postision
}
Event.current.character = ‘k’; // SET THIS TO PREVENT FOCUS DROPPING THROUGH NUMBER TEST ABOVE
if (charCount == 10){
print ("charCount = 10 dosString = " + dosString);
dosString = “”;
for(int x=0; x<dosStringArray.Length;x++){
dosString = dosString + dosStringArray[x];
print ("dosStringArray["+x+"] = " + dosStringArray[x]);
}
print ("charCount = 10 AFTER FOR dosString = " + dosString);
// $$$$$$$$$$$!!!!!!!!!!!!!!!! THIS TEST WORKS !!!!!!!!!!!!!!!!!!!!!!!$$$$$$$$$$$$$$$$$$$$$$$
Regex reg = new Regex (@"^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/(19|20)\d\d$");
Match m = reg.Match(dosString);
if (m.Success){
charCount = 0;
print (" {}{}{}{}{}{}{}{}{ MATCH {{}}{{{{}{}{} = " + dosString);
}
else{
print (" {}{}{}{}{}{}{}{}{ NOOOOOO {{}}{{{{}{}{} = " + dosString);
dosString = "MM/dd/yyyy";
dosStringArray = new string[]{"M", "M", "/", "d", "d", "/", "y", "y", "y", "y"};
charCount = 0;
invalidDate = true;
}
}
} // END Date Of Stop routine