diff --git a/src/device/input/random.go b/src/device/input/random.go index 8825936..8530ba4 100644 --- a/src/device/input/random.go +++ b/src/device/input/random.go @@ -22,7 +22,7 @@ func NewRandom(start, stop byte) *Random { } func (r *Random) Read() []Button { - if rand.Int()%2 == 0 { + if len(r.down) > 0 && rand.Int()%2 == 0 { was := r.down for i := range was { was[i].Down = false diff --git a/src/device/output/key/char.go b/src/device/output/key/char.go new file mode 100644 index 0000000..4f68066 --- /dev/null +++ b/src/device/output/key/char.go @@ -0,0 +1,58 @@ +package key + +var ( + charToKey = map[byte]Key{ + 'a': A, + 'b': B, + 'c': C, + 'd': D, + 'e': E, + 'f': F, + 'g': G, + 'h': H, + 'i': I, + 'j': J, + 'k': K, + 'l': L, + 'm': M, + 'n': N, + 'o': O, + 'p': P, + 'q': Q, + 'r': R, + 's': S, + 't': T, + 'u': U, + 'v': V, + 'w': W, + 'x': X, + 'y': Y, + 'z': Z, + } + keyToChar = func() map[Key]byte { + result := map[Key]byte{} + for k, v := range charToKey { + result[v] = k + } + return result + }() +) + +func ToChar(k Key) byte { + v, ok := keyToChar[k] + if !ok { + return '?' + } + return v +} + +func FromChar(b byte) Key { + if b < 'a' { + b += 'a' - 'A' + } + v, ok := charToKey[b] + if !ok { + return Undef + } + return v +} diff --git a/src/device/output/key/char_test.go b/src/device/output/key/char_test.go new file mode 100644 index 0000000..9f705f7 --- /dev/null +++ b/src/device/output/key/char_test.go @@ -0,0 +1,21 @@ +package key + +import "testing" + +func TestFromChar(t *testing.T) { + if got := FromChar('a'); got != A { + t.Error(got) + } + if got := FromChar('A'); got != A { + t.Error(got) + } + if got := FromChar('!'); got != Undef { + t.Error(got) + } + if got := ToChar(A); got != 'a' { + t.Error(got) + } + if got := ToChar(-1); got != '?' { + t.Error(got) + } +} diff --git a/src/device/output/key/keys.go b/src/device/output/key/keys.go new file mode 100644 index 0000000..32225fe --- /dev/null +++ b/src/device/output/key/keys.go @@ -0,0 +1,35 @@ +package key + +import "github.com/micmonay/keybd_event" + +type Key int + +const ( + Undef = Key(keybd_event.VK_SP11) + A = Key(keybd_event.VK_A) + B = Key(keybd_event.VK_B) + C = Key(keybd_event.VK_C) + D = Key(keybd_event.VK_D) + E = Key(keybd_event.VK_E) + F = Key(keybd_event.VK_F) + G = Key(keybd_event.VK_G) + H = Key(keybd_event.VK_H) + I = Key(keybd_event.VK_I) + J = Key(keybd_event.VK_J) + K = Key(keybd_event.VK_K) + L = Key(keybd_event.VK_L) + M = Key(keybd_event.VK_M) + N = Key(keybd_event.VK_N) + O = Key(keybd_event.VK_O) + P = Key(keybd_event.VK_P) + Q = Key(keybd_event.VK_Q) + R = Key(keybd_event.VK_R) + S = Key(keybd_event.VK_S) + T = Key(keybd_event.VK_T) + U = Key(keybd_event.VK_U) + V = Key(keybd_event.VK_V) + W = Key(keybd_event.VK_W) + X = Key(keybd_event.VK_X) + Y = Key(keybd_event.VK_Y) + Z = Key(keybd_event.VK_Z) +) diff --git a/src/device/output/keyboard.go b/src/device/output/keyboard.go index 6609768..19c866d 100644 --- a/src/device/output/keyboard.go +++ b/src/device/output/keyboard.go @@ -2,6 +2,7 @@ package output import ( "log" + "mayhem-party/src/device/output/key" "time" "github.com/micmonay/keybd_event" @@ -29,7 +30,7 @@ func (kb Keyboard) wait() { } } -func (kb Keyboard) Press(keys ...Key) { +func (kb Keyboard) Press(keys ...key.Key) { kb.wait() kb.release() diff --git a/src/device/output/keyboard_test.go b/src/device/output/keyboard_test.go index af8dfb0..5a54281 100644 --- a/src/device/output/keyboard_test.go +++ b/src/device/output/keyboard_test.go @@ -4,6 +4,7 @@ package output_test import ( "mayhem-party/src/device/output" + "mayhem-party/src/device/output/key" "testing" "time" ) @@ -13,7 +14,7 @@ func TestKeyboardIntegration(t *testing.T) { t.Cleanup(kb.Close) t.Log("pressing 'a' for 5 seconds") - kb.Press(output.A) + kb.Press(key.A) for i := 0; i < 5; i++ { t.Logf("\t%d...", 5-i) time.Sleep(time.Second) diff --git a/src/device/output/keys.go b/src/device/output/keys.go deleted file mode 100644 index 9f2139e..0000000 --- a/src/device/output/keys.go +++ /dev/null @@ -1,34 +0,0 @@ -package output - -import "github.com/micmonay/keybd_event" - -type Key int - -const ( - A = Key(keybd_event.VK_A) - B = Key(keybd_event.VK_B) - C = Key(keybd_event.VK_C) - D = Key(keybd_event.VK_D) - E = Key(keybd_event.VK_E) - F = Key(keybd_event.VK_F) - G = Key(keybd_event.VK_G) - H = Key(keybd_event.VK_H) - I = Key(keybd_event.VK_I) - J = Key(keybd_event.VK_J) - K = Key(keybd_event.VK_K) - L = Key(keybd_event.VK_L) - M = Key(keybd_event.VK_M) - N = Key(keybd_event.VK_N) - O = Key(keybd_event.VK_O) - P = Key(keybd_event.VK_P) - Q = Key(keybd_event.VK_Q) - R = Key(keybd_event.VK_R) - S = Key(keybd_event.VK_S) - T = Key(keybd_event.VK_T) - U = Key(keybd_event.VK_U) - V = Key(keybd_event.VK_V) - W = Key(keybd_event.VK_W) - X = Key(keybd_event.VK_X) - Y = Key(keybd_event.VK_Y) - Z = Key(keybd_event.VK_Z) -) diff --git a/src/device/output/output.go b/src/device/output/output.go index 7e695ab..e18ccf3 100644 --- a/src/device/output/output.go +++ b/src/device/output/output.go @@ -1,10 +1,13 @@ package output -import "os" +import ( + "mayhem-party/src/device/output/key" + "os" +) type Output interface { Close() - Press(...Key) + Press(...key.Key) } func New() Output { diff --git a/src/device/output/writer.go b/src/device/output/writer.go index 9170ef2..bbd2605 100644 --- a/src/device/output/writer.go +++ b/src/device/output/writer.go @@ -3,6 +3,7 @@ package output import ( "fmt" "io" + "mayhem-party/src/device/output/key" ) type Writer struct { @@ -19,6 +20,10 @@ func (w Writer) Close() { } } -func (w Writer) Press(keys ...Key) { - fmt.Fprintf(w.w, "%+v\n", keys) +func (w Writer) Press(keys ...key.Key) { + result := make([]byte, len(keys)) + for i := range keys { + result[i] = key.ToChar(keys[i]) + } + fmt.Fprintf(w.w, "%s\n", result) } diff --git a/src/main.go b/src/main.go index ddd9053..1e57ecd 100644 --- a/src/main.go +++ b/src/main.go @@ -4,6 +4,7 @@ import ( "context" "mayhem-party/src/device/input" "mayhem-party/src/device/output" + "mayhem-party/src/device/output/key" "time" ) @@ -22,13 +23,13 @@ func Main(ctx context.Context) error { } } - state := map[output.Key]bool{} + state := map[key.Key]bool{} for block() { delta := reader.Read() for _, button := range delta { - state[output.Key(button.Char)] = button.Down + state[key.FromChar(button.Char)] = button.Down } - keys := make([]output.Key, 0) + keys := make([]key.Key, 0) for k, v := range state { if v { keys = append(keys, k)