Skip to content

Commit

Permalink
Adjust types
Browse files Browse the repository at this point in the history
  • Loading branch information
raub committed Dec 3, 2023
1 parent c424903 commit a877206
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 195 deletions.
28 changes: 11 additions & 17 deletions examples/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,27 @@ iohook.on('mousewheel', (msg) => {
console.log(msg);
});

iohook.on('mousemove', (msg) => {
// console.log(msg);
});
// iohook.on('mousemove', (msg) => {
// console.log(msg);
// });

iohook.on('mousedrag', (msg) => {
console.log(msg);
});

// const CTRL = 29;
// const ALT = 56;
// const F7 = 65;

// iohook.registerShortcut([CTRL, F7], (keys) => {
// console.log('Shortcut pressed with keys:', keys);
// });
const CTRL = 29;
const F7 = 65;

// let shId = iohook.registerShortcut([ALT, F7], (keys) => {
// console.log('This shortcut will be called once. Keys:', keys);
// iohook.unregisterShortcut(shId);
// });
iohook.shortcut([CTRL, F7], (keys) => {
console.log('Shortcut pressed with keys:', keys);
});

iohook.on('mouseup', (event) => console.log(event));

iohook.start();
// iohook.setDebug(true); // Uncomment this line for see all debug information from iohook

console.log('Hook started. Try input.');
console.log('Hook started. Try input. CTRL+F7 shortcut.');


setTimeout(() => {
Expand All @@ -68,6 +62,6 @@ setTimeout(() => {
console.log('Hook stopped.');
iohook.stop();
}, 3000);
}, 3000);
}, 3000);
}, 5000);
}, 5000);

105 changes: 73 additions & 32 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,59 @@ declare module "iohook-raub" {

type EventEmitter = import('node:events').EventEmitter;

// type TEvent = {
// type: string;
// keychar?: number;
// keycode?: number;
// rawcode?: number;
// button?: number;
// clicks?: number;
// x?: number;
// y?: number;
// }
type THookEventCommon = {
mask: number;
time: number;
};

type TEventNameKeyboard = 'keypress' | 'keydown' | 'keyup';

type TEventNameMouse = 'mouseclick' | 'mousedown' | 'mouseup' | 'mousemove' | 'mousedrag';

type TEventNameWheel = 'mousewheel';

type THookEventKeyboard = THookEventCommon & {
type: TEventNameKeyboard;
shiftKey: boolean;
altKey: boolean;
ctrlKey: boolean;
metaKey: boolean;
keychar: number;
keycode: number;
rawcode: number;
};

type THookEventMouse = THookEventCommon & {
type: TEventNameMouse;
button: number;
clicks: number;
x: number;
y: number;
};

type THookEventWheel = THookEventCommon & {
type: TEventNameWheel;
amount: number;
clicks: number;
direction: number;
rotation: number;
x: number;
y: number;
};

interface TPossibleSubscriptions {
addListener(eventName: TEventNameKeyboard, listener: (event: THookEventKeyboard) => void): this;
addListener(eventName: TEventNameMouse, listener: (event: THookEventMouse) => void): this;
addListener(eventName: TEventNameWheel, listener: (event: THookEventWheel) => void): this;
on(eventName: TEventNameKeyboard, listener: (event: THookEventKeyboard) => void): this;
on(eventName: TEventNameMouse, listener: (event: THookEventMouse) => void): this;
on(eventName: TEventNameWheel, listener: (event: THookEventWheel) => void): this;
once(eventName: TEventNameKeyboard, listener: (event: THookEventKeyboard) => void): this;
once(eventName: TEventNameMouse, listener: (event: THookEventMouse) => void): this;
once(eventName: TEventNameWheel, listener: (event: THookEventWheel) => void): this;
}

type IoHook = EventEmitter & {
type IoHook = Omit<EventEmitter, 'addListener' | 'on' | 'once'> & TPossibleSubscriptions & {
/**
* Start hooking engine. Call it when you ready to receive events
* If `enableLogger` is true, module will publish debug information to stdout
Expand All @@ -40,34 +81,34 @@ declare module "iohook-raub" {
useRawcode(using: boolean): void;

/**
* Register global shortcut. When all keys in keys array pressed, callback will be called
* @param {Array<string|number>} keys Array of keycodes
* @param {Function} callback Callback for when shortcut pressed
* @param {Function} [releaseCallback] Callback for when shortcut released
* @return {number} ShortcutId for unregister
*/
registerShortcut(
keys: Array<string | number>,
callback: Function,
releaseCallback?: Function
): number;

/**
* Unregister shortcut by ShortcutId
* @param {number} shortcutId
* Register a global shortcut. When all keys in keys array pressed, callback will be called
*
* @returns Function that removes this specific shortcut.
*/
unregisterShortcut(shortcutId: number): void;
shortcut(
/**
* Array of keycodes.
*/
keys: number[],
/**
* Callback for when shortcut pressedю
*/
onDown: (keys: number[]) => void,
/**
* Callback for when shortcut releasedю
*/
onUp?: (keys: number[]) => void
): (() => void);

/**
* Unregister shortcut via its key codes
* @param {Array<string|number>} keys
* Unregister shortcut via its key codes.
*/
unregisterShortcut(keys: Array<string | number>): void;
removeShortcut(keys: number[]): void;

/**
* Unregister all shortcuts
* Unregister all shortcuts.
*/
unregisterAllShortcuts(): void;
clearShortcuts(): void;
};

const iohook: IoHook;
Expand Down

0 comments on commit a877206

Please sign in to comment.