From 2ef9b10032cbfde0c67802f52bc78e54b1faa2a6 Mon Sep 17 00:00:00 2001
From: licui
Date: Tue, 22 Jan 2019 16:13:49 -0500
Subject: [PATCH] Add types for GlobalHotkey
---
types/openfin/index.d.ts | 58 ++++++++++++++++++++++++++++++++++
types/openfin/openfin-tests.ts | 45 ++++++++++++++++++++++++++
2 files changed, 103 insertions(+)
diff --git a/types/openfin/index.d.ts b/types/openfin/index.d.ts
index 40f921a3fe..b5d04f1a27 100644
--- a/types/openfin/index.d.ts
+++ b/types/openfin/index.d.ts
@@ -78,6 +78,7 @@ declare namespace fin {
main(f: () => any): void;
Application: OpenFinApplicationStatic;
ExternalApp: OpenFinExternalApplicationStatic;
+ GlobalHotkey: OpenFinGlobalHotkey;
InterApplicationBus: OpenFinInterApplicationBus;
Notification: OpenFinNotificationStatic;
System: OpenFinSystem;
@@ -312,6 +313,45 @@ declare namespace fin {
errorCallback?: (reason: string, error: ErrorInfo) => void): void;
}
+ /**
+ * GlobalHotkey
+ * The Global Hotkey allows the registration and unregistration of given hotkeys at the OS level, meaning a Window/Application will receive the events regardless of focused state.
+ */
+ interface OpenFinGlobalHotkey {
+ /**
+ * Registers an event listener on the specified event.
+ */
+ addEventListener(
+ type: OpenFinGlobalHotkeyEventType,
+ listener: (event: GlobalHotkeyEvent) => void,
+ callback?: () => void,
+ errorCallback?: (reason: string, error: ErrorInfo) => void): void;
+ /**
+ * Checks if a given hotkey has been registered
+ */
+ isRegistered(hotkey: string, callback?: (registered: boolean) => void, errorCallback?: (reason: string, error: ErrorInfo) => void): void;
+ /**
+ * Registers a global hotkey with the operating system.
+ */
+ register(hotkey: string, listener: () => void, callback?: () => void, errorCallback?: (reason: string, error: ErrorInfo) => void): void;
+ /**
+ * Removes a previously registered event listener from the specified event.
+ */
+ removeEventListener(
+ type: OpenFinGlobalHotkeyEventType,
+ listener: (event: GlobalHotkeyEvent) => void,
+ callback?: () => void,
+ errorCallback?: (reason: string, error: ErrorInfo) => void): void;
+ /**
+ * Unregisters a global hotkey with the operating system.
+ */
+ unregister(hotkey: string, callback?: () => void, errorCallback?: (reason: string, error: ErrorInfo) => void): void;
+ /**
+ * Unregisters all global hotkeys for the current application.
+ */
+ unregisterAll(callback?: () => void, errorCallback?: (reason: string, error: ErrorInfo) => void): void;
+ }
+
/**
* InterApplicationBus
* A messaging bus that allows for pub/sub messaging between different applications.
@@ -982,6 +1022,21 @@ declare namespace fin {
uuid: string;
}
+ interface GlobalHotkeyEvent {
+ topic: string;
+ type: OpenFinGlobalHotkeyEventType;
+ /**
+ * The Identity that has just registered the hotkey
+ */
+ identity: {
+ name: string;
+ uuid: string;
+ parentFrame: string;
+ entityType: string;
+ },
+ hotkey: string;
+ }
+
interface DesktopIconClickedEvent {
mouse: {
/**
@@ -1211,6 +1266,9 @@ declare namespace fin {
type OpenFinExternalApplicationEventType = "connected"
| "disconnected";
+ type OpenFinGlobalHotkeyEventType = "registered"
+ | "unregistered";
+
type OpenFinSystemEventType = "application-closed"
| "application-crashed"
| "application-created"
diff --git a/types/openfin/openfin-tests.ts b/types/openfin/openfin-tests.ts
index fb9e216d04..5367823dae 100644
--- a/types/openfin/openfin-tests.ts
+++ b/types/openfin/openfin-tests.ts
@@ -164,6 +164,51 @@ function test_external_application() {
}, err => console.log(err));
}
+function test_global_hot_key() {
+ // addEventListener
+ fin.desktop.GlobalHotkey.addEventListener('registered', (event) => {
+ console.log(` window ${event.identity.name} has registered ${event.hotkey}`, event);
+ }, () => {
+ console.log('The registration was successful');
+ }, (reason, err) => {
+ console.log(`Error Message: ${err.message} Error Stack: ${err.stack}`);
+ });
+ // isRegister
+ const hotkey = 'CommandOrControl+X';
+ fin.desktop.GlobalHotkey.isRegistered(hotkey, registered => {
+ console.log(`hotkey ${hotkey} is registered ? ${registered}`);
+ }, (reason, err) => {
+ console.log('Error unregistering the hotkey', err);
+ });
+ // register
+ fin.desktop.GlobalHotkey.register(hotkey, () => {
+ console.log(`${hotkey} pressed`);
+ }, () => {
+ console.log('Success');
+ }, (reason, err) => {
+ console.log('Error registering the hotkey', err);
+ });
+ // removeEventListener
+ const previousCallback = () => { };
+ fin.desktop.GlobalHotkey.removeEventListener('registered', previousCallback, () => {
+ console.log('The unregistration was successful');
+ }, (reason, err) => {
+ console.log(`Error Message: ${err.message} Error Stack: ${err.stack}`);
+ });
+ // unregister
+ fin.desktop.GlobalHotkey.unregister(hotkey, () => {
+ console.log('Success');
+ }, (reason, err) => {
+ console.log('Error unregistering the hotkey', err);
+ });
+ // unregisterAll
+ fin.desktop.GlobalHotkey.unregisterAll(() => {
+ console.log('Success');
+ }, (reason, err) => {
+ console.log('Error unregistering all hotkeys for this application', err);
+ });
+}
+
function test_inter_application_bus() {
// addSubscribeListener
fin.desktop.InterApplicationBus.addSubscribeListener((uuid, topic, name) => {