-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix unhandled edge cases for keyboard shortcuts (fixes #927, fixes #932, fixes #1063) #1211
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| var registerComponent = require('../core/component').registerComponent; | ||
| var shouldCaptureKeyEvent = require('../utils/').shouldCaptureKeyEvent; | ||
| var THREE = require('../lib/three'); | ||
|
|
||
| var MAX_DELTA = 0.2; | ||
|
|
@@ -24,8 +25,12 @@ module.exports.Component = registerComponent('wasd-controls', { | |
| this.velocity = new THREE.Vector3(); | ||
| // To keep track of the pressed keys | ||
| this.keys = {}; | ||
| this.onBlur = this.onBlur.bind(this); | ||
| this.onFocus = this.onFocus.bind(this); | ||
| this.onVisibilityChange = this.onVisibilityChange.bind(this); | ||
| this.onKeyDown = this.onKeyDown.bind(this); | ||
| this.onKeyUp = this.onKeyUp.bind(this); | ||
| this.attachVisibilityEventListeners(); | ||
| }, | ||
|
|
||
| update: function (previousData) { | ||
|
|
@@ -81,11 +86,12 @@ module.exports.Component = registerComponent('wasd-controls', { | |
| }, | ||
|
|
||
| play: function () { | ||
| this.attachEventListeners(); | ||
| this.attachKeyEventListeners(); | ||
| }, | ||
|
|
||
| pause: function () { | ||
| this.removeEventListeners(); | ||
| this.keys = {}; | ||
| this.removeKeyEventListeners(); | ||
| }, | ||
|
|
||
| tick: function (t) { | ||
|
|
@@ -94,25 +100,54 @@ module.exports.Component = registerComponent('wasd-controls', { | |
|
|
||
| remove: function () { | ||
| this.pause(); | ||
| this.removeVisibilityEventListeners(); | ||
| }, | ||
|
|
||
| attachVisibilityEventListeners: function () { | ||
| window.addEventListener('blur', this.onBlur); | ||
| window.addEventListener('focus', this.onFocus); | ||
| document.addEventListener('visibilitychange', this.onVisibilityChange); | ||
| }, | ||
|
|
||
| attachEventListeners: function () { | ||
| // Keyboard events | ||
| window.addEventListener('keydown', this.onKeyDown, false); | ||
| window.addEventListener('keyup', this.onKeyUp, false); | ||
| removeVisibilityEventListeners: function () { | ||
| window.removeEventListener('blur', this.onBlur); | ||
| window.removeEventListener('focus', this.onFocus); | ||
| document.removeEventListener('visibilitychange', this.onVisibilityChange); | ||
| }, | ||
|
|
||
| removeEventListeners: function () { | ||
| // Keyboard events | ||
| attachKeyEventListeners: function () { | ||
| window.addEventListener('keydown', this.onKeyDown); | ||
| window.addEventListener('keyup', this.onKeyUp); | ||
| }, | ||
|
|
||
| removeKeyEventListeners: function () { | ||
| window.removeEventListener('keydown', this.onKeyDown); | ||
| window.removeEventListener('keyup', this.onKeyUp); | ||
| }, | ||
|
|
||
| onBlur: function () { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we move this to the component module and always call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting idea; probably actually, yeah. you think that's good?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When is the blur event exactly triggered?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when the current tab is no longer the focused tab - or if the window loses focus
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard to say whether we want to force pause on blur. Seems too prescriptive at the moment. |
||
| this.pause(); | ||
| }, | ||
|
|
||
| onFocus: function () { | ||
| this.play(); | ||
| }, | ||
|
|
||
| onVisibilityChange: function () { | ||
| if (document.hidden) { | ||
| this.onBlur(); | ||
| } else { | ||
| this.onFocus(); | ||
| } | ||
| }, | ||
|
|
||
| onKeyDown: function (event) { | ||
| if (!shouldCaptureKeyEvent(event)) { return; } | ||
| this.keys[event.keyCode] = true; | ||
| }, | ||
|
|
||
| onKeyUp: function (event) { | ||
| if (!shouldCaptureKeyEvent(event)) { return; } | ||
| this.keys[event.keyCode] = false; | ||
| }, | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -131,13 +131,25 @@ var isIOS = module.exports.isIOS = function () { | |
| }; | ||
|
|
||
| /** | ||
| * Checks mobile device orientation | ||
| * @return {Boolean} True if landscape orientation | ||
| * Checks mobile device orientation. | ||
| * @return {Boolean} True if landscape orientation. | ||
| */ | ||
| module.exports.isLandscape = function () { | ||
| return window.orientation === 90 || window.orientation === -90; | ||
| }; | ||
|
|
||
| /** | ||
| * Returns whether we should capture this keyboard event for keyboard shortcuts. | ||
| * @param {Event} event Event object. | ||
| * @returns {Boolean} Whether the key event should be captured. | ||
| */ | ||
| module.exports.shouldCaptureKeyEvent = function (event) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if (event.shiftKey || event.metaKey || event.altKey || event.ctrlKey) { | ||
| return false; | ||
| } | ||
| return document.activeElement === document.body; | ||
| }; | ||
|
|
||
| /** | ||
| * Splits a string into an array based on a delimiter. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also have a listener for the symmetric event.
window.addEventListener("focus", this.onFocus);?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, very good call.
hmm, the more I think about it we probably shouldn't call
pauseonblurbecause that removes the event listeners. and then we'd have to reattach them whenplaygets called onfocus. seems like maybe a waste. thoughts?