Enum VKey

Source
pub enum VKey {
Show 153 variants Back, Tab, Clear, Return, Shift, Control, Menu, Pause, Capital, Escape, Space, Prior, Next, End, Home, Left, Up, Right, Down, Select, Print, Execute, Snapshot, Insert, Delete, Help, LWin, RWin, Apps, Sleep, Numpad0, Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, Multiply, Add, Separator, Subtract, Decimal, Divide, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, Numlock, Scroll, LShift, RShift, LControl, RControl, LMenu, RMenu, BrowserBack, BrowserForward, BrowserRefresh, BrowserStop, BrowserSearch, BrowserFavorites, BrowserHome, VolumeMute, VolumeDown, VolumeUp, MediaNextTrack, MediaPrevTrack, MediaStop, MediaPlayPause, LaunchMail, LaunchMediaSelect, LaunchApp1, LaunchApp2, Oem1, OemPlus, OemComma, OemMinus, OemPeriod, Oem2, Oem3, Oem4, Oem5, Oem6, Oem7, Oem8, Oem102, Attn, Crsel, Exsel, Play, Zoom, Pa1, OemClear, Vk0, Vk1, Vk2, Vk3, Vk4, Vk5, Vk6, Vk7, Vk8, Vk9, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, CustomKeyCode(u16),
}
Expand description

Represents a virtual key (VK) code.

§See Also

Variants§

§

Back

§

Tab

§

Clear

§

Return

§

Shift

§

Control

§

Menu

§

Pause

§

Capital

§

Escape

§

Space

§

Prior

§

Next

§

End

§

Home

§

Left

§

Up

§

Right

§

Down

§

Select

§

Print

§

Execute

§

Snapshot

§

Insert

§

Delete

§

Help

§

LWin

§

RWin

§

Apps

§

Sleep

§

Numpad0

§

Numpad1

§

Numpad2

§

Numpad3

§

Numpad4

§

Numpad5

§

Numpad6

§

Numpad7

§

Numpad8

§

Numpad9

§

Multiply

§

Add

§

Separator

§

Subtract

§

Decimal

§

Divide

§

F1

§

F2

§

F3

§

F4

§

F5

§

F6

§

F7

§

F8

§

F9

§

F10

§

F11

§

F12

§

F13

§

F14

§

F15

§

F16

§

F17

§

F18

§

F19

§

F20

§

F21

§

F22

§

F23

§

F24

§

Numlock

§

Scroll

§

LShift

§

RShift

§

LControl

§

RControl

§

LMenu

§

RMenu

§

BrowserBack

§

BrowserForward

§

BrowserRefresh

§

BrowserStop

§

BrowserSearch

§

BrowserFavorites

§

BrowserHome

§

VolumeMute

§

VolumeDown

§

VolumeUp

§

MediaNextTrack

§

MediaPrevTrack

§

MediaStop

§

MediaPlayPause

§

LaunchMail

§

LaunchMediaSelect

§

LaunchApp1

§

LaunchApp2

§

Oem1

§

OemPlus

§

OemComma

§

OemMinus

§

OemPeriod

§

Oem2

§

Oem3

§

Oem4

§

Oem5

§

Oem6

§

Oem7

§

Oem8

§

Oem102

§

Attn

§

Crsel

§

Exsel

§

Play

§

Zoom

§

Pa1

§

OemClear

§

Vk0

§

Vk1

§

Vk2

§

Vk3

§

Vk4

§

Vk5

§

Vk6

§

Vk7

§

Vk8

§

Vk9

§

A

§

B

§

C

§

D

§

E

§

F

§

G

§

H

§

I

§

J

§

K

§

L

§

M

§

N

§

O

§

P

§

Q

§

R

§

S

§

T

§

U

§

V

§

W

§

X

§

Y

§

Z

§

CustomKeyCode(u16)

Implementations§

Source§

impl VKey

Source

pub const fn to_vk_code(&self) -> u16

Converts a VKey to its corresponding Windows Virtual-Key (VK) code.

§See Also
Source

pub const fn from_vk_code(vk_code: u16) -> VKey

Returns a VKey based a Windows Virtual-Key (VK) code.

§See Also
Examples found in repository?
examples/vkeys.rs (line 11)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
fn main() {
    // Create HotkeyManager
    let mut hkm = HotkeyManager::<()>::new();

    let vk_a1 = VKey::A;
    let vk_a2 = VKey::from_keyname("a").unwrap();
    let vk_a3 = VKey::from_vk_code(0x41);
    let vk_a4 = VKey::from_vk_code(VK_A.0);

    // Create custom keycode equivalent to A
    let vk_a5 = VKey::CustomKeyCode(0x41);

    assert_eq!(vk_a1, vk_a2);
    assert_eq!(vk_a1, vk_a3);
    assert_eq!(vk_a1, vk_a4);
    assert_eq!(vk_a1, vk_a5);

    // NOTE
    // When matching `CustomKeyCodes` you must include a guard if statement
    match vk_a5 {
        VKey::A => {
            // This will never show up
            println!("CustomKeyCode(0x41) matches against VKey::A");
        }
        _ => {
            println!("You didn't use the match statement correctly!");
        }
    }

    // Instead match like this
    match vk_a5 {
        _ if VKey::A == vk_a5 => {
            // This will match
            println!("CustomKeyCode(0x41) matches against VKey::A");
        }
        _ => {}
    }

    hkm.register_hotkey(vk_a5, &[], || {
        println!("You pressed A");
    })
    .unwrap();

    hkm.event_loop();
}
More examples
Hide additional examples
examples/simple.rs (line 17)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fn main() {
    let mut hkm = HotkeyManager::new();

    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
    let trigger_key = VKey::from_keyname("a").unwrap();
    let mod_key = VKey::from_keyname("alt").unwrap();
    hkm.register_hotkey(trigger_key, &[mod_key], || {
        println!("Hotkey ALT + A was pressed");
    })
    .unwrap();

    // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
    let trigger_key = VKey::from_keyname("b").unwrap();
    let modifiers = &[VKey::from_vk_code(0x87)];
    hkm.register_hotkey(trigger_key, modifiers, || {
        println!("Hotkey F24 + B was pressed");
    })
    .unwrap();

    // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
    hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
        println!("Hotkey WIN + ALT + C was pressed");
    })
    .unwrap();

    // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
    let hotkey_id = hkm
        .register_hotkey(
            VKey::from_vk_code(0x44),
            &[VKey::from_vk_code(0xA4)],
            || {
                println!("Hotkey ALT + D was pressed");
            },
        )
        .unwrap();

    // Unregister hotkey with ID
    hkm.unregister_hotkey(hotkey_id);

    // Run the event handler in a blocking loop. This will block forever and execute the set
    // callbacks when the registered hotkeys are detected
    hkm.event_loop();
}
Source

pub fn from_keyname(name: &str) -> Result<VKey, WHKError>

Creates a VKey from a string representation of the key.

NOTE: Certain common aliases for keys are accepted in addition to the Microsoft Virtual-Key Codes names

WIN maps to VKey::LWin CTRL maps to VKey::Control ALT maps to VKey::Menu

§See Also
Examples found in repository?
examples/vkeys.rs (line 10)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
fn main() {
    // Create HotkeyManager
    let mut hkm = HotkeyManager::<()>::new();

    let vk_a1 = VKey::A;
    let vk_a2 = VKey::from_keyname("a").unwrap();
    let vk_a3 = VKey::from_vk_code(0x41);
    let vk_a4 = VKey::from_vk_code(VK_A.0);

    // Create custom keycode equivalent to A
    let vk_a5 = VKey::CustomKeyCode(0x41);

    assert_eq!(vk_a1, vk_a2);
    assert_eq!(vk_a1, vk_a3);
    assert_eq!(vk_a1, vk_a4);
    assert_eq!(vk_a1, vk_a5);

    // NOTE
    // When matching `CustomKeyCodes` you must include a guard if statement
    match vk_a5 {
        VKey::A => {
            // This will never show up
            println!("CustomKeyCode(0x41) matches against VKey::A");
        }
        _ => {
            println!("You didn't use the match statement correctly!");
        }
    }

    // Instead match like this
    match vk_a5 {
        _ if VKey::A == vk_a5 => {
            // This will match
            println!("CustomKeyCode(0x41) matches against VKey::A");
        }
        _ => {}
    }

    hkm.register_hotkey(vk_a5, &[], || {
        println!("You pressed A");
    })
    .unwrap();

    hkm.event_loop();
}
More examples
Hide additional examples
examples/simple.rs (line 8)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fn main() {
    let mut hkm = HotkeyManager::new();

    // Register a system-wide hotkey with the trigger key 'A' and the modifier key 'ALT'
    let trigger_key = VKey::from_keyname("a").unwrap();
    let mod_key = VKey::from_keyname("alt").unwrap();
    hkm.register_hotkey(trigger_key, &[mod_key], || {
        println!("Hotkey ALT + A was pressed");
    })
    .unwrap();

    // Register a system-wide hotkey with the trigger key 'B' and the modifier key 'f24'
    let trigger_key = VKey::from_keyname("b").unwrap();
    let modifiers = &[VKey::from_vk_code(0x87)];
    hkm.register_hotkey(trigger_key, modifiers, || {
        println!("Hotkey F24 + B was pressed");
    })
    .unwrap();

    // Register a system-wide hotkey with the trigger key 'C' and multiple modifier key
    hkm.register_hotkey(VKey::C, &[VKey::LWin, VKey::Menu], || {
        println!("Hotkey WIN + ALT + C was pressed");
    })
    .unwrap();

    // Register and store id for system-wide hotkey with trigger key 'D' and modifier key 'ALT'
    let hotkey_id = hkm
        .register_hotkey(
            VKey::from_vk_code(0x44),
            &[VKey::from_vk_code(0xA4)],
            || {
                println!("Hotkey ALT + D was pressed");
            },
        )
        .unwrap();

    // Unregister hotkey with ID
    hkm.unregister_hotkey(hotkey_id);

    // Run the event handler in a blocking loop. This will block forever and execute the set
    // callbacks when the registered hotkeys are detected
    hkm.event_loop();
}

Trait Implementations§

Source§

impl Clone for VKey

Source§

fn clone(&self) -> VKey

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
Source§

impl Debug for VKey

Source§

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

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

impl Display for VKey

Source§

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

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

impl FromStr for VKey

Source§

type Err = WHKError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for VKey

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 VKey

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Copy for VKey

Source§

impl Eq for VKey

Auto Trait Implementations§

§

impl Freeze for VKey

§

impl RefUnwindSafe for VKey

§

impl Send for VKey

§

impl Sync for VKey

§

impl Unpin for VKey

§

impl UnwindSafe for VKey

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 u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.