Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WindowEvent::ReceivedCharacter on web #1747

Merged
merged 2 commits into from
Oct 29, 2020

Conversation

tronical
Copy link
Contributor

@tronical tronical commented Oct 27, 2020

The event was never sent to the application because of the unconditional
preventDefault()
call on keydown.

Fixes #1741

  • Tested on all platforms changed
  • Compilation warnings were addressed
  • cargo fmt has been run on this branch
  • cargo doc builds successfully
  • Added an entry to CHANGELOG.md if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality
  • Updated feature matrix, if new features were added or implemented

@ryanisaacg
Copy link
Contributor

Looks good! Before we merge, we have to address the compile error on stdweb. Importing stdweb::web::event::IKeyboardEvent should do the trick.

@tronical
Copy link
Contributor Author

Thank you! That did the trick :)

Copy link
Contributor

@alvinhochun alvinhochun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See inline review. Also, I think the code deserves at least a short comment to describe what it does and why it is needed.

Comment on lines 125 to 129
let event_key = &event.key();
let is_key_string = event_key.len() == 1 || !event_key.is_ascii();
if !is_key_string {
event.prevent_default();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be put inside on_keyboard_press instead of on_keyboard_release?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, yes, of course. Thanks for spotting!

@alvinhochun
Copy link
Contributor

Sorry, this doesn't block normal shortcut keys (e.g. Ctrl+R), but it does block function keys (e.g. F5). It does appear that we still need to check for modifiers. This should work, but I'm not entirely sure if it does what people usually expects AltGr to do:

let is_shortcut_modifiers = (event.ctrl_key() || event.alt_key()) && !event.get_modifier_state("AltGr");
if !is_key_string || is_shortcut_modifiers {
    event.prevent_default();
}

Also, I believe you need to add preventDefault to the keypress listener to stop Space or Shift+Space from scrolling the page.

@tronical
Copy link
Contributor Author

Sorry, this doesn't block normal shortcut keys (e.g. Ctrl+R), but it does block function keys (e.g. F5). It does appear that we still need to check for modifiers. This should work, but I'm not entirely sure if it does what people usually expects AltGr to do:

let is_shortcut_modifiers = (event.ctrl_key() || event.alt_key()) && !event.get_modifier_state("AltGr");
if !is_key_string || is_shortcut_modifiers {
event.prevent_default();
}

Done. Also added the missing comment, you're totally right that the handling needs it.

Also, I believe you need to add preventDefault to the keypress listener to stop Space or Shift+Space from scrolling the page.

I'm not sure I understand, can you elaborate?

@tronical
Copy link
Contributor Author

The remaining CI failure looks like an unrelated network problem to me.

@alvinhochun
Copy link
Contributor

Also, I believe you need to add preventDefault to the keypress listener to stop Space or Shift+Space from scrolling the page.

I'm not sure I understand, can you elaborate?

Open https://codepen.io/alvinhochun/pen/yLJpwGX, focus on the canvas and try pressing Space. You will find that the page gets scrolled down. Uncomment e.preventDefault(); in the keypress listener and you will find that this behaviour is blocked.

The event was never sent to the application because of the unconditional
preventDefault() call on keydown.

Fixes rust-windowing#1741
After reaching keypress, we should prevent further propagation.

Relates to rust-windowing#1741
@tronical
Copy link
Contributor Author

Open https://codepen.io/alvinhochun/pen/yLJpwGX, focus on the canvas and try pressing Space. You will find that the page gets scrolled down. Uncomment e.preventDefault(); in the keypress listener and you will find that this behaviour is blocked.

Got it, thanks!

Added this in a follow-up commit on top (and fixed changelog grammar in initial commit).

Copy link
Contributor

@alvinhochun alvinhochun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@ryanisaacg ryanisaacg merged commit 33fb62b into rust-windowing:master Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

wasm: WindowEvent::CharacterReceived is not sent anymore
3 participants