[web/glium] better text input filtering

CapsLock and function keys (F1, F2, ...) are now ignored on web.

All special keys are ignored on glium.
This commit is contained in:
Emil Ernerfeldt
2020-07-30 11:58:24 +02:00
parent 554e6e7120
commit f693a558c5
4 changed files with 60 additions and 39 deletions
+4 -2
View File
@@ -155,7 +155,8 @@ impl Default for MouseInput {
pub enum Event {
Copy,
Cut,
/// Text input, e.g. via keyboard or paste action
/// Text input, e.g. via keyboard or paste action.
/// Do not pass '\n', '\r' here, but send `Key::Enter` instead.
Text(String),
Key {
key: Key,
@@ -179,7 +180,8 @@ pub enum Key {
Logo,
PageDown,
PageUp,
Return,
/// Enter/Return key
Enter,
Right,
Shift,
// Space,
+13 -1
View File
@@ -97,7 +97,18 @@ impl<'t> Widget for TextEdit<'t> {
ui.ctx().output().copied_text = text.clone();
}
Event::Text(text_to_insert) => {
insert_text(&mut cursor, text, text_to_insert);
// newlines are handled by `Key::Enter`.
if text_to_insert != "\n" && text_to_insert != "\r" {
insert_text(&mut cursor, text, text_to_insert);
}
}
Event::Key {
key: Key::Enter,
pressed: true,
} => {
if multiline {
insert_text(&mut cursor, text, "\n");
}
}
Event::Key { key, pressed: true } => {
on_key_press(&mut cursor, text, *key);
@@ -190,6 +201,7 @@ fn on_key_press(cursor: &mut usize, text: &mut String, key: Key) {
new_text.extend(char_it.skip(1));
*text = new_text;
}
Key::Enter => {} // handled earlier
Key::Home => {
// To start of paragraph:
let pos = line_col_from_char_idx(text, *cursor);