Add mouse detection on Windows to improve word matches

This commit is contained in:
Federico Terzi 2020-03-08 20:59:57 +01:00
parent 5712c7fd61
commit c20b4728ba
3 changed files with 24 additions and 6 deletions

View File

@ -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;
}

View File

@ -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;
/*

View File

@ -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<KeyModifier> = 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();
}
}
}