Skip to content

Commit aa73a24

Browse files
author
lauren mccarthy
committed
adding prevent default functionality for key
1 parent f8b192c commit aa73a24

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

lib/p5.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.js v0.3.10 November 01, 2014 */
1+
/*! p5.js v0.3.10 November 03, 2014 */
22
var shim = function (require) {
33
window.requestDraw = function () {
44
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback, element) {
@@ -3001,7 +3001,10 @@ var inputkeyboard = function (require, core) {
30013001
this._setProperty('key', key);
30023002
var keyPressed = this.keyPressed || window.keyPressed;
30033003
if (typeof keyPressed === 'function' && !e.charCode) {
3004-
keyPressed(e);
3004+
var executeDefault = keyPressed(e);
3005+
if (executeDefault === false) {
3006+
e.preventDefault();
3007+
}
30053008
}
30063009
};
30073010
p5.prototype.onkeyup = function (e) {
@@ -3015,15 +3018,21 @@ var inputkeyboard = function (require, core) {
30153018
this._setProperty('key', key);
30163019
this._setProperty('keyCode', e.which);
30173020
if (typeof keyReleased === 'function') {
3018-
keyReleased(e);
3021+
var executeDefault = keyReleased(e);
3022+
if (executeDefault === false) {
3023+
e.preventDefault();
3024+
}
30193025
}
30203026
};
30213027
p5.prototype.onkeypress = function (e) {
30223028
this._setProperty('keyCode', e.which);
30233029
this._setProperty('key', String.fromCharCode(e.which));
30243030
var keyTyped = this.keyTyped || window.keyTyped;
30253031
if (typeof keyTyped === 'function') {
3026-
keyTyped(e);
3032+
var executeDefault = keyTyped(e);
3033+
if (executeDefault === false) {
3034+
e.preventDefault();
3035+
}
30273036
}
30283037
};
30293038
return p5;

lib/p5.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/input/keyboard.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ define(function (require) {
6868
* Because of how operating systems handle key repeats, holding down a key
6969
* may cause multiple calls to keyTyped() (and keyReleased() as well). The
7070
* rate of repeat is set by the operating system and how each computer is
71-
* configured.
71+
* configured.<br><br>
72+
* Browsers may have different default
73+
* behaviors attached to various key events. To prevent any default
74+
* behavior for this event, add "return false" to the end of the method.
7275
*
7376
* @method keyPressed
7477
* @example
@@ -101,6 +104,7 @@ define(function (require) {
101104
* } else if (keyCode === RIGHT_ARROW) {
102105
* value = 0;
103106
* }
107+
* return false; // prevent any default behavior
104108
* }
105109
* </code>
106110
* </div>
@@ -116,12 +120,18 @@ define(function (require) {
116120
this._setProperty('key', key);
117121
var keyPressed = this.keyPressed || window.keyPressed;
118122
if (typeof keyPressed === 'function' && !e.charCode) {
119-
keyPressed(e);
123+
var executeDefault = keyPressed(e);
124+
if(executeDefault === false) {
125+
e.preventDefault();
126+
}
120127
}
121128
};
122129
/**
123130
* The keyReleased() function is called once every time a key is released.
124-
* See key and keyCode for more information.
131+
* See key and keyCode for more information.<br><br>
132+
* Browsers may have different default
133+
* behaviors attached to various key events. To prevent any default
134+
* behavior for this event, add "return false" to the end of the method.
125135
*
126136
* @method keyReleased
127137
* @example
@@ -138,6 +148,7 @@ define(function (require) {
138148
* } else {
139149
* value = 0;
140150
* }
151+
* return false; // prevent any default behavior
141152
* }
142153
* </code>
143154
* </div>
@@ -153,7 +164,10 @@ define(function (require) {
153164
this._setProperty('key', key);
154165
this._setProperty('keyCode', e.which);
155166
if (typeof keyReleased === 'function') {
156-
keyReleased(e);
167+
var executeDefault = keyReleased(e);
168+
if(executeDefault === false) {
169+
e.preventDefault();
170+
}
157171
}
158172
};
159173

@@ -164,7 +178,10 @@ define(function (require) {
164178
* <br><br>
165179
* Because of how operating systems handle key repeats, holding down a key
166180
* will cause multiple calls to keyTyped(), the rate is set by the operating
167-
* system and how each computer is configured.
181+
* system and how each computer is configured.<br><br>
182+
* Browsers may have different default
183+
* behaviors attached to various key events. To prevent any default
184+
* behavior for this event, add "return false" to the end of the method.
168185
*
169186
* @method keyTyped
170187
* @example
@@ -181,6 +198,7 @@ define(function (require) {
181198
* } else if (key === 'b') {
182199
* value = 0;
183200
* }
201+
* return false; // prevent any default behavior
184202
* }
185203
* </code>
186204
* </div>
@@ -190,7 +208,10 @@ define(function (require) {
190208
this._setProperty('key', String.fromCharCode(e.which));
191209
var keyTyped = this.keyTyped || window.keyTyped;
192210
if (typeof keyTyped === 'function') {
193-
keyTyped(e);
211+
var executeDefault = keyTyped(e);
212+
if(executeDefault === false) {
213+
e.preventDefault();
214+
}
194215
}
195216
};
196217

src/input/mouse.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ define(function (require) {
144144

145145
/**
146146
* The mouseMoved() function is called every time the mouse moves and a mouse
147-
* button is not pressed.
147+
* button is not pressed.<br><br>
148148
* Browsers may have different default
149149
* behaviors attached to various mouse events. To prevent any default
150150
* behavior for this event, add `return false` to the end of the method.
@@ -184,7 +184,7 @@ define(function (require) {
184184
/**
185185
* The mouseDragged() function is called once every time the mouse moves and
186186
* a mouse button is pressed. If no mouseDragged() function is defined, the
187-
* touchMoved() function will be called instead if it is defined.
187+
* touchMoved() function will be called instead if it is defined.<br><br>
188188
* Browsers may have different default
189189
* behaviors attached to various mouse events. To prevent any default
190190
* behavior for this event, add `return false` to the end of the method.
@@ -253,7 +253,7 @@ define(function (require) {
253253
* is pressed. The mouseButton variable (see the related reference entry)
254254
* can be used to determine which button has been pressed. If no
255255
* mousePressed() function is defined, the touchStarted() function will be
256-
* called instead if it is defined.
256+
* called instead if it is defined.<br><br>
257257
* Browsers may have different default
258258
* behaviors attached to various mouse events. To prevent any default
259259
* behavior for this event, add `return false` to the end of the method.
@@ -313,7 +313,7 @@ define(function (require) {
313313
/**
314314
* The mouseReleased() function is called every time a mouse button is
315315
* released. If no mouseReleased() function is defined, the touchEnded()
316-
* function will be called instead if it is defined.
316+
* function will be called instead if it is defined.<br><br>
317317
* Browsers may have different default
318318
* behaviors attached to various mouse events. To prevent any default
319319
* behavior for this event, add `return false` to the end of the method.
@@ -373,7 +373,7 @@ define(function (require) {
373373

374374
/**
375375
* The mouseClicked() function is called once after a mouse button has been
376-
* pressed and then released.
376+
* pressed and then released.<br><br>
377377
* Browsers may have different default
378378
* behaviors attached to various mouse events. To prevent any default
379379
* behavior for this event, add `return false` to the end of the method.
@@ -425,7 +425,7 @@ define(function (require) {
425425
* The event.wheelDelta or event.detail property returns negative values if
426426
* the mouse wheel if rotated up or away from the user and positive in the
427427
* other direction. On OS X with "natural" scrolling enabled, the values are
428-
* opposite.
428+
* opposite.<br><br>
429429
* Browsers may have different default
430430
* behaviors attached to various mouse events. To prevent any default
431431
* behavior for this event, add `return false` to the end of the method.

0 commit comments

Comments
 (0)