Wednesday, April 21, 2010

Prevent changing the cursur position in TextInput fields

Ever tried to prevent the user from changing the cursor position using the arrow keys (or other keys that normally change the cursor position)? In that case you probably already know that event.preventDefault() and event.stopImmediatePropagation() won't help you out. The code below seems to solve the problem:

private function keyDownHandler(event: KeyboardEvent): void
switch (event.keyCode)
case Keyboard.LEFT:
case Keyboard.RIGHT:
case Keyboard.UP:
case Keyboard.DOWN:
case Keyboard.PAGE_UP:
case Keyboard.PAGE_DOWN:
case Keyboard.HOME:
case Keyboard.END:
if (_orgSelectionBegin==-1) _orgSelectionBegin = selectionBeginIndex;
if (_orgSelectionEnd==-1) _orgSelectionEnd = selectionEndIndex;
setSelection(_orgSelectionBegin, _orgSelectionEnd);

private function keyUpHandler(event: KeyboardEvent): void
_orgSelectionBegin = -1;
_orgSelectionEnd = -1;

private var _orgSelectionBegin: int = -1;
private var _orgSelectionEnd: int = -1;

Do you also think this is way too much code for doing something simple?
In that case vote for

1 comment:

Anonymous said...

I seldom leave comments on blog, but I have been to this post which was recommended by my friend, lots of valuable details, thanks again.