Skip to content

Commit f446eee

Browse files
committed
Mouse move propagation enabler/disabler
- iohook.enableMovePropagation(); - iohook.disableMovePropagation(); - Tested on MacOS
1 parent fc25e6f commit f446eee

File tree

7 files changed

+70
-3
lines changed

7 files changed

+70
-3
lines changed

index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ declare class IOHook extends EventEmitter {
5757
*/
5858
disableClickPropagation(): void
5959

60+
enableMovePropagation(): void
61+
disableMovePropagation(): void
62+
6063
/**
6164
* Register global shortcut. When all keys in keys array pressed, callback will be called
6265
* @param {Array<string|number>} keys Array of keycodes

index.js

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class IOHook extends EventEmitter {
5353
stop() {
5454
this.enableClickPropagation();
5555
this.enableKeyboardPropagation();
56+
this.enableMovePropagation();
5657
if (this.active) {
5758
this.active = false;
5859
}
@@ -145,6 +146,16 @@ class IOHook extends EventEmitter {
145146
NodeHookAddon.grabMouseClick(false);
146147
}
147148

149+
/**
150+
* disable/enable mouse move propagation.
151+
*/
152+
153+
disableMovePropagation() {
154+
NodeHookAddon.grabMouseMove(true);
155+
}
156+
enableMovePropagation() {
157+
NodeHookAddon.grabMouseMove(false);
158+
}
148159
/**
149160
* Local event handler. Don't use it in your code!
150161
* @param msg Raw event message

libuiohook/include/uiohook.h

+1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ extern "C" {
430430

431431
UIOHOOK_API void grab_keyboard(bool enable);
432432
UIOHOOK_API void grab_mouse_click(bool enable);
433+
UIOHOOK_API void grab_mouse_move(bool enable);
433434

434435
// Retrieves an array of screen data for each available monitor.
435436
UIOHOOK_API screen_data* hook_create_screen_info(unsigned char *count);

libuiohook/src/darwin/input_hook.c

+20-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ static dispatcher_t dispatcher = NULL;
102102

103103
static unsigned short int grab_keyboard_event = 0x00;
104104
static unsigned short int grab_mouse_click_event = 0x00;
105+
static unsigned short int grab_mouse_move_event = 0x00;
105106

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

878879
// Populate mouse motion event.
879880
event.time = timestamp;
880-
event.reserved = 0x00;
881+
event.reserved = grab_mouse_move_event;
881882

882883
if (mouse_dragged) {
883884
event.type = EVENT_MOUSE_DRAGGED;
@@ -1071,10 +1072,19 @@ CGEventRef hook_event_proc(CGEventTapProxy tap_proxy, CGEventType type, CGEventR
10711072
case kCGEventMouseMoved:
10721073
// Set the mouse dragged flag.
10731074
mouse_dragged = false;
1075+
// CGDisplayHideCursor(kCGDirectMainDisplay);;
1076+
// CGDisplayShowCursor(kCGDirectMainDisplay);;
10741077
process_mouse_moved(timestamp, event_ref);
1078+
if (event.reserved) {
1079+
int32_t deltaX, deltaY;
1080+
CGGetLastMouseDelta(&deltaX, &deltaY);
1081+
CGPoint originalPoint;
1082+
originalPoint.x = event.data.mouse.x - deltaX;
1083+
originalPoint.y = event.data.mouse.y - deltaY;
1084+
CGWarpMouseCursorPosition(originalPoint);
1085+
}
10751086
break;
10761087

1077-
10781088
case kCGEventScrollWheel:
10791089
process_mouse_wheel(timestamp, event_ref);
10801090
break;
@@ -1126,6 +1136,14 @@ UIOHOOK_API void grab_mouse_click(bool enabled) {
11261136
}
11271137
}
11281138

1139+
UIOHOOK_API void grab_mouse_move(bool enabled) {
1140+
grab_mouse_move_event = enabled;
1141+
CGDirectDisplayID displays[2];
1142+
uint32_t dcnt;
1143+
CGGetActiveDisplayList(2, displays, &dcnt);
1144+
logger(LOG_LEVEL_DEBUG, "%s [%u]: get displays. (%d) (%d)\n",
1145+
__FUNCTION__, __LINE__, dcnt, dcnt);
1146+
}
11291147
UIOHOOK_API void grab_keyboard(bool enabled){
11301148
grab_keyboard_event = enabled;
11311149
}

libuiohook/src/windows/input_hook.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static uiohook_event event;
5151
static dispatcher_t dispatcher = NULL;
5252

5353
static unsigned short int grab_mouse_click_event = 0x00;
54+
static unsigned short int grab_mouse_move_event = 0x00;
5455
static unsigned short int grab_keyboard_event = 0x00;
5556

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

441442
// Populate mouse move event.
442443
event.time = timestamp;
443-
event.reserved = 0x00;
444+
event.reserved = grab_mouse_move_event;
444445

445446
event.mask = get_modifiers();
446447

@@ -673,6 +674,9 @@ void CALLBACK win_hook_event_proc(HWINEVENTHOOK hook, DWORD event, HWND hWnd, LO
673674
UIOHOOK_API void grab_keyboard(bool enabled) {
674675
grab_keyboard_event = enabled;
675676
}
677+
UIOHOOK_API void grab_mouse_move(bool enabled) {
678+
grab_mouse_move_event = enabled;
679+
}
676680
UIOHOOK_API void grab_mouse_click(bool enabled) {
677681
if (enabled) {
678682
grab_mouse_click_event = 0x01;

src/iohook.cc

+7
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ NAN_METHOD(GrabMouseClick) {
516516
grab_mouse_click(info[0]->IsTrue());
517517
}
518518
}
519+
NAN_METHOD(GrabMouseMove) {
520+
if (info.Length() > 0) {
521+
grab_mouse_move(info[0]->IsTrue());
522+
}
523+
}
519524

520525
NAN_METHOD(DebugEnable) {
521526
if (info.Length() > 0)
@@ -570,6 +575,8 @@ NAN_MODULE_INIT(Init) {
570575
Nan::GetFunction(Nan::New<FunctionTemplate>(GrabKeyboard)).ToLocalChecked());
571576
Nan::Set(target, Nan::New<String>("grabMouseClick").ToLocalChecked(),
572577
Nan::GetFunction(Nan::New<FunctionTemplate>(GrabMouseClick)).ToLocalChecked());
578+
Nan::Set(target, Nan::New<String>("grabMouseMove").ToLocalChecked(),
579+
Nan::GetFunction(Nan::New<FunctionTemplate>(GrabMouseMove)).ToLocalChecked());
573580
}
574581

575582
NODE_MODULE(nodeHook, Init)

test/iohook-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const iohook = require('../iohook/index.js');
2+
3+
iohook.on('keydown', event => {
4+
console.log(event);
5+
});
6+
7+
iohook.on('mousemove', event => {
8+
console.log(event);
9+
});
10+
11+
console.log('start');
12+
iohook.disableKeyboardPropagation();
13+
iohook.disableMovePropagation();
14+
iohook.disableClickPropagation();
15+
iohook.start(true);
16+
17+
setTimeout(() => {
18+
iohook.enableKeyboardPropagation();
19+
iohook.enableMovePropagation();
20+
iohook.enableClickPropagation();
21+
iohook.stop();
22+
console.log('end');
23+
}, 3000)

0 commit comments

Comments
 (0)