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

Why are there 20 millisecond sleeps in the macOS keyboard functions? #105

Open
indianakernick opened this issue Jan 22, 2021 · 1 comment
Labels
macOS macOS specific question

Comments

@indianakernick
Copy link

I noticed a lot of latency and jitteriness. It turned out that key_click was taking 43 milliseconds to run! So I went to investigate and found that key_click sleeps for 20 milliseconds twice. See here.

Why is that the case? It's only the keyboard functions that do this and not the mouse functions. It seems unnecessary to me.

@indianakernick
Copy link
Author

I found out that the delays are there because it takes "a while" for modifier keys to take effect. So you need to wait a little bit after pressing shift before pressing 5 to get a %. I really can't stand the delay so I solved this by setting the flags on the key event.

This snippet will produce a keydown event with 5 (key code 23) and shift held down. So this will produce %. No annoying delay necessary.

let event = CGEvent::new_keyboard_event(self.event_source.clone(), 23, true).unwrap();
event.set_flags(CGEventFlags::CGEventFlagShift);
event.post(CGEventTapLocation::HID);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macOS macOS specific question
Projects
None yet
Development

No branches or pull requests

2 participants