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

provide stop_listen function for user to stop listen in another thread #137

Closed
wants to merge 3 commits into from
Closed

provide stop_listen function for user to stop listen in another thread #137

wants to merge 3 commits into from

Conversation

qzd1989
Copy link

@qzd1989 qzd1989 commented Jun 30, 2024

(it works on macos and windows now)
I'm not sure if it is correct of stopping listen on macos and windows, this is my first time to contribute code, so please check them carefully.
really hope to help users. :)

on windows:
I have tried many ways to stop PostMessageA, but failed. (PostMessageA, PostMessageW, PostQuitMessage) don't work.
so I use PeekMessageA instead PostMessageA to receive msg and use a channel to send stopping msg signal in the loop to break it.

//stop listen example, only works on macos and windows
use std::{
    thread::{self, spawn},
    time::Duration,
};

use rdev::{listen, stop_listen, Event};

fn main() {
    spawn(|| {
        if let Err(error) = listen(callback) {
            println!("Error: {:?}", error)
        }
    });
    thread::sleep(Duration::from_secs(5));
    spawn(|| stop_listen());
    println!("listen loop is stopped");
    thread::sleep(Duration::from_secs(5));
    spawn(|| {
        if let Err(error) = listen(callback) {
            println!("Error: {:?}", error)
        }
    });
    thread::sleep(Duration::from_secs(5));
    spawn(|| stop_listen());
    println!("listen loop is stopped again");
    thread::sleep(Duration::from_secs(3600));
}

fn callback(event: Event) {
    println!("My callback {:?}", event);
}

@Z1xus
Copy link

Z1xus commented Jul 1, 2024

need this 👍

@qzd1989 qzd1989 closed this by deleting the head repository Jul 2, 2024
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

Successfully merging this pull request may close these issues.

2 participants