Skip to content

Commit 2f51368

Browse files
committed
Fix AltGr characters in text input on Windows
1 parent e29b901 commit 2f51368

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

src/components/textinput.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,20 @@ impl TextInputComponent {
610610
ta.scroll(Scrolling::PageUp);
611611
true
612612
}
613+
Input {
614+
key: Key::Char(c),
615+
ctrl: true,
616+
alt: true,
617+
..
618+
} => {
619+
// On Windows, AltGr is commonly reported as Ctrl+Alt.
620+
if cfg!(windows) {
621+
ta.insert_char(*c);
622+
true
623+
} else {
624+
false
625+
}
626+
}
613627
_ => false,
614628
}
615629
}
@@ -781,6 +795,49 @@ mod tests {
781795
}
782796
}
783797

798+
#[test]
799+
fn altgr_ctrl_alt_char_is_inserted_on_windows_only() {
800+
let mut ta = TextArea::new(vec![String::new()]);
801+
let input = Input {
802+
key: Key::Char('{'),
803+
ctrl: true,
804+
alt: true,
805+
shift: false,
806+
};
807+
808+
let modified =
809+
TextInputComponent::process_inputs(&mut ta, &input);
810+
811+
if cfg!(windows) {
812+
assert!(modified);
813+
assert_eq!(ta.lines()[0], "{");
814+
} else {
815+
assert!(!modified);
816+
assert_eq!(ta.lines()[0], "");
817+
}
818+
}
819+
820+
#[test]
821+
fn ctrl_alt_b_shortcut_is_preserved() {
822+
let mut ta = TextArea::new(vec![String::from("abc")]);
823+
ta.move_cursor(CursorMove::End);
824+
assert_eq!(ta.cursor(), (0, 3));
825+
826+
let input = Input {
827+
key: Key::Char('b'),
828+
ctrl: true,
829+
alt: true,
830+
shift: false,
831+
};
832+
833+
let modified =
834+
TextInputComponent::process_inputs(&mut ta, &input);
835+
836+
assert!(modified);
837+
assert_eq!(ta.lines()[0], "abc");
838+
assert_eq!(ta.cursor(), (0, 0));
839+
}
840+
784841
#[test]
785842
fn test_next_word_position() {
786843
let env = Environment::test_env();

0 commit comments

Comments
 (0)