This
did -r $dname 122 Caps lock ON
did -a $dname 122 Caps lock ON
Could be
did -ra $dname 122 Caps lock ON
For the backspace part you could use something like below.
$remove($did(1),$right($did(1),1))
Example: $remove(testing,$right(testing,1)) = testin
That way it will delete the last letter of the text in the edit box each time it is hit as so long as you start at the end of the text line.