ostrosco / device_query Goto Github PK
View Code? Open in Web Editor NEWA simple library to query mouse and keyboard inputs without a window. Will work in Windows, Linux, and macOS.
License: MIT License
A simple library to query mouse and keyboard inputs without a window. Will work in Windows, Linux, and macOS.
License: MIT License
From a different issue:
When recording keystrokes via dq, when encountering these keys (SHIFT、OPTION、CONTROL), it will be recorded as 'left' regardless of whether the press is 'left' or 'right'.
This only appears to be happening on MacOS. We'll need to figure out if this is a device_query or dependency issue.
My program gives me this error
Maximum number of clients reached. Thread 'main' panicked at 'Could not connect to a X display', /home/helium/.cargo/registry/src/github.com-1ecc6299db9ec823/device_query-0.2.8/src/linux/mod.rs:26:17
And i'm running on Wayland so I suspect that it doesn't support Wayland at the moment. Any plans on adding Wayland support?
I'm currently implementing Numpad support but there are some keys which technically can be found twice on a standard keyboard:
as well as overlapping keys:
Should nearly identical keys be merged or this too high-level for this crate?
priorKey(): The name of the last key which was down prior to the most recent key-down or key-release, or blank if no applicable key-press can be found in the key history.Just like Autohotkey.
Just wanted to know what format does the keycode output follow?
Code taken from here (https://stackoverflow.com/questions/35671985/how-do-i-get-keyboard-input-without-the-user-pressing-the-enter-key)
use device_query::{DeviceQuery, DeviceState, Keycode};
fn main() {
let device_state = DeviceState::new();
loop {
let keys: Vec<Keycode> = device_state.get_keys();
for key in keys.iter() {
println!("Pressed key: {:?}", key);
}
}
}
None of my character inputs are recognized. Only stuff like LControl
or BackSpace
Hi!
I was wondering about the arrow keys support. Is it missing for some reason in special? Is it a future work?
Hello,
as wayland is now getting the default window manager on several linux distributions, I would very like to see support for it in this lib.
In case you need support in testing I can try to help.
Hi, thanks for your awesome library.
I'm engaged in front-end development, and recently I'm using tauri
to make an app that records keystrokes (and want to use enigo
for autoplay),
But I ran into a problem, in your example (event_based_print_keys.rs),
You use println!
to print directly to the console, but I'd like to be able to log to a vector for other uses.
However, because on_key_down
's parameter callback
is of type Fn
instead of FnMut
, I cannot modify the outer vector.
Is there any good workaround please, I'm wondering if using FnMut
here will cause some side effects.
I may not have learned enough about rust
, I hope you can point out where I am wrong.
Thanks again, this library is very helpful for my development.
pub struct KeyboardEv {
code: Keycode,
press: bool,
timestamp: u128,
}
pub struct KeyboardRecorder {
ev_queue: Vec<KeyboardEv>,
}
impl KeyboardRecorder {
pub fn new() -> Self {
KeyboardRecorder {
ev_queue: vec![],
}
}
pub fn start_record(&mut self) {
// instance
let device_state = DeviceState::new();
// record start time as zero
let mut timeline = Instant::now();
// record press
let _guard = device_state.on_key_down(|key| {
println!("down: {:#?}", key);
let keyCode = key.clone();
let time_from_zero = timeline.elapsed().as_millis();
// ▲ i`d like to save custom event record in an outer vector
self.ev_queue.push(KeyboardEv {
code: keyCode,
press: true,
timestamp: time_from_zero,
});
});
// record release
// let _guard = device_state.on_key_up(|key| {
// println!("up: {:#?}", key);
// });
loop {}
}
}
I'm running this code:
use device_query::{DeviceQuery, DeviceState, Keycode};
fn main() {
let device_state = DeviceState::new();
loop {
let keys: Vec<Keycode> = device_state.get_keys();
for key in keys.iter() {
println!("Pressed key: {:?}", key);
}
}
}
There are several keys that when pressed don't trigger a 'Pressed Key ...' message. They include arrowkeys, -, +, `, backspace, and a couple others. Any idea what's going on here?
I'm running this app using cargo run
from powershell/windows terminal on Windows 10.
This project currently supports a subset of ASCII and most of the modifier keys like CTRL and SHIFT. But on other keyboard layouts it provides incorrect mappings, e.g. QWERTZ ö translates to Grave instead of the umlaut (this is mentioned in the project
files). This is to be expected as i.e. the Win32 API only labels them as "OEM keys", the linux kernel on the other hand labels this key as GRAVE instead.
However if the mappings are US-QWERTY only they are currently inconsistent as the crate/Windows does translate the ASCII keys (like z vs y in QWERTZ) between locales (at least on windows) due to windows probably pre-processing the keys (I couldn't find any information online on this).
I have not researched the APIs as to why they label it differently but this problem on begs the question:
How "high-level" is this crate? Should this crate reporting the key position or the user facing value of the key? If yes, should or can they be localized?
device_query
uses input-event-codes
crate as dependency, and has MIT as the license.
but input-event-codes
has its license set to "GPL-2.0-only"
which is AFAICT copy-left which would mean device_query (and crates that depend on it) would get infected with that license too as all of us use static linking against that crate (and its source).
I was just setting up deny.toml
for my project and this issue popped up.
Hi, I am not able to capture the escape key on windows and it seems to be missing the VK_ESCAPE key in keycode_to_key (windows/mod.rs).
When trying to input a '@' key using a German QWERTZ keyboard (so ALTGR+Q), the crate outputs "Key2" instead of L-CONTROL+R-ALT+Q. This is probably due to Linux/X11 translating the keycodes into a american keyboard layout in which the number row 2 key is used for typing '@'. In contrast on Windows the correct key is output.
The same basic problem applies to the key combination for 'µ' using ALTGR+M with no keycode whatsoever being output.
I suppose the issue doesn't lie with this crate but the way the keycodes are obtained. Are there any different less problematic methods of obtaining keycodes? If not, what can be done to mitigate this problem?
(Used the print_keycode example for testing)
sorry bro, i`m back again. /(ㄒoㄒ)/~~
Recently i`m writing a node-addon for my electron app to listen some hotkey events (such as ctrl+c for copy).
I use the napi library (https://github.com/napi-rs/napi-rs) to implement the transition from rust to node.
But I encountered a little problem when listening to the button. As written in your example,
as long as _guard
is alive, the listen will always be valid. My idea is to assign the guard returned by the function to a member of the struct so It can be held for a long time, but I don't know what type of this member should be defined, can you give me some help ┭┮﹏┭┮
Hi, I'm trying to use this crate in a TUI I'm developing as a way to detect key-up events but it seems to not detect anything when the terminal is in focus on Linux. Actually, it seems to not work at all unless I have certain other applications in focus--seemingly ones that aren't capturing text input. I'm not sure how to proceed.
Ubuntu 22.04.1 LTS
device_query v1.1.1
run this demo
https://github.com/ostrosco/device_query/blob/master/examples/event_based_print_keys.rs
High CPU usage
My computer is a MacBook Pro running the most recent MacOS (11.2.2). It appears that get_keys() is not picking up when alphanumeric keys, or the spacebar are pressed. Other keys (tab, shit, ctrl, etc.) are detected. From talking with friends, they are experiencing the same issue, and code that does not work on my MacBook runs without issue when performed on Windows.
Currently, another choice for linux user is that, using wayland rather than x11.
But as I tried device_query on my wayland based linux, the demo always returns
Current Mouse Coordinates: (94, 1035)
Is A pressed? false
It seems that there is no support for wayland now
Is it possible to add wayland support?
I have tried with some basic GTK window. I am getting a Segmentation fault (core dumped)
error.
Operating system: Linux
extern crate gtk;
extern crate device_query;
use device_query::{DeviceState};
use gtk::prelude::*;
fn main(){
if let Err(_) = gtk::init() {
panic!("UIError");
}
let window = gtk::Window::new(gtk::WindowType::Toplevel);
window.set_title("First GTK+ Program");
window.set_border_width(10);
window.set_position(gtk::WindowPosition::Center);
window.set_default_size(350, 70);
let button = gtk::Button::new_with_label("Click me!");
window.add(&button);
let device_state = DeviceState::new();
window.show_all();
gtk::main();
}
device_query = "0.2.3"
gtk = "0.8.1"
On macOS, accessibility permissions are required before this library will function at all. See here. This is already documented in the README, but to make it easier on developers using this crate it would be nice if a message could be logged when the app doesn't have permissions. The API could also do this check and expose it as a function somewhere, so that consumers of the crate can write it to their own popup or messaging for their users.
Here's the example from the README, modified to show an example API:
let device_state: DeviceState = match DeviceState::new() {
Ok(device) => device,
Err(DeviceError::InsufficientPermissions) => {
message_box("Please enable Accessibility Permissions");
return;
},
Err(e) => panic!("{}", e),
};
// The rest of the example is unchanged
let mouse: MouseState = device_state.get_mouse();
println!("Current Mouse Coordinates: {:?}", mouse.coords);
let keys: Vec<Keycode> = device_state.get_keys();
println!("Is A pressed? {}", keys.contains(Keycode::A));
I have no idea if Apple's APIs will allow this or not, so this is just spit balling to show usage. Once we know this can work, we can iterate on what a useful API would actually look like.
This commit in dteoh/SlowQuitApps added a popup dialog and is probably a good starting place to figure out how to include this kind of check.
I have this very simple function that is meant to run multiple times. About two hundred calls into this function, I have the following error:
Pointing me to this line
I suspect it's because I am creating multiple DeviceState
, and they are not having enough time to close (?)
This is on Manjaro 21 XFCE.
P.S. My original problem is that I need to query the mouse coordinates, but I could not make the DeviceState
into a static (it does not implement Sync
), so I was creating one locally every time. I appreciate Rust-specific suggestions for solving this particular problem too (How to make a unique instance of DeviceState where I can query the coords from?)
Is it possible to implement mouse wheel left/right click?
Hi @ostrosco,
Compiling your library in a clean ubuntu18, I got the following error related to your x11 dependency:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Failure { command: "\"pkg-config\" \"--libs\" \"--cflags\" \"x11\" \"x11 >= 1.4.99.1\"", output: Output { status: ExitStatus(ExitStatus(256)), stdout: "", stderr: "Package x11 was not found in the pkg-config search path.\nPerhaps you should add the directory containing `x11.pc\'\nto the PKG_CONFIG_PATH environment variable\nNo package \'x11\' found\nPackage x11 was not found in the pkg-config search path.\nPerhaps you should add the directory containing `x11.pc\'\nto the PKG_CONFIG_PATH environment variable\nNo package \'x11\' found\n" } }', src/libcore/result.rs:1188:5
After investigating, I found that it is due to the leak of the x11 development libraries in the system, needed to compile x11.
In ubuntu it can be easily installed with the following command:
sudo apt install libx11-dev
Maybe it can be useful to add this dependency under a "dependencies" section as part of the readme.
Regards!
Wanted ask if you can add support for KEY_PROG1,2,3,4?
I am creating an issue because I am not sure how would that work on Windows/MacOS, on Linux that is straight forward, only need to add them to the Keycode enum and too kernel_key_to_keycode
For me it would be perfectly fine to have those keys only working on Linux, I am working on a program for doing some power management on my laptop and it has two nice keys dedicated to it with emit KEY_PROG1 and KEY_PROG2.
Would be fine to add those keys but mark them with #[cfg(target_os = "linux")]
, in this way when someone would try to use them on MacOS/Windows it would give a compiler error?
Of course is someone finds what codes are for those keys on Windows/MacOS then they could be add too, but I am even not sure if they exist on those systems.
Would gladly like to have events :)
When recording key down and up events using DeviceState
's on_key_down
and on_key_up
I'm still ONLY receiving the meta keys even after adding my terminal to accessibility settings as listed in the README.
The terminal I'm using is Warp
My Mac:
when i poll for pressed keys using DeviceState.get_keys() when say, a game is in full screen, it does not work.
(Sorry, I'm completely new to desktop programming. Maybe it's not an issue.)
I ran the code from examples/event_based_print_keys.rs
and found that the app uses 300% CPU (on Mac). As I guess, 100% is taken by loop {}
, but is it normal that it uses 200% more?
some methods noted in https://docs.rs/device_query/1.1.1/device_query/ are not implemented, is this planned in the future or do we have to implement listeners on our own?
e.g:
let device_state = DeviceState::new();
let _guard = device_state.on_mouse_move(|position| {
println!("Mouse position: {:#?}", position);
});
no method named `on_mouse_move` found for struct `device_query::DeviceState` in the current scope
attempting to compile a project with this crate on wayland returns the error
Perhaps you should add the directory containing `x11.pc' to the PKG_CONFIG_PATH environment variable
Package 'x11', required by 'virtual:world', not found```
Just a feature request to include scrolling in the mouse state if possible
When I try to use the program listed in the README and run cargo build
it fails with this error:
Compiling device_query v0.1.3
error[E0601]: `main` function not found in crate `build_script_build`
--> /Users/rj/.cargo/registry/src/github.com-1ecc6299db9ec823/device_query-0.1.3/build.rs:1:1
|
1 | / extern crate pkg_config;
2 | |
3 | | #[cfg(target_os = "windows")]
4 | | fn main() {}
... |
39 | | }
40 | | }
| |_^ consider adding a `main` function to `/Users/rj/.cargo/registry/src/github.com-1ecc6299db9ec823/device_query-0.1.3/build.rs`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0601`.
error: could not compile `device_query`.
To learn more, run the command again with --verbose.
I am on Macos, and looking at the file in question, it doesn't look like there's a #[cfg(target_os = "macos")].
I am able to build your repo just fine by cloning it and building it a temp directory.
Wayland support would be a nice thing to add.
i I made a mwe on windows and it works fine. However, now I need to upload the code to raspberry pi. I access it through ssh (using mobaxterm which has a x server). But it does not recognise the keys through the ssh, and if i plug in a keyboard directly to the pi, it does not register the keys. any ideas?
Hi, i receive a seg fault when trying to run an example to get keypresses. If you are able to lend some advice that would be awesome. thanks
Running the example from https://github.com/ostrosco/device_query/blob/master/examples/print_keys.rs
I just see my process taking more and more memory as I type.
Hello, I have an AZERTY keyboard, and I would like to adapt this lib but I don't know how to do it
When calling get_keys() from DeviceState, no keys show but shift, backspace, meta, escape, control, and alt.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.