11suite ( 'Keyboard Events' , function ( ) {
2+ var myp5 ;
3+
4+ setup ( function ( done ) {
5+ new p5 ( function ( p ) {
6+ p . setup = function ( ) {
7+ myp5 = p ;
8+ done ( ) ;
9+ } ;
10+ } ) ;
11+ } ) ;
12+
13+ teardown ( function ( ) {
14+ myp5 . remove ( ) ;
15+ } ) ;
16+
17+ suite ( 'p5.prototype.keyIsPressed' , function ( ) {
18+ test ( 'keyIsPressed should be a boolean' , function ( ) {
19+ assert . isBoolean ( myp5 . keyIsPressed ) ;
20+ } ) ;
21+
22+ test ( 'keyIsPressed should be true on key press' , function ( ) {
23+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' ) ) ;
24+ assert . strictEqual ( myp5 . keyIsPressed , true ) ;
25+ } ) ;
26+
27+ test ( 'keyIsPressed should be false on key up' , function ( ) {
28+ window . dispatchEvent ( new KeyboardEvent ( 'keyup' ) ) ;
29+ assert . strictEqual ( myp5 . keyIsPressed , false ) ;
30+ } ) ;
31+ } ) ;
32+
33+ suite ( 'p5.prototype.isKeyPressed' , function ( ) {
34+ test ( 'isKeyPressed should be a boolean' , function ( ) {
35+ assert . isBoolean ( myp5 . isKeyPressed ) ;
36+ } ) ;
37+
38+ test ( 'isKeyPressed should be true on key press' , function ( ) {
39+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' ) ) ;
40+ assert . strictEqual ( myp5 . isKeyPressed , true ) ;
41+ } ) ;
42+
43+ test ( 'isKeyPressed should be false on key up' , function ( ) {
44+ window . dispatchEvent ( new KeyboardEvent ( 'keyup' ) ) ;
45+ assert . strictEqual ( myp5 . isKeyPressed , false ) ;
46+ } ) ;
47+ } ) ;
48+
49+ suite ( 'p5.prototype.key' , function ( ) {
50+ test ( 'key should be a string' , function ( ) {
51+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 's' } ) ) ;
52+ assert . isString ( myp5 . key ) ;
53+ } ) ;
54+
55+ test ( 'key should return the key pressed' , function ( ) {
56+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'A' } ) ) ;
57+ assert . strictEqual ( myp5 . key , 'A' ) ;
58+ } ) ;
59+
60+ test ( 'key should return the key pressed' , function ( ) {
61+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : '9' } ) ) ;
62+ assert . strictEqual ( myp5 . key , '9' ) ;
63+ } ) ;
64+
65+ test ( 'key should return the key pressed' , function ( ) {
66+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'CapsLock' } ) ) ;
67+ assert . strictEqual ( myp5 . key , 'CapsLock' ) ;
68+ } ) ;
69+ } ) ;
70+
71+ suite ( 'p5.prototype.keyCode' , function ( ) {
72+ test ( 'keyCode should be a number' , function ( ) {
73+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 's' } ) ) ;
74+ assert . isNumber ( myp5 . keyCode ) ;
75+ } ) ;
76+
77+ test ( 'key should return the key pressed' , function ( ) {
78+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { keyCode : 65 } ) ) ;
79+ assert . strictEqual ( myp5 . keyCode , 65 ) ;
80+ } ) ;
81+
82+ test ( 'key should return the key pressed' , function ( ) {
83+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' , { keyCode : 20 } ) ) ;
84+ assert . strictEqual ( myp5 . keyCode , 20 ) ;
85+ } ) ;
86+ } ) ;
87+
288 suite ( 'keyPressed' , function ( ) {
389 test ( 'keyPressed functions on multiple instances must run once' , async function ( ) {
490 let sketchFn = function ( sketch , resolve , reject ) {
@@ -13,7 +99,50 @@ suite('Keyboard Events', function() {
1399 } ;
14100 let sketches = parallelSketches ( [ sketchFn , sketchFn ] ) ; //create two sketches
15101 await sketches . setup ; //wait for all sketches to setup
16- window . dispatchEvent ( new KeyboardEvent ( 'keydown' ) ) ; //dipatch a keyboard event to trigger the keyPressed functions
102+ window . dispatchEvent ( new KeyboardEvent ( 'keydown' ) ) ; //dispatch a keyboard event to trigger the keyPressed functions
103+ sketches . end ( ) ; //resolve all sketches by calling their finish functions
104+ let counts = await sketches . result ; //get array holding number of times keyPressed was called. Rejected sketches also thrown here
105+ assert . deepEqual ( counts , [ 1 , 1 ] ) ; //check if every keyPressed function was called once
106+ } ) ;
107+ } ) ;
108+
109+ suite ( 'keyReleased' , function ( ) {
110+ test ( 'keyReleased functions on multiple instances must run once' , async function ( ) {
111+ let sketchFn = function ( sketch , resolve , reject ) {
112+ let count = 0 ;
113+
114+ sketch . keyReleased = function ( ) {
115+ count += 1 ;
116+ } ;
117+
118+ sketch . finish = function ( ) {
119+ resolve ( count ) ;
120+ } ;
121+ } ;
122+ let sketches = parallelSketches ( [ sketchFn , sketchFn ] ) ; //create two sketches
123+ await sketches . setup ; //wait for all sketches to setup
124+ window . dispatchEvent ( new KeyboardEvent ( 'keyup' ) ) ; //dispatch a keyboard event to trigger the keyReleased functions
125+ sketches . end ( ) ; //resolve all sketches by calling their finish functions
126+ let counts = await sketches . result ; //get array holding number of times keyPressed was called. Rejected sketches also thrown here
127+ assert . deepEqual ( counts , [ 1 , 1 ] ) ; //check if every keyPressed function was called once
128+ } ) ;
129+ } ) ;
130+
131+ suite ( 'keyTyped' , function ( ) {
132+ test ( 'keyTyped functions on multiple instances must run once' , async function ( ) {
133+ let sketchFn = function ( sketch , resolve , reject ) {
134+ let count = 0 ;
135+ sketch . keyTyped = function ( ) {
136+ count += 1 ;
137+ } ;
138+
139+ sketch . finish = function ( ) {
140+ resolve ( count ) ;
141+ } ;
142+ } ;
143+ let sketches = parallelSketches ( [ sketchFn , sketchFn ] ) ; //create two sketches
144+ await sketches . setup ; //wait for all sketches to setup
145+ window . dispatchEvent ( new KeyboardEvent ( 'keypress' , { key : 'A' } ) ) ; //dispatch a keyboard event to trigger the keyTyped functions
17146 sketches . end ( ) ; //resolve all sketches by calling their finish functions
18147 let counts = await sketches . result ; //get array holding number of times keyPressed was called. Rejected sketches also thrown here
19148 assert . deepEqual ( counts , [ 1 , 1 ] ) ; //check if every keyPressed function was called once
0 commit comments