Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 committed Mar 30, 2019
0 parents commit 52d9f09
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
**/*.rs.bk
rust-toolchain
Cargo.lock
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "keybind"
description = "Trigger logic on specific keybind."
version = "0.1.0"
license = "MIT"
authors = ["kunicmarko20 <kunicmarko20@gmail.com>"]
edition = "2018"
repository = "https://github.com/rustysoft/keybind"

[dependencies]
device_query = "0.1.1"
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2019

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Keybind
=======

Wrapper around [device_query](https://github.com/ostrosco/device_query) providing a nicer API, allowing you to trigger
your logic on specific keybind.

Full Documentation can be read [here](https://docs.rs/keybind/latest/keybind/).

```rust
use keybind::{Keybind, Keycode};

fn main() {
let mut keybind = Keybind::new(&[Keycode::LControl, Keycode::G]);

keybind.on_trigger(|| {
println!("This will be printed when you press CTRL+G");
});

keybind.wait();
}
```
11 changes: 11 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use keybind::{Keybind, Keycode};

fn main() {
let mut keybind = Keybind::new(&[Keycode::LControl, Keycode::G]);

keybind.on_trigger(|| {
println!("This will be printed when you press CTRL+G");
});

keybind.wait();
}
121 changes: 121 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//! # Keybind
//!
//! Wrapper around [device_query](https://github.com/ostrosco/device_query) providing a nicer API, allowing you to trigger
//! your logic on specific keybind.
//!
//! # Example
//!
//! ```ignore
//! use keybind::{Keybind, Keycode};
//!
//!fn main() {
//! let mut keybind = Keybind::new(&[Keycode::LControl, Keycode::G]);
//!
//! keybind.on_trigger(|| {
//! println!("This will be printed when you press CTRL+G");
//! });
//!
//! keybind.wait();
//!}
//! ```
//!
use device_query::{DeviceQuery, DeviceState};
use std::mem;

pub use device_query::Keycode;

pub struct Keybind {
device_state: DeviceState,
pressed_keys: Vec<Keycode>,
key_binds: Vec<Keycode>,
on_trigger: Box<Fn()>,
}

impl Keybind {
/// Constructs a new `Keybind`.
///
/// # Example
///
/// ```ignore
/// use keybind::{Keybind, Keycode};
///
/// let mut keybind = Keybind::new(&[Keycode::LControl, Keycode::G]);
/// ```
pub fn new(keys: &[Keycode]) -> Keybind {
Keybind {
device_state: DeviceState::new(),
pressed_keys: Vec::new(),
key_binds: keys.to_vec(),
on_trigger: Box::new(||{})
}
}

/// Returns bool if the specific keybind has been triggered
///
/// # Example
///
/// ```ignore
/// use keybind::{Keybind, Keycode};
///
/// let mut keybind = Keybind::new(&[Keycode::LControl, Keycode::G]);
///
/// loop {
/// if self.triggered() {
/// println!("triggered");
/// }
/// }
/// ```
pub fn triggered(&mut self) -> bool {
let previous_pressed_keys = mem::replace(
&mut self.pressed_keys,
self.device_state.get_keys()
);

self.pressed_keys.len() == self.key_binds.len()
&& previous_pressed_keys != self.pressed_keys
&& self.pressed_keys == self.key_binds
}

/// Sets provided callback that will be executed on trigger.
///
/// # Example
///
/// ```ignore
/// use keybind::{Keybind, Keycode};
///
/// let mut keybind = Keybind::new(&[Keycode::LControl, Keycode::G]);
///
/// keybind.on_trigger(|| {
/// println!("This will be printed when you press CTRL+G");
/// });
/// ```
pub fn on_trigger<C: Fn() + 'static>(&mut self, callback: C) {
self.on_trigger = Box::new(callback);
}

/// Starts an infinite loop and calls provided callback when the keybind is triggered.
///
/// # Example
///
/// ```ignore
/// use keybind::{Keybind, Keycode};
///
///fn main() {
/// let mut keybind = Keybind::new(&[Keycode::LControl, Keycode::G]);
///
/// keybind.on_trigger(|| {
/// println!("This will be printed when you press CTRL+G");
/// });
///
/// keybind.wait();
///}
/// ```
pub fn wait(&mut self) {
loop {
if self.triggered() {
(self.on_trigger)();
}
}
}
}

0 comments on commit 52d9f09

Please # to comment.