From 6c17327b42f436f0f03402a16bc1ac98f5fb8b5c Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Sat, 28 Jan 2017 12:07:34 +0700 Subject: [PATCH] Progress at getting SendKeys to send arrow keys --- examples/keys/main.go | 11 ++++++---- input.go | 50 ++++++++++++++++++++++++++++++++----------- testdata/visible.html | 6 ++++++ 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/examples/keys/main.go b/examples/keys/main.go index c761fed..5ed9307 100644 --- a/examples/keys/main.go +++ b/examples/keys/main.go @@ -23,8 +23,8 @@ func main() { } // run task list - var val1, val2, val3 string - err = c.Run(ctxt, sendkeys(&val1, &val2, &val3)) + var val1, val2, val3, val4 string + err = c.Run(ctxt, sendkeys(&val1, &val2, &val3, &val4)) if err != nil { log.Fatal(err) } @@ -44,18 +44,21 @@ func main() { log.Printf("#input1 value: %s", val1) log.Printf("#textarea1 value: %s", val2) log.Printf("#input2 value: %s", val3) + log.Printf("#select1 value: %s", val4) } -func sendkeys(val1, val2, val3 *string) cdp.Tasks { +func sendkeys(val1, val2, val3, val4 *string) cdp.Tasks { return cdp.Tasks{ cdp.Navigate("file:" + os.Getenv("GOPATH") + "/src/github.com/knq/chromedp/testdata/visible.html"), cdp.WaitVisible(`#input1`, cdp.ByID), cdp.WaitVisible(`#textarea1`, cdp.ByID), - cdp.SendKeys(`#textarea1`, "\b\b\n\naoeu\n\ntest1\n\nblah2\n\n\t\b\bother box!\t\ntest4", cdp.ByID), + cdp.SendKeys(`#textarea1`, "\b\b\n\naoeu\n\ntest1\n\nblah2\n\n\t\t\t\b\bother box!\t\ntest4", cdp.ByID), cdp.Value(`#input1`, val1, cdp.ByID), cdp.Value(`#textarea1`, val2, cdp.ByID), cdp.SetValue(`#input2`, "test3", cdp.ByID), cdp.Value(`#input2`, val3, cdp.ByID), + cdp.SendKeys(`#select1`, cdp.KeyCodeDown+cdp.KeyCodeDown, cdp.ByID), + cdp.Value(`#select1`, val4, cdp.ByID), cdp.Sleep(30 * time.Second), } } diff --git a/input.go b/input.go index 2dce765..583ff00 100644 --- a/input.go +++ b/input.go @@ -96,11 +96,36 @@ type KeyAction struct { opts []KeyOption } -var keyNames = map[rune]string{ - '\b': "Backspace", - '\t': "Tab", - '\r': "Enter", - '\n': "Enter", +// KeyCode are known system key codes. +type KeyCode string + +// KeyCode values. +const ( + KeyCodeBackspace = "\b" + KeyCodeTab = "\t" + KeyCodeCR = "\r" + KeyCodeLF = "\n" + KeyCodeLeft = "\x25" + KeyCodeUp = "\x26" + KeyCodeRight = "\x27" + KeyCodeDown = "\x28" +) + +const ( + keyRuneCR = '\r' +) + +// keyCodeNames is the map of key code values to their respective named +// identifiers. +var keyCodeNames = map[KeyCode]string{ + KeyCodeBackspace: "Backspace", + KeyCodeTab: "Tab", + KeyCodeCR: "Enter", + KeyCodeLF: "Enter", + KeyCodeLeft: "Left", + KeyCodeUp: "Up", + KeyCodeRight: "Right", + KeyCodeDown: "Down", } // Do satisfies Action interface. @@ -117,18 +142,19 @@ func (ka *KeyAction) Do(ctxt context.Context, h cdp.FrameHandler) error { for _, r := range ka.v { s := string(r) - if n, ok := keyNames[r]; ok { + keyS := KeyCode(r) + if n, ok := keyCodeNames[keyS]; ok { kc := int64(r) - if r == '\n' { - s = string('\r') - kc = int64('\r') + if keyS == KeyCodeLF { + s = string(keyRuneCR) + kc = int64(keyRuneCR) } err = sysP.WithKey(n). WithNativeVirtualKeyCode(kc). WithWindowsVirtualKeyCode(kc). - WithUnmodifiedText(s). - WithText(s). + WithKeyIdentifier(s). + WithIsSystemKey(true). Do(ctxt, h) if err != nil { return err @@ -141,7 +167,7 @@ func (ka *KeyAction) Do(ctxt context.Context, h cdp.FrameHandler) error { } // FIXME - time.Sleep(5 * time.Millisecond) + time.Sleep(100 * time.Millisecond) } return nil diff --git a/testdata/visible.html b/testdata/visible.html index 921311e..c05ec22 100644 --- a/testdata/visible.html +++ b/testdata/visible.html @@ -16,6 +16,12 @@



+