Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't restrict VirtualKey mode to Windows only #92

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions SDK_USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ By default, the mode is set to HID

### Notes

- `VirtualKey` and `VirtualKeyTranslate` are only available on Windows
- `VirtualKeyTranslate` is only available on Windows
- 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
- 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.

Expand Down Expand Up @@ -165,9 +165,9 @@ The `device_id` can be found through calling [Get Device Info](#get-connected-de
```
wooting_analog_set_mode(KeycodeType::ScanCode1);
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)
wooting_analog_set_mode(KeycodeType::VirtualKey); //This will only work on Windows
wooting_analog_set_mode(KeycodeType::VirtualKey);
wooting_analog_read_analog(0x51); //This will get you the value for the key that is Q on the standard layout
wooting_analog_set_mode(KeycodeType::VirtualKeyTranslate); //Also will only work on Windows
wooting_analog_set_mode(KeycodeType::VirtualKeyTranslate); //This will only work on Windows
wooting_analog_read_analog(0x51); //This will get you the value for the key that inputs Q on the current layout
```

Expand Down
4 changes: 2 additions & 2 deletions wooting-analog-sdk/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ pub extern "C" fn wooting_analog_set_keycode_mode(mode: c_uint) -> WootingAnalog
return WootingAnalogResult::UnInitialized;
}

//TODO: Make it return invalid argument when attempting to use virutal keys on platforms other than win
//TODO: Make it return invalid argument when attempting to use VirtualKeyTranslate on platforms other than win
if let Some(key_mode) = KeycodeType::from_u32(mode) {
#[cfg(not(windows))]
{
if key_mode == KeycodeType::VirtualKey || key_mode == KeycodeType::VirtualKeyTranslate {
if key_mode == KeycodeType::VirtualKeyTranslate {
return WootingAnalogResult::NotAvailable;
}
}
Expand Down
74 changes: 40 additions & 34 deletions wooting-analog-sdk/src/keycode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,18 @@ lazy_static! {

}

#[allow(unused)]
pub fn vk_to_hid(vk: u16, translate: bool) -> Option<u16> {
pub fn vk_to_hid(vk: u16) -> Option<u16> {
if let Some(&hid) = HID_TO_VK_MAP_US.get_by_right(&(vk as u8)) {
return Some(hid as u16);
} else {
return None;
}
}

#[allow(unused)] // Suppress warning about 'vk' being unused on non-windows
pub fn vk_to_hid_translate(vk: u16) -> Option<u16> {
#[cfg(windows)]
if translate {
{
let scancode: u16;
if let Some(&code) = VIRTUALKEY_OVERRIDE.get_by_left(&(vk as u8)) {
scancode = code;
Expand All @@ -340,44 +348,38 @@ pub fn vk_to_hid(vk: u16, translate: bool) -> Option<u16> {
}
}
return scancode_to_hid(scancode);
} else {
if let Some(&hid) = HID_TO_VK_MAP_US.get_by_right(&(vk as u8)) {
return Some(hid as u16);
} else {
return None;
}
}

#[cfg(not(windows))]
None
}

#[allow(unused)]
pub fn hid_to_vk(hid: u16, translate: bool) -> Option<u16> {
#[cfg(windows)]
if translate {
if let Some(scancode) = hid_to_scancode(hid) {
if let Some(&hid) = VIRTUALKEY_OVERRIDE.get_by_right(&scancode) {
return Some(hid as u16);
}
pub fn hid_to_vk(hid: u16) -> Option<u16> {
if let Some(&vk) = HID_TO_VK_MAP_US.get_by_left(&(hid as u8)) {
return Some(vk as u16);
} else {
return None;
}
}

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

if (vk == 0) {
return None;
}
use winapi::um::winuser::MapVirtualKeyA;
let vk: u32 = unsafe { MapVirtualKeyA(scancode.into(), 3) };

return Some(vk as u16);
} else {
if vk == 0 {
return None;
}

return Some(vk as u16);
} else {
if let Some(&vk) = HID_TO_VK_MAP_US.get_by_left(&(hid as u8)) {
return Some(vk as u16);
} else {
return None;
}
return None;
}

#[cfg(not(windows))]
Expand Down Expand Up @@ -425,8 +427,8 @@ pub fn code_to_hid(code: u16, mode: &KeycodeType) -> Option<u16> {
}
}
KeycodeType::ScanCode1 => scancode_to_hid(code),
KeycodeType::VirtualKey => vk_to_hid(code, false),
KeycodeType::VirtualKeyTranslate => vk_to_hid(code, true),
KeycodeType::VirtualKey => vk_to_hid(code),
KeycodeType::VirtualKeyTranslate => vk_to_hid_translate(code),
}
}

Expand All @@ -448,8 +450,8 @@ pub fn hid_to_code(code: u16, mode: &KeycodeType) -> Option<u16> {
}
}
KeycodeType::ScanCode1 => hid_to_scancode(code),
KeycodeType::VirtualKey => hid_to_vk(code, false),
KeycodeType::VirtualKeyTranslate => hid_to_vk(code, true),
KeycodeType::VirtualKey => hid_to_vk(code),
KeycodeType::VirtualKeyTranslate => hid_to_vk_translate(code),
}
}

Expand All @@ -467,7 +469,11 @@ mod tests {
KeycodeType::VirtualKeyTranslate,
];
#[cfg(not(windows))]
let keycode_types = [KeycodeType::HID, KeycodeType::ScanCode1];
let keycode_types = [
KeycodeType::HID,
KeycodeType::ScanCode1,
KeycodeType::VirtualKey,
];
for code in 0..0xFFFF {
let prefix = (code & 0xFF00) >> 8;
match prefix {
Expand Down