Skip to content

Commit 063a862

Browse files
authored
Merge pull request #7472 from Vaivaswat2244/dev-2.0
Replaced deprecated keyCode functionality and docs with KeyboardEvent.code & KeyboardEvent.key also updates the keyIsDown function to accept alphanumerics as parameters
2 parents 47faadf + e3fc37c commit 063a862

File tree

4 files changed

+144
-66
lines changed

4 files changed

+144
-66
lines changed

src/core/constants.js

+42-30
Original file line numberDiff line numberDiff line change
@@ -879,92 +879,104 @@ export const MITER = 'miter';
879879
* @final
880880
*/
881881
export const AUTO = 'auto';
882-
882+
// INPUT
883883
/**
884-
* @typedef {18} ALT
884+
* @typedef {'AltLeft' | 'AltRight'} ALT
885885
* @property {ALT} ALT
886886
* @final
887887
*/
888-
// INPUT
889-
export const ALT = 18;
888+
export const ALT = 'AltLeft';
889+
890890
/**
891-
* @typedef {8} BACKSPACE
891+
* @typedef {'Backspace'} BACKSPACE
892892
* @property {BACKSPACE} BACKSPACE
893893
* @final
894894
*/
895-
export const BACKSPACE = 8;
895+
export const BACKSPACE = 'Backspace';
896+
896897
/**
897-
* @typedef {17} CONTROL
898+
* @typedef {'ControlLeft' | 'ControlRight'} CONTROL
898899
* @property {CONTROL} CONTROL
899900
* @final
900901
*/
901-
export const CONTROL = 17;
902+
export const CONTROL = 'ControlLeft';
903+
902904
/**
903-
* @typedef {46} DELETE
905+
* @typedef {'Delete'} DELETE
904906
* @property {DELETE} DELETE
905907
* @final
906908
*/
907-
export const DELETE = 46;
909+
export const DELETE = 'Delete';
910+
908911
/**
909-
* @typedef {40} DOWN_ARROW
912+
* @typedef {'ArrowDown'} DOWN_ARROW
910913
* @property {DOWN_ARROW} DOWN_ARROW
911914
* @final
912915
*/
913-
export const DOWN_ARROW = 40;
916+
export const DOWN_ARROW = 'ArrowDown';
917+
914918
/**
915-
* @typedef {13} ENTER
919+
* @typedef {'Enter'} ENTER
916920
* @property {ENTER} ENTER
917921
* @final
918922
*/
919-
export const ENTER = 13;
923+
export const ENTER = 'Enter';
924+
920925
/**
921-
* @typedef {27} ESCAPE
926+
* @typedef {'Escape'} ESCAPE
922927
* @property {ESCAPE} ESCAPE
923928
* @final
924929
*/
925-
export const ESCAPE = 27;
930+
export const ESCAPE = 'Escape';
931+
926932
/**
927-
* @typedef {37} LEFT_ARROW
933+
* @typedef {'ArrowLeft'} LEFT_ARROW
928934
* @property {LEFT_ARROW} LEFT_ARROW
929935
* @final
930936
*/
931-
export const LEFT_ARROW = 37;
937+
export const LEFT_ARROW = 'ArrowLeft';
938+
932939
/**
933-
* @typedef {18} OPTION
940+
* @typedef {'AltLeft' | 'AltRight'} OPTION
934941
* @property {OPTION} OPTION
935942
* @final
936943
*/
937-
export const OPTION = 18;
944+
export const OPTION = 'AltLeft';
945+
938946
/**
939-
* @typedef {13} RETURN
947+
* @typedef {'Enter'} RETURN
940948
* @property {RETURN} RETURN
941949
* @final
942950
*/
943-
export const RETURN = 13;
951+
export const RETURN = 'Enter';
952+
944953
/**
945-
* @typedef {39} RIGHT_ARROW
954+
* @typedef {'ArrowRight'} RIGHT_ARROW
946955
* @property {RIGHT_ARROW} RIGHT_ARROW
947956
* @final
948957
*/
949-
export const RIGHT_ARROW = 39;
958+
export const RIGHT_ARROW = 'ArrowRight';
959+
950960
/**
951-
* @typedef {16} SHIFT
961+
* @typedef {'ShiftLeft' | 'ShiftRight'} SHIFT
952962
* @property {SHIFT} SHIFT
953963
* @final
954964
*/
955-
export const SHIFT = 16;
965+
export const SHIFT = 'ShiftLeft';
966+
956967
/**
957-
* @typedef {9} TAB
968+
* @typedef {'Tab'} TAB
958969
* @property {TAB} TAB
959970
* @final
960971
*/
961-
export const TAB = 9;
972+
export const TAB = 'Tab';
973+
962974
/**
963-
* @typedef {38} UP_ARROW
975+
* @typedef {'ArrowUp'} UP_ARROW
964976
* @property {UP_ARROW} UP_ARROW
965977
* @final
966978
*/
967-
export const UP_ARROW = 38;
979+
export const UP_ARROW = 'ArrowUp';
968980

969981
// RENDERING
970982
/**

src/core/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ class p5 {
422422

423423
this._styles = [];
424424
this._downKeys = {}; //Holds the key codes of currently pressed keys
425+
this._downKeyCodes = {};
425426
}
426427
}
427428

src/events/keyboard.js

+43-29
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@
44
* @for p5
55
* @requires core
66
*/
7-
7+
export function isCode(input) {
8+
const leftRightKeys = [
9+
'Alt',
10+
'Shift',
11+
'Control',
12+
'Meta',
13+
];
14+
if (leftRightKeys.includes(input)) {
15+
return false;
16+
}
17+
if (typeof input !== 'string') {
18+
return false;
19+
}
20+
return input.length > 1;
21+
}
822
function keyboard(p5, fn){
923
/**
1024
* A `Boolean` system variable that's `true` if any key is currently pressed
@@ -95,7 +109,9 @@ function keyboard(p5, fn){
95109
* </code>
96110
* </div>
97111
*/
112+
98113
fn.keyIsPressed = false;
114+
fn.code = null;
99115

100116
/**
101117
* A `String` system variable that contains the value of the last key typed.
@@ -439,23 +455,16 @@ function keyboard(p5, fn){
439455
* </div>
440456
*/
441457
fn._onkeydown = function(e) {
442-
if (e.repeat) {
443-
// Ignore repeated key events when holding down a key
458+
if (this._downKeys[e.code]) {
444459
return;
445460
}
446461

447462
this.keyIsPressed = true;
448463
this.keyCode = e.which;
449-
this._downKeys[e.which] = true;
450-
this.key = e.key || String.fromCharCode(e.which) || e.which;
451-
452-
// Track keys pressed with meta key
453-
if (e.metaKey) {
454-
if (!this._metaKeys) {
455-
this._metaKeys = [];
456-
}
457-
this._metaKeys.push(e.which);
458-
}
464+
this.key = e.key;
465+
this.code = e.code;
466+
this._downKeyCodes[e.code] = true;
467+
this._downKeys[e.key] = true;
459468

460469
const context = this._isGlobal ? window : this;
461470
if (typeof context.keyPressed === 'function' && !e.charCode) {
@@ -623,18 +632,20 @@ function keyboard(p5, fn){
623632
* </div>
624633
*/
625634
fn._onkeyup = function(e) {
626-
this._downKeys[e.which] = false;
627-
this.keyIsPressed = false;
628-
this._lastKeyCodeTyped = null;
635+
delete this._downKeyCodes[e.code];
636+
delete this._downKeys[e.key];
629637

630-
if (e.key === 'Meta') { // Meta key codes
631-
// When meta key is released, clear all keys pressed with it
632-
if (this._metaKeys) {
633-
this._metaKeys.forEach(key => {
634-
this._downKeys[key] = false;
635-
});
636-
this._metaKeys = [];
637-
}
638+
639+
if (!this._areDownKeys()) {
640+
this.keyIsPressed = false;
641+
this.key = '';
642+
this.code = null;
643+
} else {
644+
// If other keys are still pressed, update code to the last pressed key
645+
const lastPressedCode = Object.keys(this._downKeyCodes).pop();
646+
this.code = lastPressedCode;
647+
const lastPressedKey = Object.keys(this._downKeys).pop();
648+
this.key = lastPressedKey;
638649
}
639650

640651
const context = this._isGlobal ? window : this;
@@ -821,7 +832,7 @@ function keyboard(p5, fn){
821832
* <a href="https://keycode.info" target="_blank">keycode.info</a>.
822833
*
823834
* @method keyIsDown
824-
* @param {Number} code key to check.
835+
* @param {Number|String} code key to check.
825836
* @return {Boolean} whether the key is down or not.
826837
*
827838
* @example
@@ -913,11 +924,14 @@ function keyboard(p5, fn){
913924
* </code>
914925
* </div>
915926
*/
916-
fn.keyIsDown = function(code) {
917-
// p5._validateParameters('keyIsDown', arguments);
918-
return this._downKeys[code] || false;
919-
};
920927

928+
fn.keyIsDown = function(input) {
929+
if (isCode(input)) {
930+
return this._downKeyCodes[input] || this._downKeys[input] || false;
931+
} else {
932+
return this._downKeys[input] || this._downKeyCodes[input] || false;
933+
}
934+
}
921935
/**
922936
* The _areDownKeys function returns a boolean true if any keys pressed
923937
* and a false if no keys are currently pressed.

test/unit/events/keyboard.js

+58-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import p5 from '../../../src/app.js';
2+
import { isCode } from '../../../src/events/keyboard.js';
23
import { parallelSketches } from '../../js/p5_helpers';
34

45
suite('Keyboard Events', function() {
@@ -164,18 +165,68 @@ suite('Keyboard Events', function() {
164165
});
165166
});
166167

168+
suite('isCode', function() {
169+
test('returns false for non-string inputs', function() {
170+
assert.isFalse(isCode(null));
171+
assert.isFalse(isCode(undefined));
172+
assert.isFalse(isCode(123));
173+
assert.isFalse(isCode({}));
174+
assert.isFalse(isCode([]));
175+
});
176+
177+
test('returns false for single non-digit and digit characters', function() {
178+
assert.isFalse(isCode('a'));
179+
assert.isFalse(isCode('Z'));
180+
assert.isFalse(isCode('1'));
181+
assert.isFalse(isCode('2'));
182+
assert.isFalse(isCode(' '));
183+
});
184+
185+
test('returns true for multi-character strings', function() {
186+
assert.isTrue(isCode('ShiftLeft'));
187+
assert.isTrue(isCode('ArrowUp'));
188+
assert.isTrue(isCode('ab'));
189+
});
190+
191+
test('returns false/true for apppropriate leftright keys and codes', function() {
192+
assert.isFalse(isCode('Alt'));
193+
assert.isFalse(isCode('Shift'));
194+
assert.isTrue(isCode('AltLeft'));
195+
assert.isTrue(isCode('ShiftLeft'));
196+
});
197+
198+
test('handles edge cases correctly', function() {
199+
assert.isFalse(isCode('')); // empty string
200+
assert.isTrue(isCode('11')); // multi-digit number
201+
assert.isTrue(isCode('1a')); // digit + letter
202+
});
203+
});
204+
167205
suite('p5.prototype.keyIsDown', function() {
168206
test('keyIsDown should return a boolean', function() {
169-
assert.isBoolean(myp5.keyIsDown(65));
207+
assert.isBoolean(myp5.keyIsDown('a'));
208+
assert.isBoolean(myp5.keyIsDown('Enter'));
170209
});
171210

172211
test('keyIsDown should return true if key is down', function() {
173-
window.dispatchEvent(new KeyboardEvent('keydown', { keyCode: 35 }));
174-
assert.strictEqual(myp5.keyIsDown(35), true);
175-
});
176-
177-
test.todo('keyIsDown should return false if key is not down', function() {
178-
assert.strictEqual(myp5.keyIsDown(35), false);
212+
// Test single character keys
213+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'a' }));
214+
assert.strictEqual(myp5.keyIsDown('a'), true);
215+
216+
// Test digit keys
217+
window.dispatchEvent(new KeyboardEvent('keydown', { key: '1', code: 'Digit1' }));
218+
assert.strictEqual(myp5.keyIsDown('1'), true);
219+
220+
// Test special keys
221+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter' }));
222+
assert.strictEqual(myp5.keyIsDown('Enter'), true);
223+
});
224+
225+
test('keyIsDown should return false if key is not down', function() {
226+
// Ensure key is not down
227+
window.dispatchEvent(new KeyboardEvent('keyup'));
228+
assert.strictEqual(myp5.keyIsDown('z'), false);
229+
179230
});
180231
});
181232
});

0 commit comments

Comments
 (0)