# when

当artyom中某个特殊的事件触发时，该函数允许执行你所期待的操作。 下述列表列出了所有本函数支持的事件:

| Name                        |                                                                                                                                                                                                                                                                                                     Description                                                                                                                                                                                                                                                                                                    |
| --------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| ERROR                       | <p>Catches all the events identified with the code "error" and have subobjects. The objects have the following codes:<br><em><strong>info-blocked</strong></em><br><em><strong>info-denied</strong></em><br><em><strong>no-speech</strong></em><br><em><strong>aborted</strong></em><br><em><strong>audio-capture</strong></em><br><em><strong>network</strong></em><br><em><strong>not-allowed</strong></em><br><em><strong>service-not-allowed</strong></em><br><em><strong>bad-grammar</strong></em><br><em><strong>language-not-supported</strong></em><br><em><strong>recognition\_overlap</strong></em> </p> |
| SPEECH\_SYNTHESIS\_START    |                                                                                                                                                                                                                            <p>Global event triggered when artyom.say starts to speak some text.</p><p>This event is dispatched along with the onStart event of artyom.say (2 times).</p>                                                                                                                                                                                                                           |
| SPEECH\_SYNTHESIS\_END      |                                                                                                                                                                                                                            <p>Global event triggered when artyom.say finishes to read some text.</p><p>This event is dispatched along with the onEnd event of artyom.say (2 times).</p>                                                                                                                                                                                                                            |
| TEXT\_RECOGNIZED            |                                                                                                                                                                                                                                                             Global event triggered when the user speaks to the microphone and some text is recognized.                                                                                                                                                                                                                                                             |
| COMMAND\_RECOGNITION\_START |                                                                                                                                                                                                                                                                      Global event triggered when artyom starts to listening to your commands.                                                                                                                                                                                                                                                                      |
| COMMAND\_RECOGNITION\_END   |                                                                                                                                                                                            <p>Global event triggered when artyom doesn't listen anymore to your commands. </p><p><strong>The callback returns an object with information if the onEnd event has been triggered with continuous mode or not.</strong></p>                                                                                                                                                                                           |
| COMMAND\_MATCHED            |                                                                                                                                                                                                                                                    Global event triggered when one of the provided commands matches with your voice. (A commands has been found)                                                                                                                                                                                                                                                   |
| NOT\_COMMAND\_MATCHED       |                                                                                                                                                                                                                                                         Global event triggered when the text spoken by the user does not match any of the loaded commands.                                                                                                                                                                                                                                                         |

## 获取所有artyom中的错误

```javascript
let artyom = new Artyom();

//All catchable artyom errors will be catched with this
artyom.when("ERROR",function(error){

    if(error.code == "network"){
        alert("An error ocurred, artyom cannot work without internet connection !");
    }

    if(error.code == "audio-capture"){
        alert("An error ocurred, artyom cannot work without a microphone");
    }

    if(error.code == "not-allowed"){
        alert("An error ocurred, it seems the access to your microphone is denied");
    }

    console.log(error.message);
});
```

## 处理其他事件

```javascript
let artyom = new Artyom();

var onCommandMatched = "COMMAND_MATCHED";

artyom.when(onCommandMatched,function(){
   console.log("A command has been found and it will be executed.");
});

// Some events receives an object
artyom.when("COMMAND_RECOGNITION_END",function(status){
   if(status.code == "continuous_mode_enabled"){
     console.log("You're using continuous mode, therefore this callbacks is more likely to don't be used");
   }else if(status.code == "continuous_mode_disabled"){
     console.log("The continuous mode is disabled. Artyom will not listen anymore till the next initialization");
   }
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://more-about-angular.gitbook.io/artyom-js/api/untitled-30.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
