RosettaCodeData/Task/Mouse-position/Rust/mouse-position.rust

38 lines
876 B
Text
Raw Permalink Normal View History

2014-04-02 16:56:35 +00:00
// rustc 0.9 (7613b15 2014-01-08 18:04:43 -0800)
2013-10-27 22:24:23 +00:00
2014-04-02 16:56:35 +00:00
use std::libc::{BOOL, HANDLE, LONG};
2013-10-27 22:24:23 +00:00
use std::ptr::mut_null;
type HWND = HANDLE;
#[deriving(Eq)]
struct POINT {
x: LONG,
y: LONG
}
2014-01-17 05:32:22 +00:00
#[link_name = "user32"]
extern "system" {
2014-04-02 16:56:35 +00:00
fn GetCursorPos(lpPoint:&mut POINT) -> BOOL;
fn GetForegroundWindow() -> HWND;
fn ScreenToClient(hWnd:HWND, lpPoint:&mut POINT);
2013-10-27 22:24:23 +00:00
}
fn main() {
let mut pt = POINT{x:0, y:0};
loop {
2014-01-17 05:32:22 +00:00
std::io::timer::sleep(100); // sleep duration in milliseconds
2013-10-27 22:24:23 +00:00
let pt_prev = pt;
unsafe { GetCursorPos(&mut pt) };
if pt != pt_prev {
let h = unsafe { GetForegroundWindow() };
if h == mut_null() { continue }
2014-01-17 05:32:22 +00:00
let mut pt_client = pt;
unsafe { ScreenToClient(h, &mut pt_client) };
println!("x: {}, y: {}", pt_client.x, pt_client.y);
2013-10-27 22:24:23 +00:00
}
}
}