Skip to content

Commit

Permalink
refactor(!): migrate mousepos to mousepos-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
aalhendi committed Sep 30, 2023
1 parent b1c32a3 commit 8fcc00e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 66 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -4,4 +4,5 @@ resolver = "2"
members = [
"carnival-web",
"carnival-matchserver",
"carnival-matchserver/tooling/mousepos"
]
15 changes: 0 additions & 15 deletions carnival-matchserver/tooling/mousepos/.gitignore

This file was deleted.

13 changes: 13 additions & 0 deletions carnival-matchserver/tooling/mousepos/Cargo.toml
@@ -0,0 +1,13 @@
[package]
name = "mousepos"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
windows = { version = "0.51.1", features = [
"Win32_Foundation",
"Win32_UI_Input_KeyboardAndMouse",
"Win32_UI_WindowsAndMessaging",
] }
12 changes: 0 additions & 12 deletions carnival-matchserver/tooling/mousepos/dub.json

This file was deleted.

39 changes: 0 additions & 39 deletions carnival-matchserver/tooling/mousepos/source/app.d

This file was deleted.

42 changes: 42 additions & 0 deletions carnival-matchserver/tooling/mousepos/src/main.rs
@@ -0,0 +1,42 @@
use std::thread::sleep;
use std::time::Duration;
use windows::core::Result;
use windows::Win32::Foundation::POINT;
use windows::Win32::UI::Input::KeyboardAndMouse::GetAsyncKeyState;
use windows::Win32::UI::WindowsAndMessaging::GetCursorPos;

const DELAY: Duration = Duration::from_millis(25);
const TRIGGER_KEY: i32 = 'K' as i32;

#[derive(Default)]
struct CursorState {
x: i32,
y: i32,
}

impl CursorState {
/// Update cursor coordinates.
fn update(&mut self, new_pos: POINT) {
self.x = new_pos.x;
self.y = new_pos.y;
}
}

fn main() -> Result<()> {
let mut cursor_pos = POINT { x: 0, y: 0 };
let mut state = CursorState::default();

loop {
// Check if the trigger key is pressed and try to get the cursor position.
if unsafe { GetAsyncKeyState(TRIGGER_KEY) & 1 } != 0
&& unsafe { GetCursorPos(&mut cursor_pos).is_ok() }
{
state.update(cursor_pos);
// Log the cursor state
println!("{{ x = {}, y = {}, delay = {}}},", state.x, state.y, DELAY.as_millis());
}

// Sleep for a defined delay to limit CPU usage.
sleep(DELAY);
}
}

0 comments on commit 8fcc00e

Please sign in to comment.