Skip to content

Commit

Permalink
Adding support for relative mouse movement (Bug #91)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhat committed Dec 6, 2023
1 parent 0635b69 commit 6dcde72
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/uiohook.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ typedef struct _mouse_event_data {
uint16_t clicks;
int16_t x;
int16_t y;
int16_t dx;
int16_t dy;
} mouse_event_data,
mouse_pressed_event_data,
mouse_released_event_data,
Expand Down
6 changes: 6 additions & 0 deletions src/darwin/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ bool dispatch_mouse_move(uint64_t timestamp, CGEventRef event_ref) {

CGPoint event_point = CGEventGetLocation(event_ref);


logger(LOG_LEVEL_WARN, "#### %s [%u]: Mouse %s to %i, %i.\n",
__FUNCTION__, __LINE__, is_mouse_dragged() ? "dragged" : "moved",
CGEventGetIntegerValueField(event_ref, kCGMouseEventDeltaX),
CGEventGetIntegerValueField(event_ref, kCGMouseEventDeltaY));

// Populate mouse motion event.
uio_event.time = timestamp;
uio_event.reserved = CGEventGetIntegerValueField(event_ref, kCGEventSourceUnixProcessID) ? 0x02 : 0x00;
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/input_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static int create_event_runloop_info(event_runloop_info **hook) {

// Create the event tap.
(*hook)->port = CGEventTapCreate(
kCGSessionEventTap, // kCGHIDEventTap
kCGSessionEventTap, // kCGHIDEventTap requires privileges?
kCGHeadInsertEventTap, // kCGTailAppendEventTap
kCGEventTapOptionDefault, // kCGEventTapOptionListenOnly See https://github.com/kwhat/jnativehook/issues/22
event_mask,
Expand Down
13 changes: 12 additions & 1 deletion src/windows/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static unsigned short click_count = 0;
static uint64_t click_time = 0;
static unsigned short int click_button = MOUSE_NOBUTTON;
static POINT last_click;
static POINT last_moved;

// Event dispatch callback.
static dispatcher_t dispatch = NULL;
Expand Down Expand Up @@ -80,6 +81,11 @@ bool dispatch_hook_enable() {
// Initialize native input helper functions.
load_input_helper();

if (!GetCursorPos(&last_moved)) {
logger(LOG_LEVEL_WARN, "%s [%u]: Failed to locate cursor position!\n",
__FUNCTION__, __LINE__);
}

// Get the local system time in UNIX epoch form.
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
Expand Down Expand Up @@ -361,7 +367,6 @@ bool dispatch_button_release(MSLLHOOKSTRUCT *mshook, uint16_t button) {
return consumed;
}


bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook) {
bool consumed = false;
#ifdef USE_EPOCH_TIME
Expand Down Expand Up @@ -399,6 +404,12 @@ bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook) {
uio_event.data.mouse.x = (int16_t) mshook->pt.x;
uio_event.data.mouse.y = (int16_t) mshook->pt.y;

logger(LOG_LEVEL_WARN, "#### %s [%u]: Mouse %s to %i, %i.\n",
__FUNCTION__, __LINE__,
mouse_dragged ? "dragged" : "moved",
last_moved.x - mshook->pt.x, last_moved.y - mshook->pt.y);
last_moved = mshook->pt;

logger(LOG_LEVEL_DEBUG, "%s [%u]: Mouse %s to %u, %u.\n",
__FUNCTION__, __LINE__,
mouse_dragged ? "dragged" : "moved",
Expand Down

0 comments on commit 6dcde72

Please sign in to comment.