How do I get notified when a user presses the delete button on a textfield?
4 Answers
Accepted Answer
The ASCII code for the delete key is 127. Therefore the following should allow you to detect that the delete key was pressed:
If (textField.charCodeAt(0) == 127) { // your code here; }
I don't know of a listener specifically for this, but there is a way to do it.
Just monitor the value of the TextField, if it becomes empty, then the user hit the clear button.
if (textField.value == "") { // execute something; }
Thanks antonio. Unfortunately, I need to know if they hit the delete button after the textfield has been cleared.
My solution, not too pretty, but it works for Ti.UI.TextField:
- After create the TextField (tf), add a variable called _prevValue
- Listen to event 'change' for tf
- Initialize _pervValue with the default value
- In the listener function compare the length of tf().getValue (lenA) and tf._prevValue (lenB). lenA == lenB + 1 implies <del> key was pressed.
- This method, however, will fail if user perform cut-and-paste to tf
Your Answer
Think you can help? Login to answer this question!