2013-02-04 11 views
1

나는 검색 상자와 검색 버튼이있는 플래시 무비를 가지고 있습니다. 버튼의 코드는 다음과 같습니다.(keyPress "<Enter>")이 작동하지 않습니다.

on (release, keyPress "<Enter>") { 
    searchbox.execute();  
    /*the function above processes searches*/ 
} 

버튼을 클릭하면 정상적으로 작동합니다. Enter를 누르면 콩을하지 않습니다! 아무도 이것이 왜, 그리고 내가 그것을 해결할 수있는 방법을 알 수 있습니까? 나는 그것을 전혀 피할 수 있다면 청취자를 사용하지 않는 것을 선호한다.

답변

3

on() 사용은 비난 된 AS1 연습이므로 사용을 중지해야합니다. MovieClip 클래스의 onKeyDown 이벤트 핸들러 덕분에 적절한 코드를 사용하여 리스너 없이도이를 수행 할 수 있으므로 걱정할 필요가 없습니다. ;)

어쨌든 코드가 있습니다. 버튼이 포함 된 타임 라인에 다음을 입력하십시오.

//Enable focus for and set focus to your button 
searchButton.focusEnabled = true; 
Selection.setFocus(searchButton); 

//The onRelease handler for the button 
searchButton.onRelease = function(){ 
    //You need this._parent this code belongs to the button 
    this._parent.searchbox.execute(); 
} 

//The onKeyDown handler for the button 
searchButton.onKeyDown = function(){ 
    //Key.getCode() returns the key code of the last key press 
    //Key.ENTER is a constant equal to the key code of the enter key 
    if(Key.getCode() == Key.ENTER){ 
     this._parent.searchbox.execute(); 
    } 
} 
+0

감사합니다! 항상 모든 키에서 발사 되었기 때문에 onKeyDown이 (keyPress)보다 비효율적이라고 생각했기 때문에 사용하기를 꺼려했습니다.하지만 솔루션은 작동합니다. 고맙습니다! – SoItBegins