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

Segmentation fault on keypress (OSX) #74

Open
red010182 opened this issue Jan 28, 2022 · 4 comments
Open

Segmentation fault on keypress (OSX) #74

red010182 opened this issue Jan 28, 2022 · 4 comments

Comments

@red010182
Copy link

red010182 commented Jan 28, 2022

OS: OSX (Terminal.app has been add to Accessibility->Privacy)

use rdev::{listen, Event};


fn main() {
  // This will block.
  if let Err(error) = listen(callback) {
      println!("Error: {:?}", error)
  }

  fn callback(event: Event) {
      println!("My callback {:?}", event);
      match event.name {
          Some(string) => println!("User wrote {:?}", string),
          None => (),
      }
  }

}
cargo run

Mouse move & click are succesfully listened but whenever I press any key the program exist with

Segmentation fault: 11
@red010182 red010182 changed the title Segmentation fault Segmentation fault on OSX Jan 28, 2022
@red010182 red010182 changed the title Segmentation fault on OSX Segmentation fault on keypress (OSX) Jan 28, 2022
@skyslide22
Copy link

skyslide22 commented Jul 11, 2022

i got the same issue, but only if the listen(callback) is invoked in a new thread to make it not blocking (tauri framework)
also, i am on osx.

vscode, terminal & app are allowed to listen to keyboard events and so on, i have checked the entire osx settings list

fn create_device_query_listener(handle: tauri::AppHandle) {
    
    
    //! keyboard key press will freeze, if run in another thread
    std::thread::spawn(move || {
        let mut alt_pressed = false;
        let mut tab_pressed = false;
        let mut coords_on_open = Vector2::new(0.0, 0.0);
        let mut coords_current = Vector2::new(0.0, 0.0);
        let mut win_visible = false;

        let callback = move |event: Event| {
                // println!("My callback {:?}", event);
                // return ;
            match event.event_type {
                KeyRelease(key) => {
                    if key == Alt {
                        alt_pressed = false;
                    } else if key == Tab {
                        tab_pressed = false;
                    }
                }
                KeyPress(key) => {
                    if key == Alt {
                        alt_pressed = true;
                    } else if key == Tab {
                        tab_pressed = true;
                    }
                }
                MouseMove { x, y } => {
                    coords_current = Vector2::new(x as f32, y as f32);
                    println!("mouse: x {} y {}", x , y);
                }
                _ => println!("event ignored")
            }

            // {
            //     println!("alt: {}, tab: {}", alt_pressed, tab_pressed);
            // }
            if alt_pressed && tab_pressed {

                println!("alt tab pressed");
                if win_visible == false {
                    win_visible = true;
                    coords_on_open = coords_current.clone();
                    show_window(&handle, &coords_on_open)
                }

                let angle = get_angle(&coords_on_open, &coords_current);
                let distance = get_distance(&coords_on_open, &coords_current);

                transfer_angle_distance_to_window(&handle, &angle, &distance);
            } else {
                win_visible = false;
            }
        };

        if let Err(error) = listen(callback) {
            println!("Error: {:?}", error)
        }
    });
}

@xiyanZ
Copy link

xiyanZ commented Feb 11, 2023

I got the same issue

i got the same issue, but only if the listen(callback) is invoked in a new thread to make it not blocking (tauri framework) also, i am on osx.

vscode, terminal & app are allowed to listen to keyboard events and so on, i have checked the entire osx settings list

fn create_device_query_listener(handle: tauri::AppHandle) {
    
    
    //! keyboard key press will freeze, if run in another thread
    std::thread::spawn(move || {
        let mut alt_pressed = false;
        let mut tab_pressed = false;
        let mut coords_on_open = Vector2::new(0.0, 0.0);
        let mut coords_current = Vector2::new(0.0, 0.0);
        let mut win_visible = false;

        let callback = move |event: Event| {
                // println!("My callback {:?}", event);
                // return ;
            match event.event_type {
                KeyRelease(key) => {
                    if key == Alt {
                        alt_pressed = false;
                    } else if key == Tab {
                        tab_pressed = false;
                    }
                }
                KeyPress(key) => {
                    if key == Alt {
                        alt_pressed = true;
                    } else if key == Tab {
                        tab_pressed = true;
                    }
                }
                MouseMove { x, y } => {
                    coords_current = Vector2::new(x as f32, y as f32);
                    println!("mouse: x {} y {}", x , y);
                }
                _ => println!("event ignored")
            }

            // {
            //     println!("alt: {}, tab: {}", alt_pressed, tab_pressed);
            // }
            if alt_pressed && tab_pressed {

                println!("alt tab pressed");
                if win_visible == false {
                    win_visible = true;
                    coords_on_open = coords_current.clone();
                    show_window(&handle, &coords_on_open)
                }

                let angle = get_angle(&coords_on_open, &coords_current);
                let distance = get_distance(&coords_on_open, &coords_current);

                transfer_angle_distance_to_window(&handle, &angle, &distance);
            } else {
                win_visible = false;
            }
        };

        if let Err(error) = listen(callback) {
            println!("Error: {:?}", error)
        }
    });
}

this is the code

  app.run(|app_handler, run_event| {
        tauri::async_runtime::spawn(async move {
            listen(move |event| match event.event_type {
                EventType::KeyPress(Key::Alt) => {
                    println!("alt")
                }
                _ => {}
            })
            .expect("error");
        });
    });

@karimelhajoui63
Copy link

I had the same issue too (tauri / MacOS / segfault on keypress)
But this fixed the problem ✅

(@Narsil, maybe you should consider pull back the changes made on this fork ?)

@medzernik
Copy link

Please do pull the changes if possible, it would be very helpful

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

5 participants