From c20b4728ba7fb04edecf69bf12d179bb7c4ded1f Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 8 Mar 2020 20:59:57 +0100 Subject: [PATCH] Add mouse detection on Windows to improve word matches --- native/libwinbridge/bridge.cpp | 15 +++++++++++++-- native/libwinbridge/bridge.h | 2 +- src/context/windows.rs | 13 ++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/native/libwinbridge/bridge.cpp b/native/libwinbridge/bridge.cpp index 0621a5a..7d6d3df 100644 --- a/native/libwinbridge/bridge.cpp +++ b/native/libwinbridge/bridge.cpp @@ -263,6 +263,12 @@ LRESULT CALLBACK window_procedure(HWND window, unsigned int msg, WPARAM wp, LPAR keypress_callback(manager_instance, nullptr, 0, 1, raw->data.keyboard.VKey, is_key_down); } } + }else if (raw->header.dwType == RIM_TYPEMOUSE) // Mouse input, registered as "other" events. Needed to improve the reliability of word matches + { + if ((raw->data.mouse.usButtonFlags & (RI_MOUSE_LEFT_BUTTON_DOWN | RI_MOUSE_RIGHT_BUTTON_DOWN | RI_MOUSE_MIDDLE_BUTTON_DOWN)) != 0) { + //std::cout << "mouse down" << std::endl; + keypress_callback(manager_instance, nullptr, 0, 2, raw->data.mouse.usButtonFlags, 0); + } } return 0; @@ -343,14 +349,19 @@ int32_t initialize(void * self, wchar_t * ico_path, wchar_t * bmp_path) { ); // Register raw inputs - RAWINPUTDEVICE Rid[1]; + RAWINPUTDEVICE Rid[2]; Rid[0].usUsagePage = 0x01; Rid[0].usUsage = 0x06; Rid[0].dwFlags = RIDEV_NOLEGACY | RIDEV_INPUTSINK; // adds HID keyboard and also ignores legacy keyboard messages Rid[0].hwndTarget = window; - if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) { // Something went wrong, error. + Rid[1].usUsagePage = 0x01; + Rid[1].usUsage = 0x02; + Rid[1].dwFlags = RIDEV_NOLEGACY | RIDEV_INPUTSINK; // adds HID mouse and also ignores legacy mouse messages + Rid[1].hwndTarget = window; + + if (RegisterRawInputDevices(Rid, 2, sizeof(Rid[0])) == FALSE) { // Something went wrong, error. return -1; } diff --git a/native/libwinbridge/bridge.h b/native/libwinbridge/bridge.h index a5f1d89..40ed1e6 100644 --- a/native/libwinbridge/bridge.h +++ b/native/libwinbridge/bridge.h @@ -39,7 +39,7 @@ extern "C" int32_t initialize(void * self, wchar_t * ico_path, wchar_t * bmp_pat * Called when a new keypress is made, the first argument is an int array, * while the second is the size of the array. */ -typedef void (*KeypressCallback)(void * self, uint16_t *buffer, int32_t len, int32_t is_modifier, int32_t key_code, int32_t is_key_down); +typedef void (*KeypressCallback)(void * self, uint16_t *buffer, int32_t len, int32_t event_type, int32_t key_code, int32_t is_key_down); extern KeypressCallback keypress_callback; /* diff --git a/src/context/windows.rs b/src/context/windows.rs index a0cf529..ab15201 100644 --- a/src/context/windows.rs +++ b/src/context/windows.rs @@ -108,7 +108,7 @@ impl super::Context for WindowsContext { // Native bridge code extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32, - is_modifier: i32, key_code: i32, is_key_down: i32) { + event_type: i32, key_code: i32, is_key_down: i32) { unsafe { let _self = _self as *mut WindowsContext; @@ -121,7 +121,7 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32 } if is_key_down != 0 { // KEY DOWN EVENT - if is_modifier == 0 { // Char event + if event_type == 0 { // Char event // Convert the received buffer to a string let buffer = std::slice::from_raw_parts(raw_buffer, len as usize); let c_string = U16CStr::from_slice_with_nul(buffer); @@ -144,7 +144,7 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32 } } }else{ // KEY UP event - if is_modifier != 0 { // Modifier event + if event_type == 1 { // Modifier event let modifier: Option = match key_code { 0x5B | 0x5C => Some(META), 0x10 => Some(SHIFT), @@ -157,7 +157,14 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32 if let Some(modifier) = modifier { let event = Event::Key(KeyEvent::Modifier(modifier)); (*_self).send_channel.send(event).unwrap(); + }else{ // Not one of the default modifiers, send an "other" event + let event = Event::Key(KeyEvent::Other); + (*_self).send_channel.send(event).unwrap(); } + }else{ + // Other type of event + let event = Event::Key(KeyEvent::Other); + (*_self).send_channel.send(event).unwrap(); } } }