这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/rdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ pub struct Event {
pub time: SystemTime,
pub name: Option<String>,
pub event_type: EventType,
#[cfg(target_os = "windows")]
pub extra_info: u32,
}

/// We can define a dummy Keyboard, that we will use to detect
Expand Down
97 changes: 74 additions & 23 deletions src/windows/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ pub unsafe fn get_scan_code(lpdata: LPARAM) -> DWORD {
kb.scanCode
}
}
pub unsafe fn get_key_extra_info(lpdata: LPARAM) -> DWORD {
unsafe {
let kb = *(lpdata as *const KBDLLHOOKSTRUCT);
kb.dwExtraInfo as u32
}
}
pub unsafe fn get_mouse_extra_info(lpdata: LPARAM) -> DWORD {
unsafe {
let mouse = *(lpdata as *const MSLLHOOKSTRUCT);
mouse.dwExtraInfo as u32
}
}
pub unsafe fn get_point(lpdata: LPARAM) -> (LONG, LONG) {
unsafe {
let mouse = *(lpdata as *const MSLLHOOKSTRUCT);
Expand All @@ -58,54 +70,93 @@ pub unsafe fn get_button_code(lpdata: LPARAM) -> WORD {
}
}

pub unsafe fn convert(param: WPARAM, lpdata: LPARAM) -> Option<EventType> {
pub unsafe fn convert(param: WPARAM, lpdata: LPARAM) -> Option<(EventType, u32)> {
unsafe {
match param.try_into() {
Ok(WM_KEYDOWN) | Ok(WM_SYSKEYDOWN) => {
let code = get_code(lpdata);
let key = key_from_code(code as u16);
Some(EventType::KeyPress(key))
let extra = get_key_extra_info(lpdata);
Some((EventType::KeyPress(key), extra))
}
Ok(WM_KEYUP) | Ok(WM_SYSKEYUP) => {
let code = get_code(lpdata);
let key = key_from_code(code as u16);
Some(EventType::KeyRelease(key))
let extra = get_key_extra_info(lpdata);
Some((EventType::KeyRelease(key), extra))
}
Ok(WM_LBUTTONDOWN) => Some(EventType::ButtonPress(Button::Left)),
Ok(WM_LBUTTONUP) => Some(EventType::ButtonRelease(Button::Left)),
Ok(WM_MBUTTONDOWN) => Some(EventType::ButtonPress(Button::Middle)),
Ok(WM_MBUTTONUP) => Some(EventType::ButtonRelease(Button::Middle)),
Ok(WM_RBUTTONDOWN) => Some(EventType::ButtonPress(Button::Right)),
Ok(WM_RBUTTONUP) => Some(EventType::ButtonRelease(Button::Right)),

Ok(WM_LBUTTONDOWN) => Some((
EventType::ButtonPress(Button::Left),
get_mouse_extra_info(lpdata),
)),
Ok(WM_LBUTTONUP) => Some((
EventType::ButtonRelease(Button::Left),
get_mouse_extra_info(lpdata),
)),
Ok(WM_MBUTTONDOWN) => Some((
EventType::ButtonPress(Button::Middle),
get_mouse_extra_info(lpdata),
)),
Ok(WM_MBUTTONUP) => Some((
EventType::ButtonRelease(Button::Middle),
get_mouse_extra_info(lpdata),
)),
Ok(WM_RBUTTONDOWN) => Some((
EventType::ButtonPress(Button::Right),
get_mouse_extra_info(lpdata),
)),
Ok(WM_RBUTTONUP) => Some((
EventType::ButtonRelease(Button::Right),
get_mouse_extra_info(lpdata),
)),

Ok(WM_XBUTTONDOWN) => {
let code = get_button_code(lpdata) as u8;
Some(EventType::ButtonPress(Button::Unknown(code)))
Some((
EventType::ButtonPress(Button::Unknown(code)),
get_mouse_extra_info(lpdata),
))
}
Ok(WM_XBUTTONUP) => {
let code = get_button_code(lpdata) as u8;
Some(EventType::ButtonRelease(Button::Unknown(code)))
Some((
EventType::ButtonRelease(Button::Unknown(code)),
get_mouse_extra_info(lpdata),
))
}

Ok(WM_MOUSEMOVE) => {
let (x, y) = get_point(lpdata);
Some(EventType::MouseMove {
x: x as f64,
y: y as f64,
})
Some((
EventType::MouseMove {
x: x as f64,
y: y as f64,
},
get_mouse_extra_info(lpdata),
))
}
Ok(WM_MOUSEWHEEL) => {
let delta = get_delta(lpdata) as c_short;
Some(EventType::Wheel {
delta_x: 0,
delta_y: (delta / WHEEL_DELTA) as i64,
})
Some((
EventType::Wheel {
delta_x: 0,
delta_y: (delta / WHEEL_DELTA) as i64,
},
get_mouse_extra_info(lpdata),
))
}
Ok(WM_MOUSEHWHEEL) => {
let delta = get_delta(lpdata) as c_short;
Some(EventType::Wheel {
delta_x: (delta / WHEEL_DELTA) as i64,
delta_y: 0,
})
Some((
EventType::Wheel {
delta_x: (delta / WHEEL_DELTA) as i64,
delta_y: 0,
},
get_mouse_extra_info(lpdata),
))
}

_ => None,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/windows/grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ unsafe extern "system" fn raw_callback(code: i32, param: usize, lpdata: isize) -
unsafe {
if code == HC_ACTION {
let opt = convert(param, lpdata);
if let Some(event_type) = opt {
if let Some((event_type, extra_info)) = opt {
let name = match &event_type {
EventType::KeyPress(_key) => match (*KEYBOARD).lock() {
Ok(mut keyboard) => keyboard.get_name(lpdata),
Expand All @@ -22,6 +22,7 @@ unsafe extern "system" fn raw_callback(code: i32, param: usize, lpdata: isize) -
event_type,
time: SystemTime::now(),
name,
extra_info,
};
let ptr = &raw mut GLOBAL_CALLBACK;
if let Some(callback) = &mut *ptr {
Expand Down
3 changes: 2 additions & 1 deletion src/windows/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ unsafe extern "system" fn raw_callback(code: c_int, param: WPARAM, lpdata: LPARA
unsafe {
if code == HC_ACTION {
let opt = convert(param, lpdata);
if let Some(event_type) = opt {
if let Some((event_type, extra_info)) = opt {
let name = match &event_type {
EventType::KeyPress(_key) => match (*KEYBOARD).lock() {
Ok(mut keyboard) => keyboard.get_name(lpdata),
Expand All @@ -33,6 +33,7 @@ unsafe extern "system" fn raw_callback(code: c_int, param: WPARAM, lpdata: LPARA
event_type,
time: SystemTime::now(),
name,
extra_info,
};
let ptr = &raw mut GLOBAL_CALLBACK;
if let Some(callback) = &mut *ptr {
Expand Down