Preset Global Hotkey Development (for System Applications Only)
When to Use
You can use the preset global hotkeys to set the delay time for starting an ability. An example is to take a screenshot 5 seconds after the hotkey is pressed.
Modules to Import
import { shortKey } from '@kit.InputKit';
Available APIs
The following table lists common APIs for event injection. For details, see @ohos.multimodalInput.shortKey (Preset Global Shortcut Keys) (System API).
| API | Description |
|---|---|
| setKeyDownDuration(businessKey: string, delay: number, callback: AsyncCallback<void>): void | Sets the delay for starting an ability using the hotkey. |
How to Develop
The following describes how to take a screenshot five seconds after the hotkey is pressed.
import { shortKey } from '@kit.InputKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
build() {
RelativeContainer() {
Text()
.onClick(() => {
try {
shortKey.setKeyDownDuration("screenshot", 500, (error: BusinessError) => { // Set the delay to 5 seconds (500 ms).
if (error) {
console.error(`Set key down duration failed, error: ${JSON.stringify(error, ["code", "message"])}`);
return;
}
console.info(`Set key down duration success`);
});
} catch (error) {
console.error(`Set key down duration failed, error: ${JSON.stringify(error, ["code", "message"])}`);
}
})
}
}
}