Skip to content

Commit 4f9ebda

Browse files
committed
Add Keyboard Functions example
1 parent 4a3c1f0 commit 4f9ebda

2 files changed

Lines changed: 13 additions & 47 deletions

File tree

assets/examples_js/Basics/input/Keyboard_Functions/Keyboard_Functions.js

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@ let maxHeight = 40;
22
let minHeight = 20;
33
let letterHeight = maxHeight;
44
let letterWidth = 20;
5-
65
let x = -20;
76
let y = 0;
8-
97
let newletter = false;
10-
118
let numChars = 26;
129
let colors = [];
1310

1411
function setup() {
1512
createCanvas(640, 360);
1613
noStroke();
1714
colorMode(HSB, numChars);
18-
1915
background(numChars / 2);
20-
2116
for (let i = 0; i < numChars; i++) {
2217
colors[i] = color(i, numChars, numChars);
2318
}
@@ -26,56 +21,44 @@ function setup() {
2621
function draw() {
2722
if (newletter) {
2823
let y_pos;
29-
3024
if (letterHeight === maxHeight) {
3125
y_pos = y;
32-
fill(currentFill);
3326
rect(x, y_pos, letterWidth, letterHeight);
3427
} else {
3528
y_pos = y + minHeight;
3629
rect(x, y_pos, letterWidth, letterHeight);
37-
3830
fill(numChars / 2);
3931
rect(x, y_pos - minHeight, letterWidth, letterHeight);
4032
}
41-
4233
newletter = false;
4334
}
4435
}
4536

46-
let currentFill;
47-
4837
function keyPressed() {
49-
if (
50-
(key >= 'A' && key <= 'Z') ||
51-
(key >= 'a' && key <= 'z')
52-
) {
38+
if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z')) {
5339
let keyIndex;
54-
5540
if (key <= 'Z') {
5641
keyIndex = key.charCodeAt(0) - 'A'.charCodeAt(0);
5742
letterHeight = maxHeight;
43+
fill(colors[keyIndex]);
5844
} else {
5945
keyIndex = key.charCodeAt(0) - 'a'.charCodeAt(0);
6046
letterHeight = minHeight;
47+
fill(colors[keyIndex]);
6148
}
62-
63-
currentFill = colors[keyIndex];
6449
} else {
65-
currentFill = color(0);
50+
fill(0);
6651
letterHeight = 10;
6752
}
68-
6953
newletter = true;
70-
7154
x += letterWidth;
72-
7355
if (x > width - letterWidth) {
7456
x = 0;
7557
y += maxHeight;
7658
}
77-
7859
if (y > height - letterHeight) {
7960
y = 0;
61+
background(numChars / 2);
8062
}
81-
}
63+
return false; // prevent browser from handling keys
64+
}

examples/keyboard-functions.html

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,17 @@ <h1>Keyboard Functions</h1>
7272
let minHeight = 20;
7373
let letterHeight = maxHeight;
7474
let letterWidth = 20;
75-
7675
let x = -20;
7776
let y = 0;
78-
7977
let newletter = false;
80-
8178
let numChars = 26;
8279
let colors = [];
8380
8481
function setup() {
8582
createCanvas(640, 360);
8683
noStroke();
8784
colorMode(HSB, numChars);
88-
8985
background(numChars / 2);
90-
9186
for (let i = 0; i < numChars; i++) {
9287
colors[i] = color(i, numChars, numChars);
9388
}
@@ -96,58 +91,46 @@ <h1>Keyboard Functions</h1>
9691
function draw() {
9792
if (newletter) {
9893
let y_pos;
99-
10094
if (letterHeight === maxHeight) {
10195
y_pos = y;
102-
fill(currentFill);
10396
rect(x, y_pos, letterWidth, letterHeight);
10497
} else {
10598
y_pos = y + minHeight;
10699
rect(x, y_pos, letterWidth, letterHeight);
107-
108100
fill(numChars / 2);
109101
rect(x, y_pos - minHeight, letterWidth, letterHeight);
110102
}
111-
112103
newletter = false;
113104
}
114105
}
115106
116-
let currentFill;
117-
118107
function keyPressed() {
119-
if (
120-
(key >= 'A' && key <= 'Z') ||
121-
(key >= 'a' && key <= 'z')
122-
) {
108+
if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z')) {
123109
let keyIndex;
124-
125110
if (key <= 'Z') {
126111
keyIndex = key.charCodeAt(0) - 'A'.charCodeAt(0);
127112
letterHeight = maxHeight;
113+
fill(colors[keyIndex]);
128114
} else {
129115
keyIndex = key.charCodeAt(0) - 'a'.charCodeAt(0);
130116
letterHeight = minHeight;
117+
fill(colors[keyIndex]);
131118
}
132-
133-
currentFill = colors[keyIndex];
134119
} else {
135-
currentFill = color(0);
120+
fill(0);
136121
letterHeight = 10;
137122
}
138-
139123
newletter = true;
140-
141124
x += letterWidth;
142-
143125
if (x > width - letterWidth) {
144126
x = 0;
145127
y += maxHeight;
146128
}
147-
148129
if (y > height - letterHeight) {
149130
y = 0;
131+
background(numChars / 2);
150132
}
133+
return false; // prevent browser from handling keys
151134
}<\/script></body></html>`);doc.close();})();</script>
152135
<div class="code-block">
153136
<div class="code-header">

0 commit comments

Comments
 (0)