Skip to content

Commit 8f258ea

Browse files
authored
Don't restrict VirtualKey mode to Windows only (#92)
1 parent bc31f47 commit 8f258ea

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

SDK_USAGE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ By default, the mode is set to HID
105105

106106
### Notes
107107

108-
- `VirtualKey` and `VirtualKeyTranslate` are only available on Windows
108+
- `VirtualKeyTranslate` is only available on Windows
109109
- With all modes except `VirtualKeyTranslate`, the key identifier will point to the physical key on the standard layout. i.e. if you ask for the Q key, it will be the key right to tab regardless of the layout you have selected
110110
- With `VirtualKeyTranslate`, if you request Q, it will be the key that inputs Q on the current layout, not the key that is Q on the standard layout.
111111

@@ -165,9 +165,9 @@ The `device_id` can be found through calling [Get Device Info](#get-connected-de
165165
```
166166
wooting_analog_set_mode(KeycodeType::ScanCode1);
167167
wooting_analog_read_analog(0x10); //This will get you the value for the key which is Q in the standard US layout (The key just right to tab)
168-
wooting_analog_set_mode(KeycodeType::VirtualKey); //This will only work on Windows
168+
wooting_analog_set_mode(KeycodeType::VirtualKey);
169169
wooting_analog_read_analog(0x51); //This will get you the value for the key that is Q on the standard layout
170-
wooting_analog_set_mode(KeycodeType::VirtualKeyTranslate); //Also will only work on Windows
170+
wooting_analog_set_mode(KeycodeType::VirtualKeyTranslate); //This will only work on Windows
171171
wooting_analog_read_analog(0x51); //This will get you the value for the key that inputs Q on the current layout
172172
```
173173

wooting-analog-sdk/src/ffi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ pub extern "C" fn wooting_analog_set_keycode_mode(mode: c_uint) -> WootingAnalog
105105
return WootingAnalogResult::UnInitialized;
106106
}
107107

108-
//TODO: Make it return invalid argument when attempting to use virutal keys on platforms other than win
108+
//TODO: Make it return invalid argument when attempting to use VirtualKeyTranslate on platforms other than win
109109
if let Some(key_mode) = KeycodeType::from_u32(mode) {
110110
#[cfg(not(windows))]
111111
{
112-
if key_mode == KeycodeType::VirtualKey || key_mode == KeycodeType::VirtualKeyTranslate {
112+
if key_mode == KeycodeType::VirtualKeyTranslate {
113113
return WootingAnalogResult::NotAvailable;
114114
}
115115
}

wooting-analog-sdk/src/keycode.rs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,18 @@ lazy_static! {
324324

325325
}
326326

327-
#[allow(unused)]
328-
pub fn vk_to_hid(vk: u16, translate: bool) -> Option<u16> {
327+
pub fn vk_to_hid(vk: u16) -> Option<u16> {
328+
if let Some(&hid) = HID_TO_VK_MAP_US.get_by_right(&(vk as u8)) {
329+
return Some(hid as u16);
330+
} else {
331+
return None;
332+
}
333+
}
334+
335+
#[allow(unused)] // Suppress warning about 'vk' being unused on non-windows
336+
pub fn vk_to_hid_translate(vk: u16) -> Option<u16> {
329337
#[cfg(windows)]
330-
if translate {
338+
{
331339
let scancode: u16;
332340
if let Some(&code) = VIRTUALKEY_OVERRIDE.get_by_left(&(vk as u8)) {
333341
scancode = code;
@@ -340,44 +348,38 @@ pub fn vk_to_hid(vk: u16, translate: bool) -> Option<u16> {
340348
}
341349
}
342350
return scancode_to_hid(scancode);
343-
} else {
344-
if let Some(&hid) = HID_TO_VK_MAP_US.get_by_right(&(vk as u8)) {
345-
return Some(hid as u16);
346-
} else {
347-
return None;
348-
}
349351
}
350352

351353
#[cfg(not(windows))]
352354
None
353355
}
354356

355-
#[allow(unused)]
356-
pub fn hid_to_vk(hid: u16, translate: bool) -> Option<u16> {
357-
#[cfg(windows)]
358-
if translate {
359-
if let Some(scancode) = hid_to_scancode(hid) {
360-
if let Some(&hid) = VIRTUALKEY_OVERRIDE.get_by_right(&scancode) {
361-
return Some(hid as u16);
362-
}
357+
pub fn hid_to_vk(hid: u16) -> Option<u16> {
358+
if let Some(&vk) = HID_TO_VK_MAP_US.get_by_left(&(hid as u8)) {
359+
return Some(vk as u16);
360+
} else {
361+
return None;
362+
}
363+
}
363364

364-
use winapi::um::winuser::MapVirtualKeyA;
365-
let vk: u32 = unsafe { MapVirtualKeyA(scancode.into(), 3) };
365+
#[allow(unused)] // Suppress warning about 'hid' being unused on non-windows
366+
pub fn hid_to_vk_translate(hid: u16) -> Option<u16> {
367+
#[cfg(windows)]
368+
if let Some(scancode) = hid_to_scancode(hid) {
369+
if let Some(&hid) = VIRTUALKEY_OVERRIDE.get_by_right(&scancode) {
370+
return Some(hid as u16);
371+
}
366372

367-
if (vk == 0) {
368-
return None;
369-
}
373+
use winapi::um::winuser::MapVirtualKeyA;
374+
let vk: u32 = unsafe { MapVirtualKeyA(scancode.into(), 3) };
370375

371-
return Some(vk as u16);
372-
} else {
376+
if vk == 0 {
373377
return None;
374378
}
379+
380+
return Some(vk as u16);
375381
} else {
376-
if let Some(&vk) = HID_TO_VK_MAP_US.get_by_left(&(hid as u8)) {
377-
return Some(vk as u16);
378-
} else {
379-
return None;
380-
}
382+
return None;
381383
}
382384

383385
#[cfg(not(windows))]
@@ -425,8 +427,8 @@ pub fn code_to_hid(code: u16, mode: &KeycodeType) -> Option<u16> {
425427
}
426428
}
427429
KeycodeType::ScanCode1 => scancode_to_hid(code),
428-
KeycodeType::VirtualKey => vk_to_hid(code, false),
429-
KeycodeType::VirtualKeyTranslate => vk_to_hid(code, true),
430+
KeycodeType::VirtualKey => vk_to_hid(code),
431+
KeycodeType::VirtualKeyTranslate => vk_to_hid_translate(code),
430432
}
431433
}
432434

@@ -448,8 +450,8 @@ pub fn hid_to_code(code: u16, mode: &KeycodeType) -> Option<u16> {
448450
}
449451
}
450452
KeycodeType::ScanCode1 => hid_to_scancode(code),
451-
KeycodeType::VirtualKey => hid_to_vk(code, false),
452-
KeycodeType::VirtualKeyTranslate => hid_to_vk(code, true),
453+
KeycodeType::VirtualKey => hid_to_vk(code),
454+
KeycodeType::VirtualKeyTranslate => hid_to_vk_translate(code),
453455
}
454456
}
455457

@@ -467,7 +469,11 @@ mod tests {
467469
KeycodeType::VirtualKeyTranslate,
468470
];
469471
#[cfg(not(windows))]
470-
let keycode_types = [KeycodeType::HID, KeycodeType::ScanCode1];
472+
let keycode_types = [
473+
KeycodeType::HID,
474+
KeycodeType::ScanCode1,
475+
KeycodeType::VirtualKey,
476+
];
471477
for code in 0..0xFFFF {
472478
let prefix = (code & 0xFF00) >> 8;
473479
match prefix {

0 commit comments

Comments
 (0)