key.To/FromChar
This commit is contained in:
58
src/device/output/key/char.go
Normal file
58
src/device/output/key/char.go
Normal file
@@ -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
|
||||
}
|
||||
21
src/device/output/key/char_test.go
Normal file
21
src/device/output/key/char_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
35
src/device/output/key/keys.go
Normal file
35
src/device/output/key/keys.go
Normal file
@@ -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)
|
||||
)
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user