Animated keyboard based on source text layer
Copy link to clipboard
Copied
Hi,
I am trying to expand on a type on script I recently learned using the input of the text to type on based on a slider controller. I have a comp with a phone keyboard added, all of the keypads and letters have their own comp. I would like to drive the opacity on each key as a letter is typed. I have some coding experience in other languages and though a switch or dictionary would be the best way but I am not sure if these are implemented in the latest JavaScript version in AE. How would I take the source.text.value and use that to trigger opacity animation on the appropriate keys. I am solely focused on Alpha characters, spaces and special characters will use one of 3 other comps in the layer. I have a picture of my script that drives the text layer animation. I have a drop down menu with 26 items(named A-Z) on a null. I want the toUpper() method applied to the result of the source text.substring value and then that value used to find the appropriate comp and apply a ease in out on the opacity of the comp.
Copy link to clipboard
Copied
If you have a text animator added to a text layer and you set the reference to characters you could add an if statement to each pre-comp of a typewriter key that looks for the Character under the character offset and changes the opacity from zero to 100.
Let's say that your 4th character was "a" then when the slider hit 4, 'a' would appear. The if statement would look something like this:
src=thisComp.layer("Control").effect("Character Slider")("Slider");
if (src== 4){
tMax = 100;
}
else{
tMax = 0;
}
You would have a different src== number for each key and the keys would have to correspond to the letters of the alphabet.
There may be a way to look at the source text layer and the character index for the animator slider and determine if the character equals a specific letter, but I would have to experiment with that. It's an interesting idea. I hope this gets you started. If I have a few minutes I'll play with the idea later this afternoon. I'm just not sure that you can read the index of a character or search a text string for a specific character.
Copy link to clipboard
Copied
Would be as simple as a basic conditional. If you really just have a flat list from your dropdown or a numerical value from a slider a case()switch{} statement would be more efficient than endless if()else{}. Just make sure the values have proper rounding for the matching to work. Building the string from the source text should not be an issue, either as you basically just iterate through it and check at each letter's index. JS treats strings and arrays the same, so there is not much to do on this end.
Mylenium
Copy link to clipboard
Copied
I got it figured out. It's pretty simple if you use a text animator set to index and a simple expression that just compares the charAt() value to a letter in a simple if statement.
I used a free typewriter key font, converted the characters to a shape layer to quickly set up a virtual keyboard, then I added a Source Text layer, set some type, then added an Opacity character animator, set the Range Selector to Index and Characters, then used the Range Selector/Start value to drive a color change in the shape layers fill color to get this:
Here is the expression for the letter "a":
src=thisComp.layer("Source Text Layer");
srcTxt = src.text.sourceText;
chrIndex = src.text.animator("Animator 1").selector("Range Selector 1").start;
smpl = srcTxt.charAt(chrIndex - 1);
if (smpl == "a" || smpl == "A"){
clr = [.9, .9, 0, 1];
}
else{
clr = [.6, .6, .6, 1];
}
I tried it on opacity first, but I liked the color change better. Then I saved it as an animation preset. Any time I want an animated keyboard all I have to do is name the text layer "Source Text Layer" and add a text animator set to index.
The whole thing took me less than 20 minutes. If you want to fiddle with the project file, here it is.
I'll probably refine the animation preset I created from this project with some improvement in the code and create a full keyboard and a fade-in fade-out overlap, but that kind of coding will take me a while.

