Skip to content

Commit

Permalink
Add keypress_delay_ms to workaround xremap#179
Browse files Browse the repository at this point in the history
  • Loading branch information
kzys committed Oct 11, 2022
1 parent 32011d6 commit c421866
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ keymap:
CapsLock-l: Right
```

### keypress_delay_ms

Some applications have trouble understanding synthesized key events, especially on
Wayland. `keypress_delay_ms` can be used to workaround the issue.
See [#179](https://github.com/k0kubun/xremap/issues/179) for the detail.

## License

`xremap` is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
2 changes: 2 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct Config {
pub default_mode: String,
#[serde(deserialize_with = "deserialize_virtual_modifiers", default = "Vec::new")]
pub virtual_modifiers: Vec<Key>,
#[serde(default)]
pub keypress_delay_ms: u64,

// Internals
#[serde(skip)]
Expand Down
8 changes: 6 additions & 2 deletions src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use nix::sys::timerfd::{Expiration, TimerFd, TimerSetTimeFlags};
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::process::{Command, Stdio};
use std::time::Instant;
use std::thread;
use std::time::{Duration, Instant};

pub struct EventHandler {
// Device to emit events
Expand Down Expand Up @@ -47,10 +48,11 @@ pub struct EventHandler {
mark_set: bool,
// { escape_next_key: true }
escape_next_key: bool,
keypress_delay: Duration,
}

impl EventHandler {
pub fn new(device: VirtualDevice, timer: TimerFd, mode: &str) -> EventHandler {
pub fn new(device: VirtualDevice, timer: TimerFd, mode: &str, keypress_delay: Duration) -> EventHandler {
EventHandler {
device,
modifiers: HashSet::new(),
Expand All @@ -66,6 +68,7 @@ impl EventHandler {
mode: mode.to_string(),
mark_set: false,
escape_next_key: false,
keypress_delay,
}
}

Expand Down Expand Up @@ -343,6 +346,7 @@ impl EventHandler {

// Press the main key
self.send_key(&key_press.key, PRESS)?;
thread::sleep(self.keypress_delay);
self.send_key(&key_press.key, RELEASE)?;

// Resurrect the original modifiers
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::collections::HashMap;
use std::io::stdout;
use std::os::unix::io::{AsRawFd, RawFd};
use std::path::{Path, PathBuf};
use std::time::Duration;

mod client;
mod config;
Expand Down Expand Up @@ -110,7 +111,8 @@ fn main() -> anyhow::Result<()> {
};
let timer = TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty())?;
let timer_fd = timer.as_raw_fd();
let mut handler = EventHandler::new(output_device, timer, &config.default_mode);
let delay = Duration::from_millis(config.keypress_delay_ms);
let mut handler = EventHandler::new(output_device, timer, &config.default_mode, delay);
let mut input_devices = match get_input_devices(&device_filter, &ignore_filter, mouse, watch_devices) {
Ok(input_devices) => input_devices,
Err(e) => bail!("Failed to prepare input devices: {}", e),
Expand Down

0 comments on commit c421866

Please sign in to comment.