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

class

DictationTargets

public static class DictationTargets

Finds 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

NameDescription
RegisterRecords 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.
OfThe target an element was registered with, or null when it is not a target's root element.
ElementsThe root elements of every target on the page, in document order.
AllEvery target currently on the page, in document order, visible or not.
VisibleThe targets that can be seen right now: rendered, not hidden, and within the viewport.
FocusedThe target holding keyboard focus (focus is in its element), or null when focus is elsewhere.
CurrentThe target to dictate into: Focused when there is one, otherwise the first Visible target that is IsEditable. Null when there is none.
Method
DictationTargets.Register
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.
Method
DictationTargets.Of
public static IDictationTarget Of(HTMLElement element)

The target an element was registered with, or null when it is not a target's root element.

Method
DictationTargets.Elements
public static IReadOnlyList<HTMLElement> Elements()

The root elements of every target on the page, in document order.

Method
DictationTargets.All
public static IReadOnlyList<IDictationTarget> All()

Every target currently on the page, in document order, visible or not.

Method
DictationTargets.Visible
public static IReadOnlyList<IDictationTarget> Visible()

The targets that can be seen right now: rendered, not hidden, and within the viewport.

Method
DictationTargets.Focused
public static IDictationTarget Focused()

The target holding keyboard focus (focus is in its element), or null when focus is elsewhere.

Method
DictationTargets.Current
public static IDictationTarget Current()

The target to dictate into: Focused when there is one, otherwise the first Visible target that is IsEditable. Null when there is none.

Fields

NameDescription
ElementAttributeThe attribute that marks the root element of a target, for a query or a style.
Field
DictationTargets.ElementAttribute
public const string ElementAttribute = "data-tss-dictation"

The attribute that marks the root element of a target, for a query or a style.

interface

IDictationTarget

public interface IDictationTarget

A 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

NameDescription
TextThe text in the field. For an OmniBox, the text of the input that is showing.
IsEditableTrue when the field takes text: it is enabled and not read-only.
CanDictateTrue when something can supply words for the field (Dictation); an OmniBox also needs DictationButton.
IsDictatingTrue while dictation is running into the field.
Property
IDictationTarget.Text
string Text { get; set; }

The text in the field. For an OmniBox, the text of the input that is showing.

Property
IDictationTarget.IsEditable
bool IsEditable { get; }

True when the field takes text: it is enabled and not read-only.

Property
IDictationTarget.CanDictate
bool CanDictate { get; }

True when something can supply words for the field (Dictation); an OmniBox also needs DictationButton.

Property
IDictationTarget.IsDictating
bool IsDictating { get; }

True while dictation is running into the field.

Methods

NameDescription
BeginDictationStarts 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.
EndDictationStops dictating; what was heard stays in the field.
Method
IDictationTarget.BeginDictation
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.

Method
IDictationTarget.EndDictation
void EndDictation()

Stops dictating; what was heard stays in the field.

See also