rust_minifb/examples/char_callback.rs
john01dav 2b2540067c
Support typed keys on x11
* Added not-yet-working draft implementation of proper X11 support for reading correct typed characters

* Made shift work for capital letters with X11, but compose key is still broken

* Compose key and numpad now work correctly in X11

* XIM and XIC are now freed when a window destructor runs

* Ran cargo fmt on x11.rs

* Removed commented-out empty-string

Closes #200
2021-11-15 08:45:44 +01:00

64 lines
1.5 KiB
Rust

use minifb::{Key, Window, WindowOptions};
use std::cell::RefCell;
use std::rc::Rc;
const WIDTH: usize = 640;
const HEIGHT: usize = 360;
type KeyVec = Rc<RefCell<Vec<u32>>>;
struct Input {
keys: KeyVec,
}
impl Input {
fn new(data: &KeyVec) -> Input {
Input { keys: data.clone() }
}
}
impl minifb::InputCallback for Input {
fn add_char(&mut self, uni_char: u32) {
self.keys.borrow_mut().push(uni_char);
}
}
fn main() {
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
let mut window = Window::new(
"Test - ESC to exit",
WIDTH,
HEIGHT,
WindowOptions::default(),
)
.unwrap_or_else(|e| {
panic!("{:?}", e);
});
let keys_data = KeyVec::new(RefCell::new(Vec::new()));
let input = Box::new(Input::new(&keys_data));
// Limit to max ~60 fps update rate
window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));
window.set_input_callback(input);
while window.is_open() && !window.is_key_down(Key::Escape) {
for i in buffer.iter_mut() {
*i = 0; // write something more funny here!
}
// We unwrap here as we want this code to exit if it fails. Real applications may want to handle this in a different way
window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
let mut keys = keys_data.borrow_mut();
for t in keys.iter() {
println!("Code point: {}, Character: {:?}", *t, char::from_u32(*t));
}
keys.clear();
}
}