VKey

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(i32),
}
Expand description

Virtual Key Code wrapper. The codes and variants follow the virtual key codes. Not supported as enum variants are the mouse buttons, IME keys, VK_PACKET and VK_NONAME. The letter keys (A to Z) are added as additionall variants, as well as the number keys (0 to 9) which are available as Vk0 to Vk9.

A VKey can be created for any arbitrary keycode by using the CustomKeyCode variant.

See: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

§Note

Matching against a VKey can be problematic since all of the variants can also be represented using the CustomKeyCode variant. If a reliable check for a VKey is needed, the keycode from the VKey::to_vk_code function should be used to get the unique keycode.

Variants§

§

Back

Backspace key

§

Tab

Tab key

§

Clear

CLEAR key

§

Return

ENTER key

§

Shift

Shift key

§

Control

CTRL key

§

Menu

ALT key

§

Pause

PAUSE

§

Capital

CAPS LOCK key

§

Escape

ESC key

§

Space

SPACEBAR

§

Prior

PAGE UP key

§

Next

PAGE DOWN key

§

End

END key

§

Home

HOME key

§

Left

LEFT ARROW key

§

Up

UP ARROW key

§

Right

RIGHT ARROW key

§

Down

DOWN ARROW key

§

Select

SELECT key

§

Print

PRINT key

§

Execute

EXECUTE key

§

Snapshot

PRINT SCREEN key

§

Insert

INS key

§

Delete

DEL key

§

Help

HELP key

§

LWin

Left Windows key (Natural keyboard)

§

RWin

Right Windows key (Natural keyboard)

§

Apps

Applications key (Natural keyboard)

§

Sleep

Computer Sleep key

§

Numpad0

Numeric keypad 0 key

§

Numpad1

Numeric keypad 1 key

§

Numpad2

Numeric keypad 2 key

§

Numpad3

Numeric keypad 3 key

§

Numpad4

Numeric keypad 4 key

§

Numpad5

Numeric keypad 5 key

§

Numpad6

Numeric keypad 6 key

§

Numpad7

Numeric keypad 7 key

§

Numpad8

Numeric keypad 8 key

§

Numpad9

Numeric keypad 9 key

§

Multiply

Multiply key

§

Add

Add key

§

Separator

Separator key

§

Subtract

Subtract key

§

Decimal

Decimal key

§

Divide

Divide key

§

F1

F1 key

§

F2

F2 key

§

F3

F3 key

§

F4

F4 key

§

F5

F5 key

§

F6

F6 key

§

F7

F7 key

§

F8

F8 key

§

F9

F9 key

§

F10

F10 key

§

F11

F11 key

§

F12

F12 key

§

F13

F13 key

§

F14

F14 key

§

F15

F15 key

§

F16

F16 key

§

F17

F17 key

§

F18

F18 key

§

F19

F19 key

§

F20

F20 key

§

F21

F21 key

§

F22

F22 key

§

F23

F23 key

§

F24

F24 key

§

Numlock

NUM LOCK key

§

Scroll

SCROLL LOCK key

§

LShift

Left SHIFT key

§

RShift

Right SHIFT key

§

LControl

Left CONTROL key

§

RControl

Right CONTROL key

§

LMenu

Left ALT key

§

RMenu

Right ALT key

§

BrowserBack

Browser Back key

§

BrowserForward

Browser Forward key

§

BrowserRefresh

Browser Refresh key

§

BrowserStop

Browser Stop key

§

BrowserSearch

Browser Search key

§

BrowserFavorites

Browser Favorites key

§

BrowserHome

Browser Start and Home key

§

VolumeMute

Volume Mute key

§

VolumeDown

Volume Down key

§

VolumeUp

Volume Up key

§

MediaNextTrack

Next Track key

§

MediaPrevTrack

Previous Track key

§

MediaStop

Stop Media key

§

MediaPlayPause

Play/Pause Media key

§

LaunchMail

Start Mail key

§

LaunchMediaSelect

Select Media key

§

LaunchApp1

Start Application 1 key

§

LaunchApp2

Start Application 2 key

§

Oem1

Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ;: key

§

OemPlus

For any country/region, the + key

§

OemComma

For any country/region, the , key

§

OemMinus

For any country/region, the - key

§

OemPeriod

For any country/region, the . key

§

Oem2

Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the /? key

§

Oem3

Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ~ key

§

Oem4

Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the [{ key

§

Oem5

Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the \| key

§

Oem6

Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ]} key

§

Oem7

Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the "' key

§

Oem8

Used for miscellaneous characters; it can vary by keyboard.

§

Oem102

The <> keys on the US standard keyboard, or the \\| key on the non-US 102-key keyboard

§

Attn

Attn key

§

Crsel

CrSel key

§

Exsel

ExSel key

§

Play

Play key

§

Zoom

Zoom key

§

Pa1

PA1 key

§

OemClear

Clear key

§

Vk0

0 key

§

Vk1

1 key

§

Vk2

2 key

§

Vk3

3 key

§

Vk4

4 key

§

Vk5

5 key

§

Vk6

6 key

§

Vk7

7 key

§

Vk8

8 key

§

Vk9

9 key

§

A

A key

§

B

B key

§

C

C key

§

D

D key

§

E

E key

§

F

F key

§

G

G key

§

H

H key

§

I

I key

§

J

J key

§

K

K key

§

L

L key

§

M

M key

§

N

N key

§

O

O key

§

P

P key

§

Q

Q key

§

R

R key

§

S

S key

§

T

T key

§

U

U key

§

V

V key

§

W

W key

§

X

X key

§

Y

Y key

§

Z

Z key

§

CustomKeyCode(i32)

Virtual key specified by the actual keycode. This can be used to create a VKey for keys that are not covered by the other enum variants.

See: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

Implementations§

Source§

impl VKey

Source

pub const fn from_char(ch: char) -> Result<Self, HkError>

Try to create a VKey from a char. This only works for the simple number and letter keys (‘A’ to ‘Z’ and ‘0’ to ‘9’). Letters can be upper or lower case

Examples found in repository?
examples/vkeys-and-modkeys.rs (line 16)
7fn main() {
8    // Create a HotkeyManager
9    let mut hkm = HotkeyManager::<()>::new();
10
11    // Create VKey from the enum variant (recommended if possible)
12    let vk_b1 = VKey::B;
13    let vk_up1 = VKey::Up;
14
15    // Create VKey from the case insensitive key char (only for 'a'-'z' and '0'-'9')
16    let vk_b2 = VKey::from_char('b').unwrap();
17    let vk_b3 = VKey::from_char('B').unwrap();
18
19    // Create VKey from key name string. This works with "A"-"Z", "0"-"9", the constant names
20    // "VK_*" and hex codes prefixed with "0x*"
21    // Useful when reading hotkeys in text form through config files or user input
22    let vk_b4 = VKey::from_keyname("b").unwrap();
23    let vk_b5 = VKey::from_keyname("B").unwrap();
24    let vk_b6 = VKey::from_keyname("0x42").unwrap();
25    let vk_up2 = VKey::from_keyname("VK_UP").unwrap();
26
27    // Create VKey directly from the virtual keycode
28    // See: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
29    let vk_b7 = VKey::CustomKeyCode(0x42);
30
31    // All of the methods are comparable with equal and usable for hashing
32    assert_eq!(vk_b1, vk_b2);
33    assert_eq!(vk_b1, vk_b3);
34    assert_eq!(vk_b1, vk_b4);
35    assert_eq!(vk_b1, vk_b5);
36    assert_eq!(vk_b1, vk_b6);
37    assert_eq!(vk_b1, vk_b7);
38
39    assert_eq!(vk_up1, vk_up2);
40
41    // **Alert**
42    // Matching the variants does not always work, since a key can be represented through the enum
43    // variant, or with a `CustomKeyCode`
44    match vk_b7 {
45        VKey::B => println!(
46            "CustomKeyCode(0x42) matches against B (this will not show up, because it is not true)"
47        ),
48        VKey::A => println!("Just checking if the key matches A"),
49        _ => println!("CustomKeyCode(0x42) does not match against B"),
50    }
51
52    // Instead use if chains if this is really needed for now, at least until a better solution is
53    // implemented
54    if vk_b7 == VKey::B {
55        println!("CustomKeyCode(0x42) is equal to B");
56    } else if vk_b7 == VKey::A {
57        println!("Just checking if the key is equal to A");
58    } else {
59        println!(
60            "CustomKeyCode(0x42) is not equal to B \
61            (this will not show up, because it works fine when using if)"
62        );
63    }
64
65    // Create ModKey from the enum variant
66    let mod_alt1 = ModKey::Alt;
67    // Create ModKey from key name string
68    let mod_alt2 = ModKey::from_keyname("ALT").unwrap();
69
70    // With modkeys, there is no `CustomKeyCode`, so they can be safely matched and compared
71    assert_eq!(mod_alt1, mod_alt2);
72
73    hkm.register_extrakeys(vk_b1, &[mod_alt1], &[vk_up1], || {
74        println!("Hotkey ALT + B + UP pressed");
75    })
76    .unwrap();
77
78    hkm.event_loop();
79}
Source

pub const fn to_vk_code(&self) -> i32

Get the actual windows virtual keycode for the VKey for usage with winapi functions

Source

pub fn from_keyname(val: &str) -> Result<Self, HkError>

Take in a string and try to guess what Virtual Key (VK) it is meant to represent. Returns the VK code as i32 on success (a key representation was recognized).

  • For single character strings the ASCII code is used as VK, this is used to represent alphanumeric keys
  • Many of the most common VKs are represented by their constant name. For example VK_SPACE => spacebar key
  • Any other key can be represented by directly specifying the VK keycode value in 2 digit hex representation. For example 0x08 == VK_TAB (Tab key)

See https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

Examples found in repository?
examples/vkeys-and-modkeys.rs (line 22)
7fn main() {
8    // Create a HotkeyManager
9    let mut hkm = HotkeyManager::<()>::new();
10
11    // Create VKey from the enum variant (recommended if possible)
12    let vk_b1 = VKey::B;
13    let vk_up1 = VKey::Up;
14
15    // Create VKey from the case insensitive key char (only for 'a'-'z' and '0'-'9')
16    let vk_b2 = VKey::from_char('b').unwrap();
17    let vk_b3 = VKey::from_char('B').unwrap();
18
19    // Create VKey from key name string. This works with "A"-"Z", "0"-"9", the constant names
20    // "VK_*" and hex codes prefixed with "0x*"
21    // Useful when reading hotkeys in text form through config files or user input
22    let vk_b4 = VKey::from_keyname("b").unwrap();
23    let vk_b5 = VKey::from_keyname("B").unwrap();
24    let vk_b6 = VKey::from_keyname("0x42").unwrap();
25    let vk_up2 = VKey::from_keyname("VK_UP").unwrap();
26
27    // Create VKey directly from the virtual keycode
28    // See: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
29    let vk_b7 = VKey::CustomKeyCode(0x42);
30
31    // All of the methods are comparable with equal and usable for hashing
32    assert_eq!(vk_b1, vk_b2);
33    assert_eq!(vk_b1, vk_b3);
34    assert_eq!(vk_b1, vk_b4);
35    assert_eq!(vk_b1, vk_b5);
36    assert_eq!(vk_b1, vk_b6);
37    assert_eq!(vk_b1, vk_b7);
38
39    assert_eq!(vk_up1, vk_up2);
40
41    // **Alert**
42    // Matching the variants does not always work, since a key can be represented through the enum
43    // variant, or with a `CustomKeyCode`
44    match vk_b7 {
45        VKey::B => println!(
46            "CustomKeyCode(0x42) matches against B (this will not show up, because it is not true)"
47        ),
48        VKey::A => println!("Just checking if the key matches A"),
49        _ => println!("CustomKeyCode(0x42) does not match against B"),
50    }
51
52    // Instead use if chains if this is really needed for now, at least until a better solution is
53    // implemented
54    if vk_b7 == VKey::B {
55        println!("CustomKeyCode(0x42) is equal to B");
56    } else if vk_b7 == VKey::A {
57        println!("Just checking if the key is equal to A");
58    } else {
59        println!(
60            "CustomKeyCode(0x42) is not equal to B \
61            (this will not show up, because it works fine when using if)"
62        );
63    }
64
65    // Create ModKey from the enum variant
66    let mod_alt1 = ModKey::Alt;
67    // Create ModKey from key name string
68    let mod_alt2 = ModKey::from_keyname("ALT").unwrap();
69
70    // With modkeys, there is no `CustomKeyCode`, so they can be safely matched and compared
71    assert_eq!(mod_alt1, mod_alt2);
72
73    hkm.register_extrakeys(vk_b1, &[mod_alt1], &[vk_up1], || {
74        println!("Hotkey ALT + B + UP pressed");
75    })
76    .unwrap();
77
78    hkm.event_loop();
79}

Trait Implementations§

Source§

impl Clone for VKey

Source§

fn clone(&self) -> VKey

Returns a duplicate 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 From<ModKey> for VKey

Source§

fn from(mk: ModKey) -> VKey

Converts to this type from the input type.
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 TryInto<ModKey> for VKey

Source§

type Error = ()

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

fn try_into(self) -> Result<ModKey, Self::Error>

Performs the conversion.
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, 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.