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

focus and text input event.

parent d06563ce
Pipeline #9064 passed with stage
in 3 minutes and 1 second
......@@ -47,6 +47,7 @@ features = [
"MouseEvent",
"KeyboardEvent",
"WheelEvent",
"FocusEvent",
"DomRect"
]
......
......@@ -227,11 +227,20 @@ impl Window {
closure.forget();
}
// key down
// key down and text input
{
let events = events.clone();
let closure = Closure::wrap(Box::new(move |e: web_sys::KeyboardEvent| {
// need to check len of key to prevent keys like `Shift` or `Backspace` are handled as text input
if !e.repeat() && e.key().len() <= 2 {
events.borrow_mut().push(
TextInputEvent {
character: e.key().chars().nth(0).unwrap(),
}
.to_event(),
);
}
if let Some(code) = convert_code(e.code(), e.shift_key()) {
events.borrow_mut().push(
KeyEvent {
......@@ -293,6 +302,38 @@ impl Window {
closure.forget();
}
// focus
{
let events = events.clone();
let closure = Closure::wrap(Box::new(move |_: web_sys::FocusEvent| {
events
.borrow_mut()
.push(FocusEvent { focused: true }.to_event())
}) as Box<dyn FnMut(_)>);
document()
.unwrap()
.add_event_listener_with_callback("focus", closure.as_ref().unchecked_ref())
.unwrap();
closure.forget();
}
// blur
{
let events = events.clone();
let closure = Closure::wrap(Box::new(move |_: web_sys::FocusEvent| {
events
.borrow_mut()
.push(FocusEvent { focused: false }.to_event())
}) as Box<dyn FnMut(_)>);
document()
.unwrap()
.add_event_listener_with_callback("blur", closure.as_ref().unchecked_ref())
.unwrap();
closure.forget();
}
if let Ok(context) = context_2d(&canvas) {
return Some(Window {
x,
......
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