Skip to main content

pc_keyboard/layouts/
jis109.rs

1//! JIS keyboard support
2
3use crate::{
4    DecodedKey, HandleControl, KeyCode, KeyboardLayout, Modifiers, PhysicalKeyboard, QUO, SLS,
5};
6
7/// A standard Japan 106-key (or 109-key including Windows keys) keyboard.
8///
9/// Has a small space bar, to fit in extra keys.
10///
11/// We used <https://www.win.tue.nl/~aeb/linux/kbd/scancodes-8.html> as a
12/// reference.
13///
14/// These diagrams illustrate the conversion from [`KeyCode`] to Unicode. We
15/// show either a Unicode glyph, or a hex number if the glyph isn't a
16/// printable character. Blank spaces are passed through as
17/// [`DecodedKey::RawKey`].
18///
19/// Run the `print_keyboard` example to re-generate these images.
20///
21/// ## Unmodified
22///
23/// ```text
24/// ┌────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐   ┌────┬────┬────┐
25/// │001b│  │    │    │    │    │  │    │    │    │    │  │    │    │    │    │   │    │    │    │
26/// └────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘   └────┴────┴────┘
27///
28/// ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐  ┌────┬────┬────┐  ┌────┬────┬────┬────┐
29/// │    │ 1  │ 2  │ 3  │ 4  │ 5  │ 6  │ 7  │ 8  │ 9  │ 0  │ -  │ ^  │ ¥  │0008│  │    │    │    │  │    │ /  │ *  │ -  │
30/// ├────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬────────┤  ├────┼────┼────┤  ├────┼────┼────┼────┤
31/// │0009 │ q  │ w  │ e  │ r  │ t  │ y  │ u  │ i  │ o  │ p  │ @  │ [  │  000a  │  │007f│    │    │  │ 7  │ 8  │ 9  │    │
32/// ├─────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┐       │  └────┴────┴────┘  ├────┼────┼────┤ +  │
33/// │      │ a  │ s  │ d  │ f  │ g  │ h  │ j  │ k  │ l  │ ;  │ :  │ ]  │       │                    │ 4  │ 5  │ 6  │    │
34/// ├──────┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴────┼───────┤       ┌────┐       ├────┼────┼────┼────┤
35/// │         │ z  │ x  │ c  │ v  │ b  │ n  │ m  │ ,  │ .  │ /  │ \    │       │       │    │       │ 1  │ 2  │ 3  │    │
36/// ├─────┬───┴─┬──┴──┬─┴───┬┴────┴────┴────┴────┴┬───┴─┬──┴──┬─┴──┬───┴┬──────┤  ┌────┼────┼────┐  ├────┴────┼────┤000a│
37/// │     │     │     │     │       0020          │     │     │    │    │      │  │    │    │    │  │ 0       │ .  │    │
38/// └─────┴─────┴─────┴─────┴─────────────────────┴─────┴─────┴────┴────┴──────┘  └────┴────┴────┘  └─────────┴────┴────┘
39/// ```
40///
41/// ## Caps Lock
42///
43/// ```text
44/// ┌────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐   ┌────┬────┬────┐
45/// │001b│  │    │    │    │    │  │    │    │    │    │  │    │    │    │    │   │    │    │    │
46/// └────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘   └────┴────┴────┘
47///
48/// ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐  ┌────┬────┬────┐  ┌────┬────┬────┬────┐
49/// │    │ 1  │ 2  │ 3  │ 4  │ 5  │ 6  │ 7  │ 8  │ 9  │ 0  │ -  │ ^  │ ¥  │0008│  │    │    │    │  │    │ /  │ *  │ -  │
50/// ├────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬────────┤  ├────┼────┼────┤  ├────┼────┼────┼────┤
51/// │0009 │ Q  │ W  │ E  │ R  │ T  │ Y  │ U  │ I  │ O  │ P  │ @  │ [  │  000a  │  │007f│    │    │  │ 7  │ 8  │ 9  │    │
52/// ├─────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┐       │  └────┴────┴────┘  ├────┼────┼────┤ +  │
53/// │      │ A  │ S  │ D  │ F  │ G  │ H  │ J  │ K  │ L  │ ;  │ :  │ ]  │       │                    │ 4  │ 5  │ 6  │    │
54/// ├──────┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴────┼───────┤       ┌────┐       ├────┼────┼────┼────┤
55/// │         │ Z  │ X  │ C  │ V  │ B  │ N  │ M  │ ,  │ .  │ /  │ \    │       │       │    │       │ 1  │ 2  │ 3  │    │
56/// ├─────┬───┴─┬──┴──┬─┴───┬┴────┴────┴────┴────┴┬───┴─┬──┴──┬─┴──┬───┴┬──────┤  ┌────┼────┼────┐  ├────┴────┼────┤000a│
57/// │     │     │     │     │       0020          │     │     │    │    │      │  │    │    │    │  │ 0       │ .  │    │
58/// └─────┴─────┴─────┴─────┴─────────────────────┴─────┴─────┴────┴────┴──────┘  └────┴────┴────┘  └─────────┴────┴────┘
59/// ```
60///
61/// ## Shifted
62///
63/// ```text
64/// ┌────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐   ┌────┬────┬────┐
65/// │001b│  │    │    │    │    │  │    │    │    │    │  │    │    │    │    │   │    │    │    │
66/// └────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘   └────┴────┴────┘
67///
68/// ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐  ┌────┬────┬────┐  ┌────┬────┬────┬────┐
69/// │    │ !  │ "  │ #  │ $  │ %  │ &  │ '  │ (  │ )  │ ~  │ =  │ ¯  │ |  │0008│  │    │    │    │  │    │ /  │ *  │ -  │
70/// ├────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬────────┤  ├────┼────┼────┤  ├────┼────┼────┼────┤
71/// │0009 │ Q  │ W  │ E  │ R  │ T  │ Y  │ U  │ I  │ O  │ P  │ `  │ {  │  000a  │  │007f│    │    │  │ 7  │ 8  │ 9  │    │
72/// ├─────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┐       │  └────┴────┴────┘  ├────┼────┼────┤ +  │
73/// │      │ A  │ S  │ D  │ F  │ G  │ H  │ J  │ K  │ L  │ +  │ *  │ }  │       │                    │ 4  │ 5  │ 6  │    │
74/// ├──────┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴────┼───────┤       ┌────┐       ├────┼────┼────┼────┤
75/// │         │ Z  │ X  │ C  │ V  │ B  │ N  │ M  │ <  │ >  │ ?  │ _    │       │       │    │       │ 1  │ 2  │ 3  │    │
76/// ├─────┬───┴─┬──┴──┬─┴───┬┴────┴────┴────┴────┴┬───┴─┬──┴──┬─┴──┬───┴┬──────┤  ┌────┼────┼────┐  ├────┴────┼────┤000a│
77/// │     │     │     │     │       0020          │     │     │    │    │      │  │    │    │    │  │ 0       │ .  │    │
78/// └─────┴─────┴─────┴─────┴─────────────────────┴─────┴─────┴────┴────┴──────┘  └────┴────┴────┘  └─────────┴────┴────┘
79/// ```
80///
81/// ## Control
82///
83/// ```text
84/// ┌────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐   ┌────┬────┬────┐
85/// │001b│  │    │    │    │    │  │    │    │    │    │  │    │    │    │    │   │    │    │    │
86/// └────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘   └────┴────┴────┘
87///
88/// ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐  ┌────┬────┬────┐  ┌────┬────┬────┬────┐
89/// │    │ 1  │ 2  │ 3  │ 4  │ 5  │ 6  │ 7  │ 8  │ 9  │ 0  │ -  │ ^  │ ¥  │0008│  │    │    │    │  │    │ /  │ *  │ -  │
90/// ├────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬────────┤  ├────┼────┼────┤  ├────┼────┼────┼────┤
91/// │0009 │0011│0017│0005│0012│0014│0019│0015│0009│000f│0010│ @  │ [  │  000a  │  │007f│    │    │  │ 7  │ 8  │ 9  │    │
92/// ├─────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┐       │  └────┴────┴────┘  ├────┼────┼────┤ +  │
93/// │      │0001│0013│0004│0006│0007│0008│000a│000b│000c│ ;  │ :  │ ]  │       │                    │ 4  │ 5  │ 6  │    │
94/// ├──────┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴────┼───────┤       ┌────┐       ├────┼────┼────┼────┤
95/// │         │001a│0018│0003│0016│0002│000e│000d│ ,  │ .  │ /  │ \    │       │       │    │       │ 1  │ 2  │ 3  │    │
96/// ├─────┬───┴─┬──┴──┬─┴───┬┴────┴────┴────┴────┴┬───┴─┬──┴──┬─┴──┬───┴┬──────┤  ┌────┼────┼────┐  ├────┴────┼────┤000a│
97/// │     │     │     │     │       0020          │     │     │    │    │      │  │    │    │    │  │ 0       │ .  │    │
98/// └─────┴─────┴─────┴─────┴─────────────────────┴─────┴─────┴────┴────┴──────┘  └────┴────┴────┘  └─────────┴────┴────┘
99/// ```
100///
101/// ## AltGr
102///
103/// ```text
104/// ┌────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐   ┌────┬────┬────┐
105/// │001b│  │    │    │    │    │  │    │    │    │    │  │    │    │    │    │   │    │    │    │
106/// └────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘   └────┴────┴────┘
107///
108/// ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐  ┌────┬────┬────┐  ┌────┬────┬────┬────┐
109/// │    │ 1  │ 2  │ 3  │ 4  │ 5  │ 6  │ 7  │ 8  │ 9  │ 0  │ -  │ ^  │ ¥  │0008│  │    │    │    │  │    │ /  │ *  │ -  │
110/// ├────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬────────┤  ├────┼────┼────┤  ├────┼────┼────┼────┤
111/// │0009 │ q  │ w  │ e  │ r  │ t  │ y  │ u  │ i  │ o  │ p  │ @  │ [  │  000a  │  │007f│    │    │  │ 7  │ 8  │ 9  │    │
112/// ├─────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┐       │  └────┴────┴────┘  ├────┼────┼────┤ +  │
113/// │      │ a  │ s  │ d  │ f  │ g  │ h  │ j  │ k  │ l  │ ;  │ :  │ ]  │       │                    │ 4  │ 5  │ 6  │    │
114/// ├──────┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴────┼───────┤       ┌────┐       ├────┼────┼────┼────┤
115/// │         │ z  │ x  │ c  │ v  │ b  │ n  │ m  │ ,  │ .  │ /  │ \    │       │       │    │       │ 1  │ 2  │ 3  │    │
116/// ├─────┬───┴─┬──┴──┬─┴───┬┴────┴────┴────┴────┴┬───┴─┬──┴──┬─┴──┬───┴┬──────┤  ┌────┼────┼────┐  ├────┴────┼────┤000a│
117/// │     │     │     │     │       0020          │     │     │    │    │      │  │    │    │    │  │ 0       │ .  │    │
118/// └─────┴─────┴─────┴─────┴─────────────────────┴─────┴─────┴────┴────┴──────┘  └────┴────┴────┘  └─────────┴────┴────┘
119/// ```
120///
121/// ## Shift AltGr
122///
123/// ```text
124/// ┌────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐  ┌────┬────┬────┬────┐   ┌────┬────┬────┐
125/// │001b│  │    │    │    │    │  │    │    │    │    │  │    │    │    │    │   │    │    │    │
126/// └────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘  └────┴────┴────┴────┘   └────┴────┴────┘
127///
128/// ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐  ┌────┬────┬────┐  ┌────┬────┬────┬────┐
129/// │    │ !  │ "  │ #  │ $  │ %  │ &  │ '  │ (  │ )  │ ~  │ =  │ ¯  │ |  │0008│  │    │    │    │  │    │ /  │ *  │ -  │
130/// ├────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬────────┤  ├────┼────┼────┤  ├────┼────┼────┼────┤
131/// │0009 │ Q  │ W  │ E  │ R  │ T  │ Y  │ U  │ I  │ O  │ P  │ `  │ {  │  000a  │  │007f│    │    │  │ 7  │ 8  │ 9  │    │
132/// ├─────┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┬───┴┐       │  └────┴────┴────┘  ├────┼────┼────┤ +  │
133/// │      │ A  │ S  │ D  │ F  │ G  │ H  │ J  │ K  │ L  │ +  │ *  │ }  │       │                    │ 4  │ 5  │ 6  │    │
134/// ├──────┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴──┬─┴────┼───────┤       ┌────┐       ├────┼────┼────┼────┤
135/// │         │ Z  │ X  │ C  │ V  │ B  │ N  │ M  │ <  │ >  │ ?  │ _    │       │       │    │       │ 1  │ 2  │ 3  │    │
136/// ├─────┬───┴─┬──┴──┬─┴───┬┴────┴────┴────┴────┴┬───┴─┬──┴──┬─┴──┬───┴┬──────┤  ┌────┼────┼────┐  ├────┴────┼────┤000a│
137/// │     │     │     │     │       0020          │     │     │    │    │      │  │    │    │    │  │ 0       │ .  │    │
138/// └─────┴─────┴─────┴─────┴─────────────────────┴─────┴─────┴────┴────┴──────┘  └────┴────┴────┘  └─────────┴────┴────┘
139/// ```
140pub struct Jis109Key;
141
142impl KeyboardLayout for Jis109Key {
143    #[rustfmt::skip]
144    fn map_keycode(
145        &self,
146        keycode: KeyCode,
147        modifiers: &Modifiers,
148        handle_ctrl: HandleControl,
149    ) -> DecodedKey {
150        match keycode {
151            // ========= Row 2 (the numbers) =========
152            // hankaku/zenkaku/kanji
153            KeyCode::Oem8            => DecodedKey::RawKey(keycode),
154            KeyCode::Key2            => modifiers.handle_symbol2('2', '"'),
155            KeyCode::Key6            => modifiers.handle_symbol2('6', '&'),
156            KeyCode::Key7            => modifiers.handle_symbol2('7', QUO),
157            KeyCode::Key8            => modifiers.handle_symbol2('8', '('),
158            KeyCode::Key9            => modifiers.handle_symbol2('9', ')'),
159            KeyCode::Key0            => modifiers.handle_symbol2('0', '~'),
160            KeyCode::OemMinus        => modifiers.handle_symbol2('-', '='),
161            KeyCode::OemPlus         => modifiers.handle_symbol2('^', '¯'),
162            KeyCode::Oem13           => modifiers.handle_symbol2('¥', '|'),
163            // ========= Row 3 (QWERTY) =========
164            KeyCode::Oem4            => modifiers.handle_symbol2('@', '`'),
165            KeyCode::Oem6            => modifiers.handle_symbol2('[', '{'),
166            // ========= Row 4 (ASDFG) =========
167            KeyCode::Oem1            => modifiers.handle_symbol2(';', '+'),
168            KeyCode::Oem3            => modifiers.handle_symbol2(':', '*'),
169            KeyCode::Oem7            => modifiers.handle_symbol2(']', '}'),
170            // ========= Row 5 (ZXCVB) =========
171            KeyCode::Oem12           => modifiers.handle_symbol2(SLS, '_'),
172            // ========= Modifiers =========
173            // Muhenkan
174            KeyCode::Oem9            => DecodedKey::RawKey(keycode),
175            // Henkan/Zenkouho
176            KeyCode::Oem10           => DecodedKey::RawKey(keycode),
177            // Hiragana/Katakana
178            KeyCode::Oem11           => DecodedKey::RawKey(keycode),
179            // ========= Fallback =========
180            e => super::Us104Key.map_keycode(e, modifiers, handle_ctrl)
181        }
182    }
183
184    fn get_physical(&self) -> PhysicalKeyboard {
185        PhysicalKeyboard::Jis
186    }
187}