From b6b36fb07f5f8f6bfab55be4ecf626d7975e7503 Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 12 Sep 2023 07:35:26 -0400 Subject: [PATCH] Include scan code when simulating keyboard events on Windows --- src/windows/keycodes.rs | 12 ++++++++++++ src/windows/simulate.rs | 8 +++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/windows/keycodes.rs b/src/windows/keycodes.rs index c3707f76..f718b7d6 100644 --- a/src/windows/keycodes.rs +++ b/src/windows/keycodes.rs @@ -1,6 +1,18 @@ use crate::rdev::Key; use std::convert::TryInto; use winapi::shared::minwindef::WORD; +use winapi::um::winuser::{MapVirtualKeyA, MAPVK_VK_TO_VSC}; + +pub fn scan_from_code(code: WORD) -> Option { + let scan = unsafe { MapVirtualKeyA(code as u32, MAPVK_VK_TO_VSC) } as WORD; + + if scan != 0 { + Some(scan) + } + else { + None + } +} macro_rules! decl_keycodes { ($($key:ident, $code:literal),*) => { diff --git a/src/windows/simulate.rs b/src/windows/simulate.rs index cd62e220..8b366fdd 100644 --- a/src/windows/simulate.rs +++ b/src/windows/simulate.rs @@ -1,5 +1,5 @@ use crate::rdev::{Button, EventType, SimulateError}; -use crate::windows::keycodes::code_from_key; +use crate::windows::keycodes::{code_from_key, scan_from_code}; use std::convert::TryFrom; use std::mem::size_of; use winapi::ctypes::{c_int, c_short}; @@ -77,11 +77,13 @@ pub fn simulate(event_type: &EventType) -> Result<(), SimulateError> { match event_type { EventType::KeyPress(key) => { let code = code_from_key(*key).ok_or(SimulateError)?; - sim_keyboard_event(KEYEVENTF_KEYDOWN, code, 0) + let scan = scan_from_code(code).ok_or(SimulateError)?; + sim_keyboard_event(KEYEVENTF_KEYDOWN, code, scan) } EventType::KeyRelease(key) => { let code = code_from_key(*key).ok_or(SimulateError)?; - sim_keyboard_event(KEYEVENTF_KEYUP, code, 0) + let scan = scan_from_code(code).ok_or(SimulateError)?; + sim_keyboard_event(KEYEVENTF_KEYUP, code, scan) } EventType::ButtonPress(button) => match button { Button::Left => sim_mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0),