Skip to content

Commit 0033e7a

Browse files
authored
Merge pull request #5 from moppoi5168/master
Add JIS(109-key) layout support and fix usage code in README.md
2 parents 34595c1 + 56bbc1d commit 0033e7a

File tree

5 files changed

+235
-68
lines changed

5 files changed

+235
-68
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ output directly).
99
* Scancode Set 1 and 2
1010
* US 104-key layout
1111
* UK 105-key layout
12+
* JIS 109-key layout
1213

1314
## Usage
1415

1516
```rust
1617
extern crate pc_keyboard;
1718

18-
use pc_keyboard::{Keyboard, layouts, ScancodeSet2, HandleControlPlusLetter};
19+
use pc_keyboard::{Keyboard, layouts, ScancodeSet2, HandleControl};
1920

2021
fn main() {
21-
let mut kb = pc_keyboard::Keyboard::new(layouts::Us104Key, ScancodeSet2, HandleControlPlusLetter::MapToUnicode);
22+
let mut kb = pc_keyboard::Keyboard::new(layouts::Us104Key, ScancodeSet2, HandleControl::MapLettersToUnicode);
2223
match kb.add_byte(0x20) {
2324
Ok(Some(event)) => {
2425
println!("Event {:?}", event);

src/layouts/jis109.rs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/// A standard Japan 106-key (or 109-key including Windows keys) keyboard.
2+
/// Has a 2-row high Enter key, with Backslash above.
3+
4+
use crate::{DecodedKey, HandleControl, KeyCode, KeyboardLayout, Modifiers};
5+
6+
pub use super::us104::Us104Key;
7+
8+
pub struct Jis109Key;
9+
10+
impl KeyboardLayout for Jis109Key {
11+
fn map_keycode(
12+
keycode: KeyCode,
13+
modifiers: &Modifiers,
14+
handle_ctrl: HandleControl,
15+
) -> DecodedKey {
16+
match keycode {
17+
KeyCode::BackTick => {
18+
if modifiers.is_shifted() {
19+
DecodedKey::Unicode('`')
20+
} else {
21+
DecodedKey::Unicode('@')
22+
}
23+
}
24+
KeyCode::Escape => DecodedKey::Unicode(0x1B.into()),
25+
KeyCode::Key1 => {
26+
if modifiers.is_shifted() {
27+
DecodedKey::Unicode('!')
28+
} else {
29+
DecodedKey::Unicode('1')
30+
}
31+
}
32+
KeyCode::Key2 => {
33+
if modifiers.is_shifted() {
34+
DecodedKey::Unicode('"')
35+
} else {
36+
DecodedKey::Unicode('2')
37+
}
38+
}
39+
KeyCode::Key3 => {
40+
if modifiers.is_shifted() {
41+
DecodedKey::Unicode('#')
42+
} else {
43+
DecodedKey::Unicode('3')
44+
}
45+
}
46+
KeyCode::Key4 => {
47+
if modifiers.is_shifted() {
48+
DecodedKey::Unicode('$')
49+
} else {
50+
DecodedKey::Unicode('4')
51+
}
52+
}
53+
KeyCode::Key5 => {
54+
if modifiers.is_shifted() {
55+
DecodedKey::Unicode('%')
56+
} else {
57+
DecodedKey::Unicode('5')
58+
}
59+
}
60+
KeyCode::Key6 => {
61+
if modifiers.is_shifted() {
62+
DecodedKey::Unicode('&')
63+
} else {
64+
DecodedKey::Unicode('6')
65+
}
66+
}
67+
KeyCode::Key7 => {
68+
if modifiers.is_shifted() {
69+
DecodedKey::Unicode('\'')
70+
} else {
71+
DecodedKey::Unicode('7')
72+
}
73+
}
74+
KeyCode::Key8 => {
75+
if modifiers.is_shifted() {
76+
DecodedKey::Unicode('(')
77+
} else {
78+
DecodedKey::Unicode('8')
79+
}
80+
}
81+
KeyCode::Key9 => {
82+
if modifiers.is_shifted() {
83+
DecodedKey::Unicode(')')
84+
} else {
85+
DecodedKey::Unicode('9')
86+
}
87+
}
88+
KeyCode::Key0 => {
89+
if modifiers.is_shifted() {
90+
DecodedKey::Unicode(' ')
91+
} else {
92+
DecodedKey::Unicode('0')
93+
}
94+
}
95+
KeyCode::Minus => {
96+
if modifiers.is_shifted() {
97+
DecodedKey::Unicode('=')
98+
} else {
99+
DecodedKey::Unicode('-')
100+
}
101+
}
102+
KeyCode::Equals => {
103+
if modifiers.is_shifted() {
104+
DecodedKey::Unicode('+')
105+
} else {
106+
DecodedKey::Unicode(';')
107+
}
108+
}
109+
KeyCode::BracketSquareLeft => {
110+
if modifiers.is_shifted() {
111+
DecodedKey::Unicode('{')
112+
} else {
113+
DecodedKey::Unicode('[')
114+
}
115+
}
116+
KeyCode::BracketSquareRight => {
117+
if modifiers.is_shifted() {
118+
DecodedKey::Unicode('}')
119+
} else {
120+
DecodedKey::Unicode(']')
121+
}
122+
}
123+
KeyCode::BackSlash => {
124+
if modifiers.is_shifted() {
125+
DecodedKey::Unicode('|')
126+
} else {
127+
DecodedKey::Unicode('\\')
128+
}
129+
}
130+
KeyCode::SemiColon => {
131+
if modifiers.is_shifted() {
132+
DecodedKey::Unicode('*')
133+
} else {
134+
DecodedKey::Unicode(':')
135+
}
136+
}
137+
KeyCode::Quote => {
138+
if modifiers.is_shifted() {
139+
DecodedKey::Unicode('~')
140+
} else {
141+
DecodedKey::Unicode('^')
142+
}
143+
}
144+
e => <Us104Key as KeyboardLayout>::map_keycode(e, modifiers, handle_ctrl),
145+
}
146+
}
147+
}

src/layouts/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Implements the various keyboard layouts.
2+
//!
3+
//! We have one layout per file, but where two layouts are similar, you can
4+
//! handle all the 'different' keys first, and then jump to another handler -
5+
//! see UK105 and US104 as an example of that.
6+
7+
mod us104;
8+
pub use self::us104::Us104Key;
9+
10+
mod uk105;
11+
pub use self::uk105::Uk105Key;
12+
13+
mod jis109;
14+
pub use self::jis109::Jis109Key;

src/layouts/uk105.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! A standard United Kingdom 102-key (or 105-key including Windows keys) keyboard.
2+
//! Has a 2-row high Enter key, with Backslash next to the left shift.
3+
4+
use crate::{DecodedKey, HandleControl, KeyCode, KeyboardLayout, Modifiers};
5+
6+
pub use super::us104::Us104Key;
7+
8+
pub struct Uk105Key;
9+
10+
impl KeyboardLayout for Uk105Key {
11+
fn map_keycode(
12+
keycode: KeyCode,
13+
modifiers: &Modifiers,
14+
handle_ctrl: HandleControl,
15+
) -> DecodedKey {
16+
match keycode {
17+
KeyCode::BackTick => {
18+
if modifiers.alt_gr {
19+
DecodedKey::Unicode('|')
20+
} else if modifiers.is_shifted() {
21+
DecodedKey::Unicode('¬')
22+
} else {
23+
DecodedKey::Unicode('`')
24+
}
25+
}
26+
KeyCode::Key2 => {
27+
if modifiers.is_shifted() {
28+
DecodedKey::Unicode('"')
29+
} else {
30+
DecodedKey::Unicode('2')
31+
}
32+
}
33+
KeyCode::Quote => {
34+
if modifiers.is_shifted() {
35+
DecodedKey::Unicode('@')
36+
} else {
37+
DecodedKey::Unicode('\'')
38+
}
39+
}
40+
KeyCode::Key3 => {
41+
if modifiers.is_shifted() {
42+
DecodedKey::Unicode('£')
43+
} else {
44+
DecodedKey::Unicode('3')
45+
}
46+
}
47+
KeyCode::Key4 => {
48+
if modifiers.alt_gr {
49+
DecodedKey::Unicode('€')
50+
} else if modifiers.is_shifted() {
51+
DecodedKey::Unicode('$')
52+
} else {
53+
DecodedKey::Unicode('4')
54+
}
55+
}
56+
KeyCode::HashTilde => {
57+
if modifiers.is_shifted() {
58+
DecodedKey::Unicode('~')
59+
} else {
60+
DecodedKey::Unicode('#')
61+
}
62+
}
63+
e => <super::Us104Key as KeyboardLayout>::map_keycode(e, modifiers, handle_ctrl),
64+
}
65+
}
66+
}

src/layouts.rs renamed to src/layouts/us104.rs

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use super::{DecodedKey, HandleControl, KeyCode, KeyboardLayout, Modifiers};
1+
//! A standard United States 101-key (or 104-key including Windows keys) keyboard.
2+
//! Has a 1-row high Enter key, with Backslash above.
23
3-
/// A standard United States 101-key (or 104-key including Windows keys) keyboard.
4-
/// Has a 1-row high Enter key, with Backslash above.
5-
pub struct Us104Key;
4+
use crate::{DecodedKey, HandleControl, KeyCode, KeyboardLayout, Modifiers};
65

7-
/// A standard United Kingdom 102-key (or 105-key including Windows keys) keyboard.
8-
/// Has a 2-row high Enter key, with Backslash next to the left shift.
9-
pub struct Uk105Key;
6+
pub struct Us104Key;
107

118
impl KeyboardLayout for Us104Key {
129
fn map_keycode(
@@ -483,62 +480,4 @@ impl KeyboardLayout for Us104Key {
483480
k => DecodedKey::RawKey(k),
484481
}
485482
}
486-
}
487-
488-
impl KeyboardLayout for Uk105Key {
489-
fn map_keycode(
490-
keycode: KeyCode,
491-
modifiers: &Modifiers,
492-
handle_ctrl: HandleControl,
493-
) -> DecodedKey {
494-
match keycode {
495-
KeyCode::BackTick => {
496-
if modifiers.alt_gr {
497-
DecodedKey::Unicode('|')
498-
} else if modifiers.is_shifted() {
499-
DecodedKey::Unicode('¬')
500-
} else {
501-
DecodedKey::Unicode('`')
502-
}
503-
}
504-
KeyCode::Key2 => {
505-
if modifiers.is_shifted() {
506-
DecodedKey::Unicode('"')
507-
} else {
508-
DecodedKey::Unicode('2')
509-
}
510-
}
511-
KeyCode::Quote => {
512-
if modifiers.is_shifted() {
513-
DecodedKey::Unicode('@')
514-
} else {
515-
DecodedKey::Unicode('\'')
516-
}
517-
}
518-
KeyCode::Key3 => {
519-
if modifiers.is_shifted() {
520-
DecodedKey::Unicode('£')
521-
} else {
522-
DecodedKey::Unicode('3')
523-
}
524-
}
525-
KeyCode::Key4 => {
526-
if modifiers.alt_gr {
527-
DecodedKey::Unicode('€')
528-
} else if modifiers.is_shifted() {
529-
DecodedKey::Unicode('$')
530-
} else {
531-
DecodedKey::Unicode('4')
532-
}
533-
}
534-
KeyCode::HashTilde => {
535-
if modifiers.is_shifted() {
536-
DecodedKey::Unicode('~')
537-
} else {
538-
DecodedKey::Unicode('#')
539-
}
540-
}
541-
e => <Us104Key as KeyboardLayout>::map_keycode(e, modifiers, handle_ctrl),
542-
}
543-
}
544-
}
483+
}

0 commit comments

Comments
 (0)