Skip to content

Instantly share code, notes, and snippets.

@YurySolovyov
Last active March 27, 2016 18:54
Show Gist options
  • Save YurySolovyov/ab14eb969f64c2b098d9 to your computer and use it in GitHub Desktop.
Save YurySolovyov/ab14eb969f64c2b098d9 to your computer and use it in GitHub Desktop.
extern crate winapi;
extern crate user32;
use self::winapi::windef::HWND;
use self::winapi::minwindef::UINT;
use self::winapi::minwindef::DWORD;
use self::winapi::minwindef::WPARAM;
use self::winapi::minwindef::LPARAM;
fn register_hotkey_api_call(key_options: u32, key_code: u32) -> bool {
unsafe {
return user32::RegisterHotKey(0 as HWND, 1, key_options, key_code) != 0
}
}
fn register_hotkey() {
let key_options = (0x0002 | 0x4000) as u32; // Ctrl + NO_REPEAT
let key = 0x50 as u32; // P key
if register_hotkey_api_call(key_options, key) {
println!("Registered");
}
}
fn get_message_api_call(message: &mut winapi::winuser::MSG) -> bool {
unsafe {
return user32::GetMessageW(message, 0 as HWND, 0, 0) != 0;
}
}
fn handle_message(message: &mut winapi::winuser::MSG) {
unsafe {
user32::TranslateMessage(message);
user32::DispatchMessageW(message);
}
}
fn process_messages() {
let mut message = winapi::winuser::MSG {
hwnd : 0 as HWND,
message : 0 as UINT,
wParam : 0 as WPARAM,
lParam : 0 as LPARAM,
time : 0 as DWORD,
pt : winapi::windef::POINT { x: 0, y: 0, },
};
loop {
let has_messages = get_message_api_call(&mut message);
if has_messages {
println!("Got message -> {:?}", message);
} else {
continue;
}
if message.message == winapi::winuser::WM_QUIT {
break;
}
handle_message(&mut message);
}
}
pub fn init() {
register_hotkey();
process_messages();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment