typing-monitor v0.1.0
Keyboard typing detection made easy.
Why?
TypingMonitor is a keyboard-input detection library for the web.
It helps you detect when a user starts or stops typing inside your app. One of the most common use cases of this library is detecting input/keyboard activity in messaging and chat-based applications.
Installation
To install TypingMonitor as a CommonJS module via a package manager:
# using npm
npm install --save typing-monitor
# using yarn
yarn add typing-monitorimport TypingMonitor from 'typing-monitor'If you are not using a package manager or a module bundler, you can access the pre-compiled production and development UMD builds from here.
You can include the build file in a script tag on your page. The UMD builds make TypingMonitor available as a window.TypingMonitor global variable.
API
TypingMonitor offers 3 different interfaces to handle different scenarios:
TypingMonitor.Static → StaticTypingMonitorTypingMonitor.Global → GlobalTypingMonitorTypingMonitor.Listener → ListenerTypingMonitor
new TypingMonitor.Static(options: Object) → StaticTypingMonitor
Highlights
- Meant to be used within an exiting
inputevent handler (e.g.React'sonInput). TypingMonitor.Staticis an alias ofTypingMonitor
import TypingMonitor from 'typing-monitor';
const monitor = new TypingMonitor({/* options */});
// or
const monitor = new TypingMonitor.Static({/* options */});Options
wait(Number): duration, in milliseconds, between each call to determine if the user has stopped typing.
Instance Methods
monitor.listen(listener: boolean → void)
Used to detect whether or not the user is typing.
Arguments
listener: boolean → void: A callback function to be called every time the user started or stopped typing. Has one argument of typebooleanindicating the typing status.
Example
import TypingMonitor from 'typing-monitor';
class MessageInput extends React.Component {
componentDidMount() {
const { discussion, user } = this.props;
this.dbRef = db.ref(`/discussions/${discussion.id}/users/${user.id}`);
// using TypingMonitor is the same as using TypingMonitor.Static
this.typingMonitor = new TypingMonitor.Static({ wait: 1000 });
}
handleInput(e) {
this.setState({ value: e.target.value });
this.typingMonitor.listen((isTyping) => {
this.dbRef.child('userTyping').set(isTyping);
});
}
render() {
return (
<input onInput={this.handleInput} value={this.state.value} />
);
}
}new TypingMonitor.Global(options) → GlobalTypingMonitor
Highlights
- Listens to the
inputevent on the global/window. - New instances of
GlobalTypingMonitorreferences a singleton. - Every
monitor#listenregisters a new listener. - Each
monitor#listenreturns a function to unsubscribe the listener.
Options
wait(Number): duration, in milliseconds, between each input change to determine if the user stopped typing.
Instance Methods
monitor.listen(listener: boolean → void): unsubscribe
Used to detect whether or not the user is typing.
Arguments
listener(Function): A callback function to be called every time the user starts or stops typing. Has one argument of typebooleanindicating the typing status.
Returns
unsubscribe (Function): A function that unsubscribes the listener
Example
import TypingMonitor from 'typing-monitor';
const globalMonitor = new TypingMonitor.Global({ wait: 1000 });
const unsubscribe = globalMonitor.listen((isTyping) => {
console.log(isTyping ? 'user is typing' : 'user stopped typing');
});
unsubscribe(); // stop listeningnew TypingMonitor.Listener(options) → ListenerTypingMonitor
Highlights
- Listens to the
inputevent of the element passed tooptions.input - Works only on
<input />and<textarea /> - Every
monitor#listenregisters a new listener
Options
wait(Number): duration, in milliseconds, between each input change to determine if the user stopped typing.input: HTMLInputElement | HTMLTextAreaElement.
Instance Methods
monitor.listen(listener: boolean → void): unsubscribe
Used to detect whether or not the user is typing.
Arguments
listener(Function): A callback function to be called every time the user starts or stops typing. Has one argument of typebooleanindicating the typing status.
Returns
unsubscribe (Function): A function that unsubscribes the listener
Example
import TypingMonitor from 'typing-monitor';
const listenerMonitor = new TypingMonitor.Listener({
input: document.querySelector('input.MyFancyInput'),
wait: 1000,
});
const unsubscribe = listenerMonitor.listen((isTyping) => {
console.log(isTyping ? 'typing' : 'stopped');
});
unsubscribe(); // stop listeningContributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request
License
MIT
7 years ago