Skip to main content

HidUsage

Struct HidUsage 

Source
pub struct HidUsage { /* private fields */ }
Expand description

Decoded ZMK HID usage values used in typed behavior APIs. Lossless decoded ZMK HID usage value (base usage + modifiers).

Implementations§

Source§

impl HidUsage

Source

pub fn from_encoded(encoded: u32) -> Self

Decode from ZMK’s encoded usage format.

ZMK encodes as:

  • bits 31:24: modifiers
  • bits 23:16: usage page
  • bits 15:00: usage id

If page is 0, ZMK treats it as keyboard page (0x07).

Examples found in repository?
examples/basic_example.rs (line 116)
71fn run_example<T: Read + Write>(mut client: StudioClient<T>) -> Result<(), Box<dyn Error>> {
72    let info = client.get_device_info()?;
73    println!("Device: {}", info.name);
74    println!("Lock: {:?}", client.get_lock_state()?);
75
76    let behavior_ids = client.list_all_behaviors()?;
77    println!("Behavior count: {}", behavior_ids.len());
78    if let Some(first_behavior_id) = behavior_ids.first().copied() {
79        let details = client.get_behavior_details(first_behavior_id)?;
80        println!("First behavior: {} ({})", details.id, details.display_name);
81    }
82
83    let keymap = match client.get_keymap() {
84        Ok(keymap) => keymap,
85        Err(ClientError::Meta(_)) => {
86            println!("Keymap request denied (likely locked); press `&studio_unlock` then rerun.");
87            return Ok(());
88        }
89        Err(err) => return Err(Box::new(err)),
90    };
91    println!("Layers: {}", keymap.layers.len());
92
93    let layouts = client.get_physical_layouts()?;
94    println!(
95        "Physical layouts: {} (active index: {})",
96        layouts.layouts.len(),
97        layouts.active_layout_index
98    );
99
100    let Some(first_layer) = keymap.layers.first() else {
101        return Ok(());
102    };
103    if first_layer.bindings.is_empty() {
104        return Ok(());
105    }
106
107    let layer_id = first_layer.id;
108    let key_position = 0;
109
110    let before = client.get_key_at(layer_id, key_position)?;
111    println!("Before: {before:?}");
112
113    client.set_key_at(
114        layer_id,
115        key_position,
116        Behavior::KeyPress(HidUsage::from_encoded(Keycode::A.to_hid_usage())),
117    )?;
118    let after = client.get_key_at(layer_id, key_position)?;
119    println!("After:  {after:?}");
120
121    // Change management APIs.
122    let has_changes = client.check_unsaved_changes()?;
123    println!("Unsaved changes: {has_changes}");
124    if has_changes {
125        client.discard_changes()?;
126    }
127
128    Ok(())
129}
Source

pub fn from_parts(page: u16, id: u16, modifiers: u8) -> Self

Source

pub fn to_hid_usage(self) -> u32

Source

pub fn page(self) -> u16

Source

pub fn id(self) -> u16

Source

pub fn modifiers(self) -> u8

Source

pub fn base(self) -> Self

Source

pub fn known_keycode(self) -> Option<Keycode>

Source

pub fn known_base_keycode(self) -> Option<Keycode>

Source

pub fn modifier_labels(self) -> Vec<&'static str>

Trait Implementations§

Source§

impl Clone for HidUsage

Source§

fn clone(&self) -> HidUsage

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for HidUsage

Source§

impl Debug for HidUsage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for HidUsage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for HidUsage

Source§

impl Hash for HidUsage

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for HidUsage

Source§

fn eq(&self, other: &HidUsage) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for HidUsage

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.