mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-06-15 12:12:01 +08:00
refactor: 删除部分多余的代码和注释
This commit is contained in:
@@ -1,50 +1,37 @@
|
||||
//! HID event types for keyboard and mouse
|
||||
//! Keyboard/mouse/consumer structs (`KeyboardEvent`, `MouseEvent`, …).
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::keyboard::CanonicalKey;
|
||||
|
||||
/// Keyboard event type
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum KeyEventType {
|
||||
/// Key pressed down
|
||||
Down,
|
||||
/// Key released
|
||||
Up,
|
||||
}
|
||||
|
||||
/// Keyboard modifier flags
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct KeyboardModifiers {
|
||||
/// Left Control
|
||||
#[serde(default)]
|
||||
pub left_ctrl: bool,
|
||||
/// Left Shift
|
||||
#[serde(default)]
|
||||
pub left_shift: bool,
|
||||
/// Left Alt
|
||||
#[serde(default)]
|
||||
pub left_alt: bool,
|
||||
/// Left Meta (Windows/Super key)
|
||||
#[serde(default)]
|
||||
pub left_meta: bool,
|
||||
/// Right Control
|
||||
#[serde(default)]
|
||||
pub right_ctrl: bool,
|
||||
/// Right Shift
|
||||
#[serde(default)]
|
||||
pub right_shift: bool,
|
||||
/// Right Alt (AltGr)
|
||||
#[serde(default)]
|
||||
pub right_alt: bool,
|
||||
/// Right Meta
|
||||
#[serde(default)]
|
||||
pub right_meta: bool,
|
||||
}
|
||||
|
||||
impl KeyboardModifiers {
|
||||
/// Convert to USB HID modifier byte
|
||||
pub fn to_hid_byte(&self) -> u8 {
|
||||
let mut byte = 0u8;
|
||||
if self.left_ctrl {
|
||||
@@ -74,7 +61,6 @@ impl KeyboardModifiers {
|
||||
byte
|
||||
}
|
||||
|
||||
/// Create from USB HID modifier byte
|
||||
pub fn from_hid_byte(byte: u8) -> Self {
|
||||
Self {
|
||||
left_ctrl: byte & 0x01 != 0,
|
||||
@@ -88,7 +74,6 @@ impl KeyboardModifiers {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if any modifier is active
|
||||
pub fn any(&self) -> bool {
|
||||
self.left_ctrl
|
||||
|| self.left_shift
|
||||
@@ -101,21 +86,16 @@ impl KeyboardModifiers {
|
||||
}
|
||||
}
|
||||
|
||||
/// Keyboard event
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct KeyboardEvent {
|
||||
/// Event type (down/up)
|
||||
#[serde(rename = "type")]
|
||||
pub event_type: KeyEventType,
|
||||
/// Canonical keyboard key identifier shared across frontend and backend
|
||||
pub key: CanonicalKey,
|
||||
/// Modifier keys state
|
||||
#[serde(default)]
|
||||
pub modifiers: KeyboardModifiers,
|
||||
}
|
||||
|
||||
impl KeyboardEvent {
|
||||
/// Create a key down event
|
||||
pub fn key_down(key: CanonicalKey, modifiers: KeyboardModifiers) -> Self {
|
||||
Self {
|
||||
event_type: KeyEventType::Down,
|
||||
@@ -124,7 +104,6 @@ impl KeyboardEvent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a key up event
|
||||
pub fn key_up(key: CanonicalKey, modifiers: KeyboardModifiers) -> Self {
|
||||
Self {
|
||||
event_type: KeyEventType::Up,
|
||||
@@ -134,7 +113,6 @@ impl KeyboardEvent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Mouse button
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum MouseButton {
|
||||
@@ -146,7 +124,6 @@ pub enum MouseButton {
|
||||
}
|
||||
|
||||
impl MouseButton {
|
||||
/// Convert to USB HID button bit
|
||||
pub fn to_hid_bit(&self) -> u8 {
|
||||
match self {
|
||||
MouseButton::Left => 0x01,
|
||||
@@ -158,44 +135,31 @@ impl MouseButton {
|
||||
}
|
||||
}
|
||||
|
||||
/// Mouse event type
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum MouseEventType {
|
||||
/// Mouse moved (relative movement)
|
||||
Move,
|
||||
/// Mouse moved (absolute position)
|
||||
MoveAbs,
|
||||
/// Button pressed
|
||||
Down,
|
||||
/// Button released
|
||||
Up,
|
||||
/// Mouse wheel scroll
|
||||
Scroll,
|
||||
}
|
||||
|
||||
/// Mouse event
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MouseEvent {
|
||||
/// Event type
|
||||
#[serde(rename = "type")]
|
||||
pub event_type: MouseEventType,
|
||||
/// X coordinate or delta
|
||||
#[serde(default)]
|
||||
pub x: i32,
|
||||
/// Y coordinate or delta
|
||||
#[serde(default)]
|
||||
pub y: i32,
|
||||
/// Button (for down/up events)
|
||||
#[serde(default)]
|
||||
pub button: Option<MouseButton>,
|
||||
/// Scroll delta (for scroll events)
|
||||
#[serde(default)]
|
||||
pub scroll: i8,
|
||||
}
|
||||
|
||||
impl MouseEvent {
|
||||
/// Create a relative move event
|
||||
pub fn move_rel(dx: i32, dy: i32) -> Self {
|
||||
Self {
|
||||
event_type: MouseEventType::Move,
|
||||
@@ -206,7 +170,6 @@ impl MouseEvent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an absolute move event
|
||||
pub fn move_abs(x: i32, y: i32) -> Self {
|
||||
Self {
|
||||
event_type: MouseEventType::MoveAbs,
|
||||
@@ -217,7 +180,6 @@ impl MouseEvent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a button down event
|
||||
pub fn button_down(button: MouseButton) -> Self {
|
||||
Self {
|
||||
event_type: MouseEventType::Down,
|
||||
@@ -228,7 +190,6 @@ impl MouseEvent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a button up event
|
||||
pub fn button_up(button: MouseButton) -> Self {
|
||||
Self {
|
||||
event_type: MouseEventType::Up,
|
||||
@@ -239,7 +200,6 @@ impl MouseEvent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a scroll event
|
||||
pub fn scroll(delta: i8) -> Self {
|
||||
Self {
|
||||
event_type: MouseEventType::Scroll,
|
||||
@@ -251,35 +211,19 @@ impl MouseEvent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Combined HID event (keyboard or mouse)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "device", rename_all = "lowercase")]
|
||||
pub enum HidEvent {
|
||||
Keyboard(KeyboardEvent),
|
||||
Mouse(MouseEvent),
|
||||
Consumer(ConsumerEvent),
|
||||
}
|
||||
|
||||
/// Consumer control event (multimedia keys)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ConsumerEvent {
|
||||
/// Consumer control usage code (e.g., 0x00CD for Play/Pause)
|
||||
pub usage: u16,
|
||||
}
|
||||
|
||||
/// USB HID keyboard report (8 bytes)
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct KeyboardReport {
|
||||
/// Modifier byte
|
||||
pub modifiers: u8,
|
||||
/// Reserved byte
|
||||
pub reserved: u8,
|
||||
/// Key codes (up to 6 simultaneous keys)
|
||||
pub keys: [u8; 6],
|
||||
}
|
||||
|
||||
impl KeyboardReport {
|
||||
/// Convert to bytes for USB HID
|
||||
pub fn to_bytes(&self) -> [u8; 8] {
|
||||
[
|
||||
self.modifiers,
|
||||
@@ -293,7 +237,6 @@ impl KeyboardReport {
|
||||
]
|
||||
}
|
||||
|
||||
/// Add a key to the report
|
||||
pub fn add_key(&mut self, key: u8) -> bool {
|
||||
for slot in &mut self.keys {
|
||||
if *slot == 0 {
|
||||
@@ -304,56 +247,21 @@ impl KeyboardReport {
|
||||
false // All slots full
|
||||
}
|
||||
|
||||
/// Remove a key from the report
|
||||
pub fn remove_key(&mut self, key: u8) {
|
||||
for slot in &mut self.keys {
|
||||
if *slot == key {
|
||||
*slot = 0;
|
||||
}
|
||||
}
|
||||
// Compact the array
|
||||
self.keys.sort_by(|a, b| b.cmp(a));
|
||||
}
|
||||
|
||||
/// Clear all keys
|
||||
pub fn clear(&mut self) {
|
||||
self.modifiers = 0;
|
||||
self.keys = [0; 6];
|
||||
}
|
||||
}
|
||||
|
||||
/// USB HID mouse report
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct MouseReport {
|
||||
/// Button state
|
||||
pub buttons: u8,
|
||||
/// X movement (-127 to 127)
|
||||
pub x: i8,
|
||||
/// Y movement (-127 to 127)
|
||||
pub y: i8,
|
||||
/// Wheel movement (-127 to 127)
|
||||
pub wheel: i8,
|
||||
}
|
||||
|
||||
impl MouseReport {
|
||||
/// Convert to bytes for USB HID (relative mouse)
|
||||
pub fn to_bytes_relative(&self) -> [u8; 4] {
|
||||
[self.buttons, self.x as u8, self.y as u8, self.wheel as u8]
|
||||
}
|
||||
|
||||
/// Convert to bytes for USB HID (absolute mouse)
|
||||
pub fn to_bytes_absolute(&self, x: u16, y: u16) -> [u8; 6] {
|
||||
[
|
||||
self.buttons,
|
||||
(x & 0xFF) as u8,
|
||||
(x >> 8) as u8,
|
||||
(y & 0xFF) as u8,
|
||||
(y >> 8) as u8,
|
||||
self.wheel as u8,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user