Skip to content

Commit

Permalink
add keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpadberg committed Oct 12, 2022
1 parent b6ac8fc commit 9ebac95
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ka"
version = "0.6.0"
version = "0.7.0"
edition = "2021"
authors = ["mdpadberg <[email protected]>"]

Expand All @@ -13,4 +13,5 @@ rand = "0.8.5"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
dirs = "4.0.0"
anyhow = "1.0.62"
anyhow = "1.0.62"
enigo = "0.0.14"
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Go to the release page and download the latest release: https://github.com/mdpad
## Examples
```console
% ka -h
ka 0.5.0
ka 0.7.0

USAGE:
ka [SUBCOMMAND]
Expand All @@ -29,8 +29,10 @@ OPTIONS:
-V, --version Print version information

SUBCOMMANDS:
help Print this message or the help of the given subcommand(s)
mouse Use mouse to keep your machine awake
help Print this message or the help of the given subcommand(s)
keyboard Use keyboard to keep your machine awake (default tab & windows/super/command
mouse Use mouse to keep your machine awake
programname Set name for this program

```

Expand All @@ -48,4 +50,33 @@ OPTIONS:
--interval <interval> set a time interval in seconds on how often you want to run this
[default: 60]
--width <width> set your monitors width in pixels [default: 1920]
```

### Subcommand: Keyboard
Use keyboard to keep your machine awake [default tab & windows/super/command]

```console
% ka keyboard -h
USAGE:
ka keyboard [OPTIONS]

OPTIONS:
-h, --help Print help information
--interval <interval> set a time interval in seconds on how often you want to run this
[default: 60]
```

### Subcommand: Programname
Set name for this program

```console
% ka programname -h
USAGE:
ka programname <name>

ARGS:
<name> Set name for this program

OPTIONS:
-h, --help Print help information
```
11 changes: 8 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{mouse::Mouse, programname::ProgramName, settings::Settings};
use crate::{keyboard::Keyboard, mouse::Mouse, programname::ProgramName, settings::Settings};
use clap::{ArgMatches, Command};

pub trait MySubCommand {
Expand All @@ -14,18 +14,23 @@ pub fn parse() {
} else {
String::from("ka")
};

let matches = Command::new(&application_name)
.bin_name(&application_name)
.version(env!("CARGO_PKG_VERSION"))
.about("Keep you machine awake")
.arg_required_else_help(true)
.subcommands(vec![Mouse::command(), ProgramName::command()])
.subcommands(vec![
Mouse::command(),
ProgramName::command(),
Keyboard::command(),
])
.get_matches();

match matches.subcommand() {
Some((Mouse::NAME, args)) => Mouse::action(args),
Some((ProgramName::NAME, args)) => ProgramName::action(args),
Some((Keyboard::NAME, args)) => Keyboard::action(args),
_ => unreachable!(),
}
}
45 changes: 44 additions & 1 deletion src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
//TODO
use std::{thread, time::Duration};

use crate::cli::MySubCommand;
use clap::{Arg, Command};
use enigo::{Enigo, Key, KeyboardControllable};

pub struct Keyboard;

impl MySubCommand for Keyboard {
const NAME: &'static str = "keyboard";

fn command() -> clap::Command<'static> {
Command::new(Keyboard::NAME)
.about("Use keyboard to keep your machine awake [default tab & windows/super/command]")
.arg(
Arg::new("interval")
.help("set a time interval in seconds on how often you want to run this")
.long("interval")
.takes_value(true)
.default_value("60"),
)
}

fn action(args: &clap::ArgMatches) {
let interval = args.get_one::<String>("interval").unwrap();
let mut enigo = Enigo::new();
if let Ok(interval) = interval.parse::<u64>() {
loop {
thread::sleep(Duration::from_secs(interval));
enigo.key_down(Key::Meta);
thread::sleep(Duration::from_millis(200));
enigo.key_down(Key::Tab);
thread::sleep(Duration::from_millis(200));
enigo.key_up(Key::Tab);
thread::sleep(Duration::from_millis(200));
enigo.key_up(Key::Meta);
}
} else {
eprintln!(
"Could not parse interval:{}. Did you configure a valid number?",
interval
);
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod cli;
mod mouse;
mod settings;
mod programname;
mod keyboard;

fn main() {
cli::parse();
Expand Down

0 comments on commit 9ebac95

Please sign in to comment.