Return to Snippet

Revision: 53279
at November 18, 2011 21:45 by adrianparr


Updated Code
// This code assumes you have an input textfield on the stage 
// called 'inputTxt' and two movieclips called 'onInsertBtnClick' and 'onBackspaceBtnClick'.
// Here we are just inserting incrementing number as an example.

import flash.events.MouseEvent;
import flash.text.TextField;

var num:int = 1;

insertBtn.addEventListener(MouseEvent.CLICK, onInsertBtnClick);
insertBtn.buttonMode = true;
backspaceBtn.addEventListener(MouseEvent.CLICK, onBackspaceBtnClick);
backspaceBtn.buttonMode = true;

function onInsertBtnClick(e:MouseEvent):void {
	var charToInsert:String = String(num);
	var tf:TextField = inputTxt;
	var initialText:String = tf.text;
	var ci:int = tf.caretIndex;
	var newText:String = initialText.substring(0, ci) + charToInsert + initialText.substring(ci, initialText.length)
	tf.text = newText;
	tf.setSelection(ci+(charToInsert.length), ci+(charToInsert.length));
	num++;
	stage.focus = tf;
}

function onBackspaceBtnClick(e:MouseEvent):void {
	var tf:TextField = inputTxt;
	var initialText:String = tf.text;
	var ci:int = tf.caretIndex;
	if (tf.text != "") {
		var newText:String = initialText.substring(0, ci-1) + initialText.substring(ci, initialText.length);
		tf.text = newText;
		tf.setSelection(ci-1, ci-1);
	}
	stage.focus = tf;
}

Revision: 53278
at November 17, 2011 22:20 by adrianparr


Updated Code
// This code assumes you have an input textfield on the stage 
// called 'inputTxt' and a button called 'btn'.
// Here we are just inserting incrementing number as an example.

import flash.events.MouseEvent;
import flash.text.TextField;

var num:int = 1;

btn.addEventListener(MouseEvent.CLICK, onBtnClick);

function onBtnClick(e:MouseEvent):void {
	var charToInsert:String = String(num);
	var tf:TextField = inputTxt;
	var ci:int = tf.caretIndex;
	var newText:String = tf.text.substring(0, ci) + charToInsert + tf.text.substring(ci, tf.text.length)
	tf.text = newText;
	tf.setSelection(ci+(charToInsert.length), ci+(charToInsert.length));
	num++;
}

Revision: 53277
at November 17, 2011 22:09 by adrianparr


Initial Code
var charToInsert:String = String(num);
var tf:TextField = inputTxt;
var ci:int = tf.caretIndex;
var newText:String = tf.text.substring(0, ci) + charToInsert + tf.text.substring(ci, tf.text.length)
tf.text = newText;
tf.setSelection(ci+(charToInsert.length), ci+(charToInsert.length));

Initial URL


Initial Description
This code gets the current caretIndex in the textfield and then inserts the specified string at that point. The caretIndex position is then updated using setSelection, ready for the next insertion. This was developed to be used with an on-screen keyboard (for entering text into a textfield when the user is in fullscreen mode). The backspace button is intended to function in the same manner as pressing the delete key on your keyboard.

Initial Title
AS3 Insert and Delete Characters at TextField CaretIndex Position

Initial Tags
text

Initial Language
ActionScript 3