Merge pull request #269 from go-vgo/dev

add gohook modern and concurrent API
This commit is contained in:
vz 2020-05-19 11:57:24 -04:00 committed by GitHub
commit baf3c322fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 4 deletions

72
examples/gohook/main.go Normal file
View File

@ -0,0 +1,72 @@
// Copyright 2016 The cauefcr Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/cauefcr/robotgo/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
package main
import (
"fmt"
"github.com/go-vgo/robotgo"
hook "github.com/robotn/gohook"
)
func addEvent() {
fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
robotgo.EventHook(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
fmt.Println("ctrl-shift-q")
robotgo.EventEnd()
})
fmt.Println("--- Please press w---")
robotgo.EventHook(hook.KeyDown, []string{"w"}, func(e hook.Event) {
fmt.Println("w")
})
s := robotgo.EventStart()
<-robotgo.EventProcess(s)
}
func addMouse() {
fmt.Println("--- Please press left mouse button to see it's position and the right mouse button to exit ---")
robotgo.EventHook(hook.MouseDown, []string{}, func(e hook.Event) {
if e.Button == hook.MouseMap["right"] {
fmt.Printf("mleft @ %v - %v\n", e.X, e.Y)
} else if e.Button == hook.MouseMap["left"] {
robotgo.EventEnd()
}
})
s := robotgo.EventStart()
<-robotgo.EventProcess(s)
}
func lowLevel() {
////////////////////////////////////////////////////////////////////////////////
// Global event listener
////////////////////////////////////////////////////////////////////////////////
fmt.Println("Press q to stop event gathering")
evChan := robotgo.EventStart()
for e := range evChan {
fmt.Println(e)
if e.Keychar == 'q' {
robotgo.EventEnd()
// break
}
}
}
func main() {
fmt.Println("test begin...")
addEvent()
addMouse()
lowLevel()
}

4
go.mod
View File

@ -5,12 +5,12 @@ require (
github.com/lxn/win v0.0.0-20191128105842-2da648fda5b4
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776 // indirect
github.com/otiai10/gosseract v2.2.1+incompatible
github.com/robotn/gohook v0.0.0-20200224154940-caaa106d8037
github.com/robotn/gohook v0.30.0
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934
github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770
github.com/vcaesar/gops v0.0.0-20200120141635-c9dda03b64e2
github.com/vcaesar/imgo v0.0.0-20191008162304-a83ea7753bc8
github.com/vcaesar/tt v0.0.0-20200219144246-a190abe69d64
github.com/vcaesar/tt v0.10.0
golang.org/x/image v0.0.0-20200119044424-58c23975cae1
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/sys v0.0.0-20200120150407-c1edf535530b // indirect

6
go.sum
View File

@ -22,8 +22,8 @@ github.com/otiai10/mint v1.2.4 h1:DxYL0itZyPaR5Z9HILdxSoHx+gNs6Yx+neOGS3IVUk0=
github.com/otiai10/mint v1.2.4/go.mod h1:d+b7n/0R3tdyUYYylALXpWQ/kTN+QobSq/4SRGBkR3M=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robotn/gohook v0.0.0-20200224154940-caaa106d8037 h1:D2n1BKKjTvtL12pP7Yy35keMRrP8tJ9FNihbowffy5I=
github.com/robotn/gohook v0.0.0-20200224154940-caaa106d8037/go.mod h1:VUHyXb+4yA0DJQs+jaC2jWs+J7s6StlztBgwDkcEZT4=
github.com/robotn/gohook v0.30.0 h1:mVbw0zJee9W6vjnd8HuzzBD2rYS5fxRc5Aer9cT3oTI=
github.com/robotn/gohook v0.30.0/go.mod h1:VUHyXb+4yA0DJQs+jaC2jWs+J7s6StlztBgwDkcEZT4=
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934 h1:2lhSR8N3T6I30q096DT7/5AKEIcf1vvnnWAmS0wfnNY=
github.com/robotn/xgb v0.0.0-20190912153532-2cb92d044934/go.mod h1:SxQhJskUJ4rleVU44YvnrdvxQr0tKy5SRSigBrCgyyQ=
github.com/robotn/xgbutil v0.0.0-20190912154524-c861d6f87770 h1:2uX8QRLkkxn2EpAQ6I3KhA79BkdRZfvugJUzJadiJwk=
@ -41,6 +41,8 @@ github.com/vcaesar/imgo v0.0.0-20191008162304-a83ea7753bc8 h1:9Y+hoKBYa+UtzGqkOD
github.com/vcaesar/imgo v0.0.0-20191008162304-a83ea7753bc8/go.mod h1:52+3yYrTNjWKh+CkQozNRCLWCE/X666yAWPGbYC3DZI=
github.com/vcaesar/tt v0.0.0-20200219144246-a190abe69d64 h1:MwxT8hyvPcPafJ2LDmoFuH7Xpz5LZ6W3dKzwVQAHD/k=
github.com/vcaesar/tt v0.0.0-20200219144246-a190abe69d64/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
github.com/vcaesar/tt v0.10.0 h1:jPtFiRRnCOXVakd5ujCed2dwWwTs91wh8W1iHeUIfc0=
github.com/vcaesar/tt v0.10.0/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a h1:gHevYm0pO4QUbwy8Dmdr01R5r1BuKtfYqRqF0h/Cbh0=
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=

22
hook.go
View File

@ -25,6 +25,28 @@ import (
|_______| \__/ |_______||__| \__| |__|
*/
// EventStart start global event hook
// return event channel
func EventStart() chan hook.Event {
return hook.Start()
}
// EventEnd removes global event hook
func EventEnd() {
hook.End()
}
// EventProcess return go hook process
func EventProcess(Events chan hook.Event) chan bool {
return hook.Process(Events)
}
// EventHook register gohook event
func EventHook(When uint8, keysPressed []string, Callback func(hook.Event)) {
hook.Register(When, keysPressed, Callback)
return
}
// AddEvent add event listener,
//
// parameters for the string type,