Skip to content

Commit 82a4635

Browse files
authored
feat(keypad): add keyboard support
1 parent 1b26c51 commit 82a4635

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

src/membrane-keypad-element.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ export class MembraneKeypadElement extends LitElement {
1414

1515
renderKey(text: string, x: number, y: number) {
1616
const keyClass = isNumeric(text) ? 'blue-key' : 'red-key';
17+
const keyName = text.toUpperCase();
1718

1819
return svg`<g
1920
transform="translate(${x} ${y})"
2021
tabindex="0"
2122
class=${keyClass}
23+
data-key-name=${keyName}
2224
@blur=${(e: FocusEvent) => {
23-
this.up(text, e);
25+
this.up(text, e.currentTarget as SVGElement);
2426
}}
2527
@mousedown=${() => this.down(text)}
2628
@mouseup=${() => this.up(text)}
2729
@touchstart=${() => this.down(text)}
2830
@touchend=${() => this.up(text)}
29-
@keydown=${(e: KeyboardEvent) => e.keyCode === SPACE_KEY && this.down(text, e)}
30-
@keyup=${(e: KeyboardEvent) => e.keyCode === SPACE_KEY && this.up(text, e)}
31+
@keydown=${(e: KeyboardEvent) =>
32+
e.keyCode === SPACE_KEY && this.down(text, e.currentTarget as SVGElement)}
33+
@keyup=${(e: KeyboardEvent) =>
34+
e.keyCode === SPACE_KEY && this.up(text, e.currentTarget as SVGElement)}
3135
>
3236
<use xlink:href="#key" />
3337
<text x="5.6" y="8.1">${text}</text>
@@ -92,6 +96,8 @@ export class MembraneKeypadElement extends LitElement {
9296
font-size="8.2px"
9397
text-anchor="middle"
9498
xmlns="http://www.w3.org/2000/svg"
99+
@keydown=${(e: KeyboardEvent) => this.keyStrokeDown(e.key)}
100+
@keyup=${(e: KeyboardEvent) => this.keyStrokeUp(e.key)}
95101
>
96102
<defs>
97103
<rect
@@ -153,11 +159,10 @@ export class MembraneKeypadElement extends LitElement {
153159
`;
154160
}
155161

156-
private down(key: string, event?: UIEvent) {
162+
private down(key: string, element?: Element) {
157163
if (!this.pressedKeys.has(key)) {
158-
if (event) {
159-
const currTarget = event.currentTarget as SVGElement;
160-
currTarget.classList.add('pressed');
164+
if (element) {
165+
element.classList.add('pressed');
161166
}
162167
this.pressedKeys.add(key);
163168
this.dispatchEvent(
@@ -168,11 +173,10 @@ export class MembraneKeypadElement extends LitElement {
168173
}
169174
}
170175

171-
private up(key: string, event?: UIEvent) {
176+
private up(key: string, element?: Element) {
172177
if (this.pressedKeys.has(key)) {
173-
if (event) {
174-
const currTarget = event.currentTarget as SVGElement;
175-
currTarget.classList.remove('pressed');
178+
if (element) {
179+
element.classList.remove('pressed');
176180
}
177181
this.pressedKeys.delete(key);
178182
this.dispatchEvent(
@@ -182,4 +186,31 @@ export class MembraneKeypadElement extends LitElement {
182186
);
183187
}
184188
}
189+
190+
private keyStrokeDown(key: string) {
191+
const text = key.toUpperCase();
192+
const selectedKey = this.shadowRoot?.querySelector(`[data-key-name="${text}"]`);
193+
if (selectedKey) {
194+
this.down(text, selectedKey as SVGElement);
195+
}
196+
}
197+
198+
private keyStrokeUp(key: string) {
199+
const text = key.toUpperCase();
200+
const selectedKey = this.shadowRoot?.querySelector(`[data-key-name="${text}"]`);
201+
const pressedKeys: NodeListOf<SVGElement> | undefined = this.shadowRoot?.querySelectorAll(
202+
'.pressed'
203+
);
204+
205+
if (key === 'Shift') {
206+
pressedKeys?.forEach((pressedKey) => {
207+
const pressedText = pressedKey.dataset.keyName!;
208+
this.up(pressedText, pressedKey);
209+
});
210+
}
211+
212+
if (selectedKey) {
213+
this.up(text, selectedKey as SVGElement);
214+
}
215+
}
185216
}

0 commit comments

Comments
 (0)