Commit ca3ef993 authored by Florian Blasius's avatar Florian Blasius 🤘

workaround to get right key event characters

parent 4538af1a
Pipeline #8325 passed with stage
in 1 minute and 34 seconds
......@@ -110,7 +110,6 @@ impl DerefMut for Event {
}
}
pub const K_A: u8 = 0x1E;
pub const K_B: u8 = 0x30;
pub const K_C: u8 = 0x2E;
......@@ -250,7 +249,7 @@ pub const K_F12: u8 = 0x58;
/// A key event (such as a pressed key)
#[derive(Copy, Clone, Debug)]
pub struct KeyEvent {
/// The charecter of the key
/// The character of the key
pub character: char,
/// The scancode of the key
pub scancode: u8,
......
......@@ -59,6 +59,10 @@ pub struct Window {
mouse_relative: bool,
/// Content of the last drop (file | text) operation
drop_content: RefCell<Option<String>>,
/// This is used to correct the character of the key down event depending on the correct text input
key_event_correction: Cell<Option<KeyEvent>>,
/// This is used to correct the character of the key up event depending on the correct text input
last_text_input: Cell<Option<char>>,
}
impl Renderer for Window {
......@@ -180,6 +184,8 @@ impl Window {
inner: window.into_canvas().software().build().unwrap(),
mouse_relative: false,
drop_content: RefCell::new(None),
key_event_correction: Cell::new(None),
last_text_input: Cell::new(None),
}),
Err(_) => None,
}
......@@ -432,16 +438,33 @@ impl Window {
sdl2::event::Event::MouseWheel { x, y, .. } => {
events.push(ScrollEvent { x: x, y: y }.to_event())
}
sdl2::event::Event::TextInput { text, .. } => {
// workaround to get right character dependent on keyboard language settings (should be removed after keycode and keymap implementation is finished)
if let Some(mut key_event) = self.key_event_correction.get() {
for c in text.chars() {
key_event.character = c;
break;
}
self.last_text_input.set(Some(key_event.character));
events.push(key_event.to_event());
self.key_event_correction.set(None);
}
}
sdl2::event::Event::KeyDown { scancode, .. } => {
if let Some(code) = self.convert_scancode(scancode, shift) {
events.push(
KeyEvent {
character: code.0,
scancode: code.1,
pressed: true,
}
.to_event(),
);
let key_event = KeyEvent {
character: code.0,
scancode: code.1,
pressed: true,
};
// workaround to get right character dependent on keyboard language settings (should be removed after keycode and keymap implementation is finished)
if key_event.character == '\0' {
events.push(key_event.to_event());
} else {
self.key_event_correction.set(Some(key_event));
}
}
}
sdl2::event::Event::DropFile { filename, .. } => {
......@@ -454,9 +477,19 @@ impl Window {
}
sdl2::event::Event::KeyUp { scancode, .. } => {
if let Some(code) = self.convert_scancode(scancode, shift) {
let mut character = code.0;
// workaround to get right character dependent on keyboard language settings (should be removed after keycode and keymap implementation is finished)
if character != '\0' {
if let Some(last) = self.last_text_input.get() {
character = last;
self.last_text_input.set(None);
}
}
events.push(
KeyEvent {
character: code.0,
character,
scancode: code.1,
pressed: false,
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment