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

bindsym for a single keypress #8148

Closed
jlo62 opened this issue May 5, 2024 · 1 comment
Closed

bindsym for a single keypress #8148

jlo62 opened this issue May 5, 2024 · 1 comment
Labels
enhancement New feature or incremental improvement

Comments

@jlo62
Copy link

jlo62 commented May 5, 2024

Hi,

It would be nice to be able to bindsym to a single keypress (like the way windows/kde uses to open it's launcher), instead of having to do this with a script.

I do this:

  • bind a no-op keycode for Super press/release
  • listen for the press-nop with a script
  • make sure no other keybind is pressed until the release-nop is triggered
  • add a 1 second timeout (for the case I simply forget my binds)

My current code, in case anyone is interested (it's rust):

use swayipc::{Connection, Fallible, EventType::Binding, Event};
use std::time::SystemTime;

fn main() -> Fallible<()> {
    let sway = Connection::new()?;
    let mut sway2 = Connection::new()?;

    let mut args = std::env::args();
   	let key: String = args.nth(1).unwrap();
    let mut exec: String = args.next().unwrap();
    for arg in args {
    	exec.push(' ');
    	exec.push_str(&arg)
    }
    let mut listen = false;
    let mut now = SystemTime::now();
        
    for event in sway.subscribe(vec![Binding])? {
    	if let Ok(Event::Binding(event)) = event {
    		if event.binding.command == format!("nop {}down", &key) {
    			listen = true;
    			now = SystemTime::now();
    		} else if event.binding.command == format!("nop {}up", &key) && listen && now.elapsed().unwrap().as_secs() < 1 {
				sway2.run_command(format!("exec {}", &exec))?;
       		} else {
    			listen = false;
    		}
    	}
    }
    Ok(())
}
@jlo62 jlo62 added the enhancement New feature or incremental improvement label May 5, 2024
@jlo62
Copy link
Author

jlo62 commented May 12, 2024

I got the solution from the hyprland wiki:

# Start wofi opens wofi on first press, closes it on second
bindr=SUPER, SUPER_L, exec, pkill wofi || wofi

and this works on sway too, as it is masked when a keybind is pressed before.

bindsym --no-repeat --release Super_L exec sirula

I will add this into the sway wiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or incremental improvement
Development

No branches or pull requests

1 participant