Keyboard
The Keyboard aptitude provides access to the the ability to listen to hotkeys pressed and text or characters typed.
The Keyboard aptitude expects that a user is using a standard QWERTY layout for their keys, and will not operate as expected with alternate layouts.
API
Example
Permissions

listenHotkey

Calls callback function when the specified hotkey is pressed or released.
1
import { keyboard } from '@oliveai/ldk';
2
3
// The hotkey to monitor to initiate callback.
4
// There are more modifiers to choose, please visit our documentation
5
// https://open-olive.github.io/loop-development-kit/ldk/javascript/interfaces/hotkey.html
6
7
// Add hotkeys: Ctrl + a
8
9
const hotkeys = {
10
key: 'a',
11
control: true,
12
};
13
14
// The callback function called when the specified hotkey is pressed or released.
15
// Calls callback function when the specified hotkey is pressed or released.
16
const callback = (pressed) => {
17
console.log('Hotkey pressed', 'response', pressed);
18
};
19
20
// Function that is called when listenHotkey() detects hotkeys being pressed.
21
const listener = keyboard.listenHotkey(hotkeys, pressed);
22
23
// Clears the listener function
24
listener.cancel();
Copied!

listenText

Sets a listener that detects text from the user's keyboard, triggering a callback when a user types and then pauses briefly.
1
import { keyboard } from '@oliveai/ldk';
2
3
// The callback function called when text is detected from the keyboard
4
const callback = (text) => {
5
console.log(`Received keyboard text: ${text}`);
6
};
7
8
// if passed in true, callback will be called while olive helps window is in focus. Disabled by default.
9
const includeOliveHelpsEvents = true;
10
11
const listener = keyboard.listenText(callback, includeOliveHelpsEvents);
12
13
// Clears the listener function
14
listener.cancel();
Copied!

listenCharacter

Calls callback function when any character is detected from the keyboard.
1
import { keyboard } from '@oliveai/ldk';
2
3
// Function that is called after listenCharacter() detects a keyboard event.
4
// The callback function called when a character is detected from the keyboard.
5
const callback = (char) => {
6
console.log(`Received keyboard text: ${char}`);
7
};
8
9
const listener = keyboard.listenCharacter(callback);
10
11
// Clears the listener function
12
listener.cancel();
Copied!
If you are trying to display an information Whisper about a physician to the user. Other than using the Clipboard Aptitude, you can also use the Keyboard Aptitude.
To do this, you would use the listenCharacter() function. It adds a sensor to the loop, catching all incoming changes of a user's keyboard.
In addition to detecting the events, it allows you to provide a callback to respond to any incoming events.
Let's look at an example to better illustrate the point.
1
import { keyboard, whisper } from '@oliveai/ldk';
2
const typedChar = 'o';
3
4
const keyboardListenChar = () =>
5
keyboard.listenCharacter((keyboardChar) => {
6
if (typedChar === keyboardChar) {
7
whisper.create({
8
// Information card whisper
9
});
10
} else {
11
console.log(`Received keyboard character: ${keyboardChar}`);
12
}
13
});
Copied!
In this example, if a user were to type the character "o" from the keyboard, it would trigger the whisper displaying an information card. Anything else would cause the loop to log received keyboard character. The second parameter includeOliveHelpsEvents is optional, so we skip this parameter for now.
To use the Keyboard aptitude, simply set the following permissions in your package.json under the ldk object.
1
...
2
"ldk": {
3
"permissions": {
4
"keyboard": {},
5
...
6
}
7
},
8
...
Copied!
Last modified 7d ago
Copy link