Skip to content

Commit 1fbabd1

Browse files
authored
Change the timing for deactivating Combos (#56)
1 parent f5e584b commit 1fbabd1

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

keyboard.go

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -245,41 +245,43 @@ func (d *Device) Tick() error {
245245
}
246246

247247
d.founds = d.founds[:0]
248-
for _, xx := range noneToPresse {
249-
kbidx, layer, index := decKey(xx)
250-
x := d.kb[kbidx].Key(layer, index)
251-
for _, combo := range d.Combos {
252-
for _, ckey := range combo[:4] {
253-
if keycodeViaToTGK(ckey) == x {
254-
uniq := true
255-
for _, f := range d.founds {
256-
if f == ckey {
257-
uniq = false
248+
if d.combosKey == 0xFFFFFFFF {
249+
for _, xx := range noneToPresse {
250+
kbidx, layer, index := decKey(xx)
251+
x := d.kb[kbidx].Key(layer, index)
252+
for _, combo := range d.Combos {
253+
for _, ckey := range combo[:4] {
254+
if keycodeViaToTGK(ckey) == x {
255+
uniq := true
256+
for _, f := range d.founds {
257+
if f == ckey {
258+
uniq = false
259+
}
258260
}
261+
if uniq {
262+
d.founds = append(d.founds, ckey)
263+
}
264+
if d.combosTimer.IsZero() {
265+
d.combosTimer = time.Now().Add(48 * time.Millisecond)
266+
}
267+
d.combosPressed[xx] = struct{}{}
259268
}
260-
if uniq {
261-
d.founds = append(d.founds, ckey)
262-
}
263-
if d.combosTimer.IsZero() {
264-
d.combosTimer = time.Now().Add(48 * time.Millisecond)
265-
}
266-
d.combosPressed[xx] = struct{}{}
267269
}
268270
}
269271
}
270-
}
271-
if len(d.founds) == len(noneToPresse) {
272-
// Remove the keys pressed before the Combos are completed.
273-
noneToPresse = noneToPresse[:0]
274-
} else {
275-
// Cancel the Combos waiting state if a key unrelated to Combos is pressed.
276-
d.combosTimer = time.Time{}
277-
for xx := range d.combosPressed {
278-
noneToPresse = append(noneToPresse, xx)
279-
delete(d.combosPressed, xx)
272+
if len(d.founds) == len(noneToPresse) {
273+
// Remove the keys pressed before the Combos are completed.
274+
noneToPresse = noneToPresse[:0]
275+
} else {
276+
// Cancel the Combos waiting state if a key unrelated to Combos is pressed.
277+
d.combosTimer = time.Time{}
278+
for xx := range d.combosPressed {
279+
noneToPresse = append(noneToPresse, xx)
280+
delete(d.combosPressed, xx)
281+
}
282+
pressToRelease = append(d.combosReleased, pressToRelease...)
283+
d.combosReleased = d.combosReleased[:0]
280284
}
281-
pressToRelease = append(d.combosReleased, pressToRelease...)
282-
d.combosReleased = d.combosReleased[:0]
283285
}
284286

285287
if !d.combosTimer.IsZero() {
@@ -321,9 +323,6 @@ func (d *Device) Tick() error {
321323
if matched {
322324
noneToPresse = append(noneToPresse, d.combosKey)
323325

324-
for k := range d.combosPressed {
325-
delete(d.combosPressed, k)
326-
}
327326
d.combosReleased = d.combosReleased[:0]
328327
} else {
329328
for k := range d.combosPressed {
@@ -399,8 +398,13 @@ func (d *Device) Tick() error {
399398
}
400399
}
401400

402-
if len(pressToRelease) > 0 && d.combosKey != 0xFFFFFFFF {
403-
// For simplicity, the release timing is set to when any key is released.
401+
for _, xx := range pressToRelease {
402+
if _, ok := d.combosPressed[xx]; ok {
403+
delete(d.combosPressed, xx)
404+
}
405+
}
406+
if len(pressToRelease) > 0 && d.combosKey != 0xFFFFFFFF && len(d.combosPressed) == 0 {
407+
// Combos are deactivated when the last key that constitutes the combo is released.
404408
pressToRelease = append(pressToRelease, d.combosKey)
405409
d.combosKey = 0xFFFFFFFF
406410
}

0 commit comments

Comments
 (0)