Skip to content

CGEvent set_flags does not work randomly #721

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

Open
Tracked by #719
ChenghaoQ opened this issue Feb 13, 2025 · 1 comment
Open
Tracked by #719

CGEvent set_flags does not work randomly #721

ChenghaoQ opened this issue Feb 13, 2025 · 1 comment

Comments

@ChenghaoQ
Copy link

ChenghaoQ commented Feb 13, 2025

I found sometimes the CGEvent.set_flags does not set flags correctly


 fn set_modifier_flag(&mut self, key: &String, key_event: CGEvent, press: bool) -> CGEvent {

        if self.modifier_map.contains_key(key) {
            let event_flag: &CGEventFlags = self.modifier_map.get(key).unwrap();
            if press {
                self.flag_holder.lock().unwrap().insert(*event_flag);
                key_event.set_flags(*self.flag_holder.lock().unwrap());
                
            } else {
                self.flag_holder.lock().unwrap().remove(*event_flag);
                key_event.set_flags(*self.flag_holder.lock().unwrap());
            }
        }

        // the code below is to make sure the key_event flags had been set correctly
        let mut repeat_setter_counter = 0;
        while key_event.get_flags() != *self.flag_holder.lock().unwrap() && repeat_setter_counter < 10 {
            key_event.set_flags(*self.flag_holder.lock().unwrap());
            repeat_setter_counter += 1;
            std::thread::sleep(std::time::Duration::from_millis(1));
        }

        key_event
    }
@madsmtm
Copy link
Contributor

madsmtm commented Mar 21, 2025

This doesn't seem like an issue with core-graphics, CGEvent::set_flags simply calls into CGEventSetFlags.

It's not entirely clear to me what your issue with it is, but perhaps submit this as feedback to Apple instead?

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

No branches or pull requests

2 participants