-
Notifications
You must be signed in to change notification settings - Fork 10
/
keyboard_test.go
86 lines (68 loc) · 2.01 KB
/
keyboard_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"runtime"
"testing"
"time"
"github.com/micmonay/keybd_event"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type TestKey struct {
Key int
CtrlEnabled bool
}
type TestKeyMap map[KeyType]TestKey
func TestPollKeyEvents(t *testing.T) {
testKeys := TestKeyMap{
Enter: TestKey{keybd_event.VK_ENTER, false},
Esc: TestKey{keybd_event.VK_ESC, false},
Tab: TestKey{keybd_event.VK_TAB, false},
CtrlA: TestKey{keybd_event.VK_A, true},
CtrlB: TestKey{keybd_event.VK_B, true},
Space: TestKey{keybd_event.VK_SPACE, false},
Delete: TestKey{keybd_event.VK_DELETE, false},
UpArrow: TestKey{keybd_event.VK_UP, false},
DownArrow: TestKey{keybd_event.VK_DOWN, false},
LeftArrow: TestKey{keybd_event.VK_LEFT, false},
RightArrow: TestKey{keybd_event.VK_RIGHT, false},
}
kb, err := keybd_event.NewKeyBonding()
if err != nil {
t.Skipf("Could not run KeyBonding: %v", err)
}
if runtime.GOOS == "linux" {
time.Sleep(2 * time.Second)
}
kb.SetKeys(keybd_event.VK_B, keybd_event.VK_I, keybd_event.VK_F,
keybd_event.VK_R, keybd_event.VK_O,
keybd_event.VK_S, keybd_event.VK_T)
err = kb.Launching()
require.NoError(t, err)
var testOutput []byte
for i := 0; i < 7; i++ {
key := pollKeyEvents()
testOutput = append(testOutput, key.Value...)
}
assert.Equal(t, []byte("bifrost"), testOutput)
for k, v := range testKeys {
kb.SetKeys(v.Key)
kb.HasCTRL(v.CtrlEnabled)
err = kb.Launching()
require.NoError(t, err)
key := pollKeyEvents()
assert.Equal(t, k, key.Type)
}
// Test CtrlC signal interrupt
fmt.Println("\n\nPress CtrlC to proceed...")
key := pollKeyEvents()
assert.Equal(t, CtrlC, key.Type)
// Test CtrlBackslash signal interrupt
fmt.Println("Press Ctrl-\\ to proceed...")
key = pollKeyEvents()
assert.Equal(t, CtrlBackslash, key.Type)
// Test Backspace signal interrupt
fmt.Println("Press Backspace to proceed...")
key = pollKeyEvents()
assert.Equal(t, Backspace, key.Type)
}