From ecbce05cf3e7f79dccc604f468efff26e7cc81de Mon Sep 17 00:00:00 2001 From: bel Date: Wed, 1 Mar 2023 21:11:26 -0700 Subject: [PATCH] press empty to release and defer close --- src/device/output/keyboard.go | 12 ++++++++++-- src/device/output/keyboard_test.go | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/device/output/keyboard.go b/src/device/output/keyboard.go index f22954e..52bce77 100644 --- a/src/device/output/keyboard.go +++ b/src/device/output/keyboard.go @@ -32,14 +32,22 @@ func (kb Keyboard) wait() { func (kb Keyboard) Press(keys ...int) { kb.wait() - kb.Release() + kb.release() + if len(keys) == 0 { + return + } + kb.kb.SetKeys(keys...) if err := kb.kb.Press(); err != nil { log.Println("failed to press keys:", err) } } -func (kb Keyboard) Release() { +func (kb Keyboard) Close() { + kb.release() +} + +func (kb Keyboard) release() { kb.wait() if err := kb.kb.Release(); err != nil { diff --git a/src/device/output/keyboard_test.go b/src/device/output/keyboard_test.go index ee7724c..af8dfb0 100644 --- a/src/device/output/keyboard_test.go +++ b/src/device/output/keyboard_test.go @@ -10,7 +10,7 @@ import ( func TestKeyboardIntegration(t *testing.T) { kb := output.NewKeyboard() - defer kb.Release() + t.Cleanup(kb.Close) t.Log("pressing 'a' for 5 seconds") kb.Press(output.A) @@ -19,5 +19,5 @@ func TestKeyboardIntegration(t *testing.T) { time.Sleep(time.Second) } t.Logf("releasing") - kb.Release() + kb.Press() }