DictationTargets
Description
DictationTargets finds the text field to dictate into from anywhere on the page: the focused field, else the first visible one that takes text. Use it for a push-to-talk button, a keyboard shortcut or a voice command that types into whatever field the user is in, without holding references to the fields.
Every TextBox, TextArea and OmniBox is an IDictationTarget, wherever it was built, and registers itself on construction. Nothing needs to be declared. Each target stores itself in a hidden field of its root element and marks the element with data-tss-dictation, so a query is one querySelectorAll over what is on the page now. A field removed from the page is not found.
The words come from the field's own source, else Dictation.Provider, else the browser's speech recognition. The browser path needs a microphone and a browser that exposes a speech recognition API, and the user has to grant microphone permission. A field with no source and no browser support cannot dictate: CanDictate is false and BeginDictation() does nothing.
Usage
DictationTargets.Current() returns the focused target, else the first visible one that is editable, or null when there is none. Call BeginDictation() on it.
A button takes focus when it is clicked, so Focused() would then find the button's page rather than the field. Stop the mousedown on the button so the field keeps focus. The sample below uses a scripted speech source in place of a microphone, so it runs in any browser.
Push-to-talk button
Members
IDictationTarget:
| Member | Description |
|---|---|
Text |
The text in the field. For an OmniBox, the input that is showing. |
IsEditable |
The field is enabled and not read-only. |
CanDictate |
Something can supply words. An OmniBox also needs Config.DictationButton. |
IsDictating |
The field is dictating now. |
BeginDictation() |
Starts dictating after the text already there, whether or not the field shows a microphone button. Does nothing when already dictating or when CanDictate is false. |
EndDictation() |
Stops. What was heard stays. |
DictationTargets (static):
| Member | Description |
|---|---|
Current() |
The focused target, else the first visible one that IsEditable. Null when there is none. |
Focused() |
The target with keyboard focus anywhere inside its element, or null. |
Visible() |
Targets rendered, not hidden and within the viewport, in document order. |
All() |
Every target on the page, in document order, visible or not. |
Elements() / Of(element) |
The root elements, and the target of one element. |
Register(element, target) |
For a component of your own that implements IDictationTarget. Call once with its root element. |
The queries live on a static class rather than on the interface because the netstandard2.0 target does not allow static interface members.
API reference
public static class DictationTargetsFinds the IDictationTargets on the page. A target puts itself in a hidden field of its root element (Register, which the TextBox, TextArea and OmniBox call on construction) and marks the element with ElementAttribute, so a query is one querySelectorAll and the answer is whatever is rendered right now: a field that was removed from the page is not in the list, and one that was never built by the toolkit but registered itself is.
- Namespace
- Tesserae
Methods
| Name | Description |
|---|---|
| Register | Records target in a hidden field of element and marks the element, so the queries here find it for as long as it is on the page. Safe to call again for the same element. |
| Of | The target an element was registered with, or null when it is not a target's root element. |
| Elements | The root elements of every target on the page, in document order. |
| All | Every target currently on the page, in document order, visible or not. |
| Visible | The targets that can be seen right now: rendered, not hidden, and within the viewport. |
| Focused | The target holding keyboard focus (focus is in its element), or null when focus is elsewhere. |
| Current | The target to dictate into: Focused when there is one, otherwise the first Visible target that is IsEditable. Null when there is none. |
public static void Register(HTMLElement element, IDictationTarget target)Records target in a hidden field of element and marks the element, so the queries here find it for as long as it is on the page. Safe to call again for the same element.
Parameters
- element
- The root element of the field: the one that holds it and its microphone button.
- target
- The field.
public static IDictationTarget Of(HTMLElement element)The target an element was registered with, or null when it is not a target's root element.
public static IReadOnlyList<HTMLElement> Elements()The root elements of every target on the page, in document order.
public static IReadOnlyList<IDictationTarget> All()Every target currently on the page, in document order, visible or not.
public static IReadOnlyList<IDictationTarget> Visible()The targets that can be seen right now: rendered, not hidden, and within the viewport.
public static IDictationTarget Focused()The target holding keyboard focus (focus is in its element), or null when focus is elsewhere.
Fields
| Name | Description |
|---|---|
| ElementAttribute | The attribute that marks the root element of a target, for a query or a style. |
public interface IDictationTargetA text field that dictation can be started into: the TextBox, the TextArea and the OmniBox are, wherever they are on the page. Whatever transcribes speech for the application - a keyboard shortcut, a push-to-talk button outside the field, a voice command - does not need to hold on to the fields: it asks Current for the one in use and starts dictating into it.
Remarks
The queries are on DictationTargets rather than static members of this interface, which the netstandard2.0 target does not allow (CS8701). Every rendered target carries itself in a hidden field of its root element, so a lookup needs nothing from the application's own code. A component of the application that implements this interface joins the toolkit's by calling Register once with its root element.
- Namespace
- Tesserae
Properties
| Name | Description |
|---|---|
| Text | The text in the field. For an OmniBox, the text of the input that is showing. |
| IsEditable | True when the field takes text: it is enabled and not read-only. |
| CanDictate | True when something can supply words for the field (Dictation); an OmniBox also needs DictationButton. |
| IsDictating | True while dictation is running into the field. |
string Text { get; set; }The text in the field. For an OmniBox, the text of the input that is showing.
bool IsEditable { get; }True when the field takes text: it is enabled and not read-only.
bool CanDictate { get; }True when something can supply words for the field (Dictation); an OmniBox also needs DictationButton.
Methods
| Name | Description |
|---|---|
| BeginDictation | Starts dictating after the text already in the field, as pressing its microphone button does, whether or not the field shows one. Does nothing when already dictating or when CanDictate is false. |
| EndDictation | Stops dictating; what was heard stays in the field. |
void BeginDictation()Starts dictating after the text already in the field, as pressing its microphone button does, whether or not the field shows one. Does nothing when already dictating or when CanDictate is false.