smithay::input::keyboard::xkb

Struct State

pub struct State { /* private fields */ }
Expand description

Keyboard state object.

State objects contain the active state of a keyboard (or keyboards), such as the currently effective layout and the active modifiers. It acts as a simple state machine, wherein key presses and releases are the input, and key symbols (keysyms) are the output.

Implementations§

§

impl State

pub unsafe fn from_raw_ptr(ptr: *mut xkb_state) -> State

pub fn get_raw_ptr(&self) -> *mut xkb_state

pub fn new(keymap: &Keymap) -> State

Create a new keyboard state object from a keymap.

pub fn get_keymap(&self) -> Keymap

Get the keymap which a keyboard state object is using.

Returns the keymap which was passed to xkb_state_new() when creating this state object.

This keymap can safely be used beyond the lifetime of this state

pub fn update_key(&mut self, key: KeyCode, direction: KeyDirection) -> u32

Update the keyboard state to reflect a given key being pressed or released.

This entry point is intended for programs which track the keyboard state explictly (like an evdev client). If the state is serialized to you by a master process (like a Wayland compositor) using functions like xkb_state_serialize_mods(), you should use xkb_state_update_mask() instead. The two functins should not generally be used together.

A series of calls to this function should be consistent; that is, a call with xkb::KEY_DOWN for a key should be matched by an xkb::KEY_UP; if a key is pressed twice, it should be released twice; etc. Otherwise (e.g. due to missed input events), situations like “stuck modifiers” may occur.

This function is often used in conjunction with the function xkb_state_key_get_syms() (or xkb_state_key_get_one_sym()), for example, when handling a key event. In this case, you should prefer to get the keysyms before updating the key, such that the keysyms reported for the key event are not affected by the event itself. This is the conventional behavior.

Returns A mask of state components that have changed as a result of the update. If nothing in the state has changed, returns 0.

pub fn update_mask( &mut self, depressed_mods: u32, latched_mods: u32, locked_mods: u32, depressed_layout: u32, latched_layout: u32, locked_layout: u32, ) -> u32

Update a keyboard state from a set of explicit masks.

This entry point is intended for window systems and the like, where a master process holds an xkb_state, then serializes it over a wire protocol, and clients then use the serialization to feed in to their own xkb_state.

All parameters must always be passed, or the resulting state may be incoherent.

The serialization is lossy and will not survive round trips; it must only be used to feed slave state objects, and must not be used to update the master state.

If you do not fit the description above, you should use xkb_state_update_key() instead. The two functions should not generally be used together.

Returns a mask of state components that have changed as a result of the update. If nothing in the state has changed, returns 0.

pub fn key_get_syms(&self, key: KeyCode) -> &[Keysym]

Get the keysyms obtained from pressing a particular key in a given keyboard state.

Get the keysyms for a key according to the current active layout, modifiers and shift level for the key, as determined by a keyboard state.

§Arguments
  • state: The keyboard state object.
  • key: The keycode of the key.
§Return
  • syms_out: An immutable array of keysyms corresponding the key in the given keyboard state.

As an extension to XKB, this function can return more than one keysym. If you do not want to handle this case, you should use xkb_state_key_get_one_sym(), which additionally performs transformations which are specific to the one-keysym case.

pub fn key_get_utf8(&self, key: KeyCode) -> String

Get the Unicode/UTF-8 string obtained from pressing a particular key in a given keyboard state.

pub fn key_get_utf32(&self, key: KeyCode) -> u32

Get the Unicode/UTF-32 codepoint obtained from pressing a particular key in a a given keyboard state.

Returns The UTF-32 representation for the key, if it consists of only a single codepoint. Otherwise, returns 0.

pub fn key_get_one_sym(&self, key: KeyCode) -> Keysym

Get the single keysym obtained from pressing a particular key in a given keyboard state.

This function is similar to xkb_state_key_get_syms(), but intended for users which cannot or do not want to handle the case where multiple keysyms are returned (in which case this function is preferred).

Returns the keysym. If the key does not have exactly one keysym, returns xkb::KEY_NoSymbol.

pub fn key_get_layout(&self, key: KeyCode) -> u32

Get the effective layout index for a key in a given keyboard state.

Returns the layout index for the key in the given keyboard state. If the given keycode is invalid, or if the key is not included in any layout at all, returns xkb::LAYOUT_INVALID.

pub fn key_get_level(&self, key: KeyCode, layout: u32) -> u32

Get the effective shift level for a key in a given keyboard state and layout.

Return the shift level index. If the key or layout are invalid, returns xkb::LEVEL_INVALID.

pub fn serialize_mods(&self, components: u32) -> u32

The counterpart to xkb_state_update_mask for modifiers, to be used on the server side of serialization.

State components other than xkb::STATE_MODS_* are ignored. If xkb::STATE_MODS_EFFECTIVE is included, all other state components are ignored.

Returns a ModMask representing the given components of the modifier state.

This function should not be used in regular clients; please use the xkb::State::mod_*_is_active API instead.

pub fn serialize_layout(&self, components: u32) -> u32

pub fn mod_name_is_active<S>(&self, name: &S, type_: u32) -> bool
where S: Borrow<str> + ?Sized,

Test whether a modifier is active in a given keyboard state by name.

pub fn mod_index_is_active(&self, idx: u32, type_: u32) -> bool

Test whether a modifier is active in a given keyboard state by index.

pub fn mod_index_is_consumed(&self, key: KeyCode, idx: u32) -> bool

Test whether a modifier is consumed by keyboard state translation for a key.

Some functions, like xkb_state_key_get_syms(), look at the state of the modifiers in the keymap and derive from it the correct shift level to use for the key. For example, in a US layout, pressing the key labeled <A> while the Shift modifier is active, generates the keysym ‘A’. In this case, the Shift modifier is said to be consumed. However, the Num Lock modifier does not affect this translation at all, even if it active, so it is not consumed by this translation.

It may be desirable for some application to not reuse consumed modifiers for further processing, e.g. for hotkeys or keyboard shortcuts. To understand why, consider some requirements from a standard shortcut mechanism, and how they are implemented:

  1. The shortcut’s modifiers must match exactly to the state. For example, it is possible to bind separate actions to <Alt><Tab> and to <Alt><Shift><Tab>. Further, if only <Alt><Tab> is bound to an action, pressing <Alt><Shift><Tab> should not trigger the shortcut. Effectively, this means that the modifiers are compared using the equality operator (==).
  2. Only relevant modifiers are considered for the matching. For example, Caps Lock and Num Lock should not generally affect the matching, e.g. when matching <Alt><Tab> against the state, it does not matter whether Num Lock is active or not. These relevant, or significant, modifiers usually include Alt, Control, Shift, Super and similar. Effectively, this means that non-significant modifiers are masked out, before doing the comparison as described above.
  3. The matching must be independent of the layout/keymap. For example, the <Plus> (+) symbol is found on the first level on some layouts, and requires holding Shift on others. If you simply bind the action to the <Plus> keysym, it would work for the unshifted kind, but not for the others, because the match against Shift would fail. If you bind the action to <Shift><Plus>, only the shifted kind would work. So what is needed is to recognize that Shift is used up in the translation of the keysym itself, and therefore should not be included in the matching. Effectively, this means that consumed modifiers (Shift in this example) are masked out as well, before doing the comparison.

state_modifiers are the modifiers reported by xkb::State::mod_index_is_active() and similar functions. consumed_modifiers are the modifiers reported by xkb::State::mod_index_is_consumed(). significant_modifiers are decided upon by the application/toolkit/user; it is up to them to decide whether these are configurable or hard-coded.

pub fn mod_mask_remove_consumed(&self, key: KeyCode, mask: u32) -> u32

Remove consumed modifiers from a modifier mask for a key.

Takes the given modifier mask, and removes all modifiers which are consumed for that particular key (as in xkb_state_mod_index_is_consumed()).

pub fn key_get_consumed_mods(&self, key: KeyCode) -> u32

Get the mask of modifiers consumed by translating a given key.

Returns a mask of the consumed modifiers.

pub fn layout_name_is_active<S>(&self, name: &S, type_: u32) -> bool
where S: Borrow<str> + ?Sized,

Test whether a layout is active in a given keyboard state by name.

If multiple layouts in the keymap have this name, the one with the lowest index is tested.

pub fn layout_index_is_active(&self, idx: u32, type_: u32) -> bool

Test whether a layout is active in a given keyboard state by index.

pub fn led_name_is_active<S>(&self, name: &S) -> bool
where S: Borrow<str> + ?Sized,

Test whether a LED is active in a given keyboard state by name.

pub fn led_index_is_active(&self, idx: u32) -> bool

Test whether a LED is active in a given keyboard state by index.

Trait Implementations§

§

impl Clone for State

§

fn clone(&self) -> State

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
§

impl Drop for State

§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for State

§

impl RefUnwindSafe for State

§

impl !Send for State

§

impl !Sync for State

§

impl Unpin for State

§

impl UnwindSafe for State

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, dst: *mut T)

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

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more