Skip to content

Commit c209bbb

Browse files
committed
added a small gui to quickly find the right key name
1 parent 6d35e17 commit c209bbb

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

Readme.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ MIT License
205205
Accept, Add, Apps, Attn, Back, BrowserBack, BrowserFavorites, BrowserForward, BrowserHome, BrowserRefresh, BrowserSearch, BrowserStop, Cancel, Capital, Clear, Control, Convert, Crsel, Decimal, Delete, Divide, Down, End, Ereof, Escape, Execute, Exsel, F1, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F2, F20, F21, F22, F23, F24, F3, F4, F5, F6, F7, F8, F9, Final, GamepadA, GamepadB, GamepadDPadDown, GamepadDPadLeft, GamepadDPadRight, GamepadDPadUp, GamepadLeftShoulder, GamepadLeftThumbStickBut, GamepadLeftThumbStickDow, GamepadLeftThumbStickLef, GamepadLeftThumbStickRig, GamepadLeftThumbStickUp, GamepadLeftTrigger, GamepadMenu, GamepadRightShoulder, GamepadRightThumbStickBu, GamepadRightThumbStickDo, GamepadRightThumbStickLe, GamepadRightThumbStickRi, GamepadRightThumbStickUp, GamepadRightTrigger, GamepadView, GamepadX, GamepadY, HanjaKanji, Help, Home, Ico00, IcoClear, IcoHelp, ImeOff, ImeOn, Insert, Junja, KanaHangeulHangul, Key0, Key1, Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, KeyA, KeyB, KeyC, KeyD, KeyE, KeyF, KeyG, KeyH, KeyI, KeyJ, KeyK, KeyL, KeyM, KeyN, KeyO, KeyP, KeyQ, KeyR, KeyS, KeyT, KeyU, KeyV, KeyW, KeyX, KeyY, KeyZ, LaunchApp1, LaunchApp2, LaunchMail, LaunchMediaSelect, LButton, LControl, Left, LMenu, LShift, LWin, MButton, MediaNextTrack, MediaPlayPause, MediaPrevTrack, MediaStop, Menu, ModeChange, Multiply, NavigationAccept, NavigationCancel, NavigationDown, NavigationLeft, NavigationMenu, NavigationRight, NavigationUp, NavigationView, Next, Noname, NonConvert, Numlock, Numpad0, Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, Oem1, Oem102, Oem2, Oem3, Oem4, Oem5, Oem6, Oem7, Oem8, OemAttn, OemAuto, OemAx, OemBackTab, OemClear, OemComma, OemCopy, OemCusel, OemEnlw, OemFinish, OemFjLoya, OemFjMasshou, OemFjRoya, OemFjTouroku, OemJump, OemMinus, OemNecEqualFjJisho, OemPa1, OemPa2, OemPa3, OemPeriod, OemPlus, OemReset, OemWsCtrl, Pa1, Packet, Pause, Play, Print, Prior, ProcessKey, RButton, RControl, Return, Right, RMenu, RShift, RWin, Scroll, Select, Separator, Shift, Sleep, Snapshot, Space, Subtract, Tab, Up, VolumeDown, VolumeMute, VolumeUp, XButton1, XButton2, Zoom
206206
```
207207

208-
**Note 1:** More information about these keys can be found [here](https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731.aspx).
208+
**Note 1:** An easy way get the name of a key is to run the client with the argument `print_keys` (`inputshare-client.exe print_keys`). This will open a small window that shows the name of the last pressed key.
209209

210-
**Note 2:** Not every key in this list will work, it just won't cause the program to crash when reading the config.
210+
![](https://imgur.com/7zO7clu.png)
211+
212+
**Note 2:** More information about these keys can be found [here](https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731.aspx).
213+
214+
**Note 3:** Not every key in this list will work, it just won't cause the program to crash when reading the config.
211215

inputshare-client/src/main.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use anyhow::Result;
2222
use native_windows_derive::NwgUi;
2323
use native_windows_gui::{CharEffects, MessageButtons, MessageIcons, MessageParams, NativeUi};
2424
use udp_connections::{Client, ClientDisconnectReason, ClientEvent, Endpoint, MAX_PACKET_SIZE};
25-
use winapi::um::winuser::{DispatchMessageW, GA_ROOT, GetAncestor, GetCursorPos, IsDialogMessageW, TranslateMessage, PostMessageW, WM_KEYDOWN, WM_QUIT, WM_USER};
25+
use winapi::um::winuser::{GetCursorPos, PostMessageW, WM_KEYDOWN, WM_QUIT, WM_USER};
2626
use inputshare_common::IDENTIFIER;
2727
use winsock2_extensions::{NetworkEvents, WinSockExt};
2828
use yawi::{HookType, Input, InputEvent, InputHook, KeyState, ScrollDirection, send_inputs, VirtualKey};
@@ -38,6 +38,10 @@ fn main() -> Result<()>{
3838
nwg::init().expect("Failed to init Native Windows GUI");
3939
nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
4040

41+
if std::env::args().find(|arg| arg == "print_keys").is_some() {
42+
return run_key_tester()
43+
}
44+
4145
match client() {
4246
Ok(_) => Ok(()),
4347
Err(e) => {
@@ -72,13 +76,7 @@ fn client() -> Result<()> {
7276
_ => Duration::from_millis(500)
7377
}))?;
7478
let mut socket_message = false;
75-
while let Some(mut msg) = get_message() {
76-
unsafe {
77-
if IsDialogMessageW(GetAncestor(msg.hwnd, GA_ROOT), &mut msg) == 0 {
78-
TranslateMessage(&msg);
79-
DispatchMessageW(&msg);
80-
}
81-
}
79+
while let Some(msg) = get_message() {
8280
match msg.message {
8381
WM_QUIT => break 'outer,
8482
WM_KEYDOWN => if matches!(VirtualKey::try_from(msg.wParam as u8), Ok(VirtualKey::F1)) && (msg.lParam & (1 << 30)) == 0{
@@ -480,4 +478,42 @@ fn inplace_format<'a>(buf: &'a mut [u8], args: Arguments<'_>) -> std::io::Result
480478
#[macro_export]
481479
macro_rules! format_buf {
482480
($dst:expr, $($arg:tt)*) => (inplace_format($dst, format_args!($($arg)*)))
481+
}
482+
483+
#[derive(Default, NwgUi)]
484+
pub struct KeyTester {
485+
#[nwg_resource(family: "Consolas", size: 25, weight: 500)]
486+
small_font: nwg::Font,
487+
488+
#[nwg_control(size: (300, 50), position: (300, 300), title: "Key Tester", flags: "WINDOW|VISIBLE",
489+
icon: Some(&nwg::EmbedResource::load(None)?.icon(1, None).unwrap()))]
490+
#[nwg_events( OnWindowClose: [nwg::stop_thread_dispatch()])]
491+
window: nwg::Window,
492+
493+
#[nwg_control(text: "Press a key", h_align: HTextAlign::Center, v_align: VTextAlign::Center,
494+
font: Some(&data.small_font), size: (300, 50), position: (0, 10), flags: "VISIBLE")]
495+
info_label: nwg::Label,
496+
497+
}
498+
499+
fn run_key_tester() -> Result<()> {
500+
let app = KeyTester::build_ui(Default::default()).expect("Failed to build UI");
501+
let mut pressed_keys = HashSet::new();
502+
let _h = InputHook::new(move |event|{
503+
if let Some(event) = event.to_key_event() {
504+
match (pressed_keys.contains(&event.key), event.state) {
505+
(false, KeyState::Pressed) => {
506+
pressed_keys.insert(event.key);
507+
app.info_label.set_text(&format!("{:?}", event.key))
508+
},
509+
(true, KeyState::Released) => {
510+
pressed_keys.remove(&event.key);
511+
},
512+
_ => { }
513+
}
514+
}
515+
true
516+
}, true, HookType::KeyboardMouse)?;
517+
nwg::dispatch_thread_events();
518+
Ok(())
483519
}

inputshare-client/src/windows.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Duration;
55
use winapi::shared::minwindef::{FALSE};
66
use winapi::shared::winerror::WAIT_TIMEOUT;
77
use winapi::um::winbase::{INFINITE, WAIT_OBJECT_0, WAIT_FAILED};
8-
use winapi::um::winuser::{MSG, MsgWaitForMultipleObjects, PeekMessageW, PM_REMOVE, QS_ALLINPUT};
8+
use winapi::um::winuser::{DispatchMessageW, GA_ROOT, GetAncestor, IsDialogMessageW, MSG, MsgWaitForMultipleObjects, PeekMessageW, PM_REMOVE, QS_ALLINPUT, TranslateMessage};
99

1010

1111
pub fn wait_message_timeout(timeout: Option<Duration>) -> std::io::Result<bool> {
@@ -28,7 +28,13 @@ pub fn get_message() -> Option<MSG> {
2828
let mut msg: MSG = mem::zeroed();
2929
match PeekMessageW(&mut msg, null_mut(), 0, 0, PM_REMOVE) {
3030
FALSE => None,
31-
_ => Some(msg)
31+
_ => {
32+
if IsDialogMessageW(GetAncestor(msg.hwnd, GA_ROOT), &mut msg) == 0 {
33+
TranslateMessage(&msg);
34+
DispatchMessageW(&msg);
35+
}
36+
Some(msg)
37+
}
3238
}
3339
}
3440
}

0 commit comments

Comments
 (0)