Skip to content

Commit b198ec2

Browse files
committed
fix(analog-joystick): improve keyboard support
always focus the knob when clicking inside it, and show the controls as long as the knob is in focus.
1 parent 3086b6b commit b198ec2

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/analog-joystick-element.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { css, customElement, html, LitElement, property } from 'lit-element';
1+
import { css, customElement, html, LitElement, property, query } from 'lit-element';
22
import { analog, ElementPin, GND, VCC } from './pin';
33
import { SPACE_KEYS } from './utils/keys';
44

@@ -8,6 +8,8 @@ export class AnalogJoystickElement extends LitElement {
88
@property({ type: Number }) yValue = 0;
99
@property() pressed = false;
1010

11+
@query('#knob') knob!: SVGCircleElement;
12+
1113
readonly pinInfo: ElementPin[] = [
1214
{ name: 'VCC', x: 33, y: 115.8, signals: [VCC()] },
1315
{ name: 'VERT', x: 42.6012, y: 115.8, signals: [analog(0)] },
@@ -38,6 +40,7 @@ export class AnalogJoystickElement extends LitElement {
3840
cursor: pointer;
3941
}
4042
43+
#knob:focus ~ .controls,
4144
#knob:hover ~ .controls {
4245
opacity: 1;
4346
}
@@ -173,7 +176,7 @@ export class AnalogJoystickElement extends LitElement {
173176
x="1"
174177
height="10"
175178
width="7"
176-
@mousedown=${() => this.mousedown(1, 0)}
179+
@mousedown=${(e: MouseEvent) => this.mousedown(e, 1, 0)}
177180
@mouseup=${() => this.mouseup(true, false)}
178181
/>
179182
<path d="m 7.022,11.459 -3.202,2.497 3.202,2.497" />
@@ -184,7 +187,7 @@ export class AnalogJoystickElement extends LitElement {
184187
x="7.9"
185188
height="7"
186189
width="10"
187-
@mousedown=${() => this.mousedown(0, 1)}
190+
@mousedown=${(e: MouseEvent) => this.mousedown(e, 0, 1)}
188191
@mouseup=${() => this.mouseup(false, true)}
189192
/>
190193
<path d="m 16.615,7.095 -2.497,-3.202 -2.497,3.202" />
@@ -195,7 +198,7 @@ export class AnalogJoystickElement extends LitElement {
195198
x="18"
196199
height="10"
197200
width="7"
198-
@mousedown=${() => this.mousedown(-1, 0)}
201+
@mousedown=${(e: MouseEvent) => this.mousedown(e, -1, 0)}
199202
@mouseup=${() => this.mouseup(true, false)}
200203
/>
201204
<path d="m 19.980,16.101 3.202,-2.497 -3.202,-2.497" />
@@ -206,7 +209,7 @@ export class AnalogJoystickElement extends LitElement {
206209
x="7.9"
207210
height="7"
208211
width="10"
209-
@mousedown=${() => this.mousedown(0, -1)}
212+
@mousedown=${(e: MouseEvent) => this.mousedown(e, 0, -1)}
210213
@mouseup=${() => this.mouseup(false, true)}
211214
/>
212215
<path d="m 11.620,20.112 2.497,3.202 2.497,-3.202" />
@@ -217,7 +220,7 @@ export class AnalogJoystickElement extends LitElement {
217220
r="3"
218221
stroke="#aaa"
219222
class=${this.pressed ? 'pressed' : ''}
220-
@mousedown=${() => this.press()}
223+
@mousedown=${(e: MouseEvent) => this.press(e)}
221224
@mouseup=${() => this.release()}
222225
/>
223226
</g>
@@ -272,14 +275,16 @@ export class AnalogJoystickElement extends LitElement {
272275
}
273276
}
274277

275-
private mousedown(dx: number, dy: number) {
278+
private mousedown(e: MouseEvent, dx: number, dy: number) {
276279
if (dx) {
277280
this.xValue = dx;
278281
}
279282
if (dy) {
280283
this.yValue = dy;
281284
}
282285
this.valueChanged();
286+
this.knob?.focus();
287+
e.preventDefault(); // Prevents stealing focus
283288
}
284289

285290
private mouseup(x: boolean, y: boolean) {
@@ -290,16 +295,20 @@ export class AnalogJoystickElement extends LitElement {
290295
this.yValue = 0;
291296
}
292297
this.valueChanged();
298+
this.knob?.focus();
293299
}
294300

295-
private press() {
301+
private press(e?: MouseEvent) {
296302
this.pressed = true;
297303
this.dispatchEvent(new InputEvent('button-press'));
304+
this.knob?.focus();
305+
e?.preventDefault(); // Prevents stealing focus
298306
}
299307

300308
private release() {
301309
this.pressed = false;
302310
this.dispatchEvent(new InputEvent('button-release'));
311+
this.knob?.focus();
303312
}
304313

305314
private valueChanged() {

0 commit comments

Comments
 (0)