Skip to content

Commit

Permalink
Mouse move propagation enabler/disabler
Browse files Browse the repository at this point in the history
- iohook.enableMovePropagation();
- iohook.disableMovePropagation();
- Tested on MacOS
  • Loading branch information
wurikiji committed Sep 21, 2018
1 parent fc25e6f commit f446eee
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ declare class IOHook extends EventEmitter {
*/
disableClickPropagation(): void

enableMovePropagation(): void
disableMovePropagation(): void

/**
* Register global shortcut. When all keys in keys array pressed, callback will be called
* @param {Array<string|number>} keys Array of keycodes
Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class IOHook extends EventEmitter {
stop() {
this.enableClickPropagation();
this.enableKeyboardPropagation();
this.enableMovePropagation();
if (this.active) {
this.active = false;
}
Expand Down Expand Up @@ -145,6 +146,16 @@ class IOHook extends EventEmitter {
NodeHookAddon.grabMouseClick(false);
}

/**
* disable/enable mouse move propagation.
*/

disableMovePropagation() {
NodeHookAddon.grabMouseMove(true);
}
enableMovePropagation() {
NodeHookAddon.grabMouseMove(false);
}
/**
* Local event handler. Don't use it in your code!
* @param msg Raw event message
Expand Down
1 change: 1 addition & 0 deletions libuiohook/include/uiohook.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ extern "C" {

UIOHOOK_API void grab_keyboard(bool enable);
UIOHOOK_API void grab_mouse_click(bool enable);
UIOHOOK_API void grab_mouse_move(bool enable);

// Retrieves an array of screen data for each available monitor.
UIOHOOK_API screen_data* hook_create_screen_info(unsigned char *count);
Expand Down
22 changes: 20 additions & 2 deletions libuiohook/src/darwin/input_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ static dispatcher_t dispatcher = NULL;

static unsigned short int grab_keyboard_event = 0x00;
static unsigned short int grab_mouse_click_event = 0x00;
static unsigned short int grab_mouse_move_event = 0x00;

UIOHOOK_API void hook_set_dispatch_proc(dispatcher_t dispatch_proc) {
logger(LOG_LEVEL_DEBUG, "%s [%u]: Setting new dispatch callback to %#p.\n",
Expand Down Expand Up @@ -877,7 +878,7 @@ static inline void process_mouse_moved(uint64_t timestamp, CGEventRef event_ref)

// Populate mouse motion event.
event.time = timestamp;
event.reserved = 0x00;
event.reserved = grab_mouse_move_event;

if (mouse_dragged) {
event.type = EVENT_MOUSE_DRAGGED;
Expand Down Expand Up @@ -1071,10 +1072,19 @@ CGEventRef hook_event_proc(CGEventTapProxy tap_proxy, CGEventType type, CGEventR
case kCGEventMouseMoved:
// Set the mouse dragged flag.
mouse_dragged = false;
// CGDisplayHideCursor(kCGDirectMainDisplay);;
// CGDisplayShowCursor(kCGDirectMainDisplay);;
process_mouse_moved(timestamp, event_ref);
if (event.reserved) {
int32_t deltaX, deltaY;
CGGetLastMouseDelta(&deltaX, &deltaY);
CGPoint originalPoint;
originalPoint.x = event.data.mouse.x - deltaX;
originalPoint.y = event.data.mouse.y - deltaY;
CGWarpMouseCursorPosition(originalPoint);
}
break;


case kCGEventScrollWheel:
process_mouse_wheel(timestamp, event_ref);
break;
Expand Down Expand Up @@ -1126,6 +1136,14 @@ UIOHOOK_API void grab_mouse_click(bool enabled) {
}
}

UIOHOOK_API void grab_mouse_move(bool enabled) {
grab_mouse_move_event = enabled;
CGDirectDisplayID displays[2];
uint32_t dcnt;
CGGetActiveDisplayList(2, displays, &dcnt);
logger(LOG_LEVEL_DEBUG, "%s [%u]: get displays. (%d) (%d)\n",
__FUNCTION__, __LINE__, dcnt, dcnt);
}
UIOHOOK_API void grab_keyboard(bool enabled){
grab_keyboard_event = enabled;
}
Expand Down
6 changes: 5 additions & 1 deletion libuiohook/src/windows/input_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static uiohook_event event;
static dispatcher_t dispatcher = NULL;

static unsigned short int grab_mouse_click_event = 0x00;
static unsigned short int grab_mouse_move_event = 0x00;
static unsigned short int grab_keyboard_event = 0x00;

UIOHOOK_API void hook_set_dispatch_proc(dispatcher_t dispatch_proc) {
Expand Down Expand Up @@ -440,7 +441,7 @@ static void process_mouse_moved(MSLLHOOKSTRUCT *mshook) {

// Populate mouse move event.
event.time = timestamp;
event.reserved = 0x00;
event.reserved = grab_mouse_move_event;

event.mask = get_modifiers();

Expand Down Expand Up @@ -673,6 +674,9 @@ void CALLBACK win_hook_event_proc(HWINEVENTHOOK hook, DWORD event, HWND hWnd, LO
UIOHOOK_API void grab_keyboard(bool enabled) {
grab_keyboard_event = enabled;
}
UIOHOOK_API void grab_mouse_move(bool enabled) {
grab_mouse_move_event = enabled;
}
UIOHOOK_API void grab_mouse_click(bool enabled) {
if (enabled) {
grab_mouse_click_event = 0x01;
Expand Down
7 changes: 7 additions & 0 deletions src/iohook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ NAN_METHOD(GrabMouseClick) {
grab_mouse_click(info[0]->IsTrue());
}
}
NAN_METHOD(GrabMouseMove) {
if (info.Length() > 0) {
grab_mouse_move(info[0]->IsTrue());
}
}

NAN_METHOD(DebugEnable) {
if (info.Length() > 0)
Expand Down Expand Up @@ -570,6 +575,8 @@ NAN_MODULE_INIT(Init) {
Nan::GetFunction(Nan::New<FunctionTemplate>(GrabKeyboard)).ToLocalChecked());
Nan::Set(target, Nan::New<String>("grabMouseClick").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(GrabMouseClick)).ToLocalChecked());
Nan::Set(target, Nan::New<String>("grabMouseMove").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(GrabMouseMove)).ToLocalChecked());
}

NODE_MODULE(nodeHook, Init)
23 changes: 23 additions & 0 deletions test/iohook-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const iohook = require('../iohook/index.js');

iohook.on('keydown', event => {
console.log(event);
});

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

console.log('start');
iohook.disableKeyboardPropagation();
iohook.disableMovePropagation();
iohook.disableClickPropagation();
iohook.start(true);

setTimeout(() => {
iohook.enableKeyboardPropagation();
iohook.enableMovePropagation();
iohook.enableClickPropagation();
iohook.stop();
console.log('end');
}, 3000)

0 comments on commit f446eee

Please sign in to comment.