Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include scan code when simulating keyboard events on Windows #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions src/windows/keycodes.rs
Original file line number Diff line number Diff line change
@@ -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<WORD> {
let scan = unsafe { MapVirtualKeyA(code as u32, MAPVK_VK_TO_VSC) } as WORD;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be either MapVirtualKey or MapVirtualKeyW instead (source)


if scan != 0 {
Some(scan)
}
else {
None
}
}

macro_rules! decl_keycodes {
($($key:ident, $code:literal),*) => {
Expand Down
8 changes: 5 additions & 3 deletions src/windows/simulate.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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),
Expand Down
Loading