상세 컨텐츠

본문 제목

플래시 as3.0 포커스 관련

ADOBE/ ActionScript

by AlrepondTech 2012. 1. 5. 16:24

본문

반응형

 

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

 

 

출처: http://metatron28.tistory.com/5

addEventListener(FocusEvent.KEY_FOCUS_CHANGE, fnTabMove); private function fnTabMove(e:Event):void {        e.preventDefault(); } 

끝이다. 간단하지 않은가?

//-------------------------------------------------------------
위의 설정을 이용해 몇몇 키버튼이벤트일때 포커스를 이동못하게 응용한다.




//--------------------------------------------------------------
*********************************************************************************************
출처: http://sierakowski.eu/list-of-tips/55-setting-the-tab-order-of-the-form-items-and-customising-the-focus-rectangle.html

etting the tab order of the form items and customising the focus rectangle

 

The tab order is the sequence in which a user moves focus from one object to another by pressing the TAB key. By default, the tab order is the same as the order in which you created the objects. So, if lets say you created the submit button first and then the input boxes, then the button will have tab focus first before input boxes. This can be changed from Action Script using the tabIndex property of every display object.

 

There are few properties that come handy when creating any kind of forms in your Flash project.

  • tabIndex - specifies the tab ordering of objects, object with index 1 will be before object with index 2.
  • tabEnabled - includes or excludes object from the tabIndex order.
  • focusRect - using this property you can enable or disable a yellow focus rectangle on the object.
  • stage.focus - specifies which stage object has focus by default
  • setSelection - method of a text fields to select range of text when getting focus
  • FocusEvent.FOCUS_IN and FOCUS_OUT events are fired when object gets or lose focus

1. Changing the tab index

In my example below I created a simple contact form using the text input components, the button components, the standard input text field and the Movie Clip that has a MouseEvent.CLICK listener added to it. I decided to set the inverse order for my form objects, so they will be getting focus from the bottom to the top. To test the clip, click on it and then use your Tab key to change objects' focus.

 

And below is the code used in this example:

//setting actions for movieclip button
 mcButton_mc.addEventListener(MouseEvent.MOUSE_DOWN, cursorDown);
 mcButton_mc.addEventListener(MouseEvent.MOUSE_UP, cursorOut);
 mcButton_mc.addEventListener(MouseEvent.ROLL_OVER, cursorOver);
 mcButton_mc.addEventListener(MouseEvent.ROLL_OUT, cursorOut);
  
 function cursorDown(e:MouseEvent):void
 {
   e.currentTarget.gotoAndStop(3);
 }
  
 function cursorOver(e:MouseEvent):void
 {
   e.currentTarget.gotoAndStop(2);
 }
  
 function cursorOut(e:MouseEvent):void
 {
   e.currentTarget.gotoAndStop(1);
 }
  
 //setting the tab order for my form elements
 firstName_mc.tabIndex = 6;
 lastName_mc.tabIndex = 5;
 checkBox_mc.tabIndex = 4;
 inputText_txt.tabIndex = 3;
 mcButton_mc.tabIndex = 2;
 submit_mc.tabIndex = 1;
  

 

As you can see, when pressing the Tab key, focus on elements goes in reverse order from the bottom to the top. Everything is fine, the only thing is that the movie clip mcButton_mc is outside this order, it never gets selected, the focus frame just jumps from the Submit button on the InputText. So lets include it in the tabIndex.

2. Including Movie Clips in the tab index

I'm going to add two more lines to the code:

mcButton_mc.tabEnabled = true;
 mcButton_mc.focusRect = true;

 

First line makes the mcButton_mc included in the tab index, so now a line mcButton_mc.tabIndex = 2 will take an effect. The second line adds focus rectangle, without it a user wouldn't know if mcButton_mc is selected or not. Now test the example below, the movie clip is second in order after the Submit button.


There is one thing to note, you can't disable focusRectangle from components. They actually have customised rectangle. Read chapter 5 of this article to learn more.

Using tabEnabled we can exclude from tab ordering all the objects that are included by default. I added this line:

firstName_mc.tabEnabled = false;

 

And now if you do test with Tab on the below example, you will see that the firstName component doesn't get focus anymore.

 

Ok, but what if we want to automatically set focus on any of the stage elements?

3. Setting focus automatically on any of the stage objects

To set focus automatically, use focus property of a stage object. In my example I decided to add default focus to the lastName_mc.

stage.focus = lastName_mc;

 

Notice that the lastName has now focus automatically set.

 

4. Selecting a range of text when a text field gets focus

Unfortunately an input text by default doesn't have any focus rectangle. You can create a movie clip and with this text field to get standard yellow rectangle.

But there is another handy feature that you can use. setSelection method sets the range of text selection when a text field has focus. As the parameters you pass begin index and end index of a string.

In my example, I added some dummy text to the input text from Flash and added this line to the code:

//select a 
 range beginning from first character
 inputText_txt.setSelection(0, 5);

 

Click on the example below and using the Tab key change focus on the input text. Note that first 5 characters of text will be automatically selected.

 

5. Customising the look of the focus rectangle

The components have pretty nice focus rectangles already. But the default yellow rectangle for movie clips is not that cool at all. Fortunately, there is a way of changing that.

When any display object gets focus, it fires FocusEvent.FOCUS_IN or FOCUS_OUT. We can add an event listener and do whatever we want with our movie clip.

I decided to just change a frame of my movie clip, same as for the mouse roll-over and the roll-out event handlers, but you can create another movie clip with some box graphic, name it focusRectangle_mc and place it to the mcButton_mc. Then you could just call mcButton_mc.focusRectangle_mc.visible = true or false to show and hide it depending where the focus is.

mcButton_mc.addEventListener(FocusEvent.FOCUS_IN, 
 showCustomRectangle);
 mcButton_mc.addEventListener(FocusEvent.FOCUS_OUT, 
 hideCustomRectangle);
  
 function showCustomRectangle(e:FocusEvent):void
 {
   e.currentTarget.gotoAndStop(2);
 }
  
 function hideCustomRectangle(e:FocusEvent):void
 {
   e.currentTarget.gotoAndStop(1);
 }

 

Now, test it by clicking on the movie clip below and press the Tab key. mcButton_mc will change its state when gets focus.

 

 

 

=================================

=================================

=================================

 

 

반응형


관련글 더보기

댓글 영역