Compare commits

...

16 Commits

Author SHA1 Message Date
vcaesar
f049036b8c Update: update examples and README.md 2025-02-08 15:44:55 -08:00
mfkgef
0d69fe6a36
Fix Issues #48 (#50)
* fix KeyUp event bug

* Update go.mod

---------

Co-authored-by: Evans <vzvway@gmail.com>
2025-02-08 15:36:18 -08:00
vcaesar
b93f6ca661 Update: update CI and go mod 2024-08-26 16:42:12 -07:00
vcaesar
c94ab299da Update: bump CI to the newest 2024-02-23 08:57:29 -08:00
vcaesar
36f129375f Update: update gomod and bump CI 2023-08-18 10:31:18 -07:00
vcaesar
885d45d4bd Update: update godoc and clean go.yml 2023-03-02 17:37:06 -08:00
vcaesar
64c6b8bf80 Update: update CI to go1.20.x 2023-02-24 10:41:31 -08:00
vcaesar
78031794ca Add: add .github issue and PR template 2022-09-22 11:54:08 -07:00
vcaesar
762bfbdd9a Update: bump CI to 1.19 2022-09-08 12:50:44 -07:00
vcaesar
03b71aacdc Update CI to go1.18.x 2022-04-10 13:24:46 -07:00
vcaesar
c8fc1e36f4 Add more examples code 2022-02-09 09:26:46 -08:00
vcaesar
833856fd33 Move robotgo event code to event.go 2022-01-02 16:24:25 -04:00
vcaesar
1dd095bd1f Update README.md 2021-12-24 14:05:52 -04:00
vcaesar
3e0ac28943 Update keycode.go use the newest code 2021-11-23 16:19:46 -04:00
vcaesar
4c47f7354b Update README.md 2021-10-16 12:01:40 -04:00
vcaesar
384f5adf97 Update go mod and version 2021-09-26 15:09:05 -04:00
14 changed files with 410 additions and 131 deletions

28
.github/issue_template.md vendored Normal file
View File

@ -0,0 +1,28 @@
1. Please **speak English (English only)**, this is the language everybody of us can speak and write.
2. Please take a moment to **search** that an issue **doesn't already exist**.
3. Please make sure `Golang, GCC` is installed correctly before installing Gohook.
<!-- 4. Please ask questions or config/deploy problems on our Gitter channel: https://gitter.im/robotn/gohook -->
5. Please give all relevant information below for bug reports, incomplete details will be handled as an invalid report.
**You MUST delete the content above including this line before posting, otherwise your issue will be invalid.**
- Gohook version (or commit ref):
- Go version:
- Gcc version:
- Operating system and bit:
- Resolution:
- Can you reproduce the bug at [Examples](https://github.com/robotn/gohook/blob/master/examples/main.go):
- [ ] Yes (provide example code)
- [ ] No
- [ ] Not relevant
- Provide example code:
```Go
```
- Log gist:
## Description
...

25
.github/pull_request_template.md vendored Normal file
View File

@ -0,0 +1,25 @@
The pull request will be closed without any reasons if it does not satisfy any of following requirements:
1. Make sure you are targeting the `master` branch, pull requests on release branches are only allowed for bug fixes.
2. Add new features, please provide the reasons and test code.
3. Please read contributing guidelines: [CONTRIBUTING](https://github.com/robotn/gohook/blob/master/CONTRIBUTING.md)
4. Describe what your pull request does and which issue you're targeting (if any and **Please use English**)
5. ... if it is not related to any particular issues, explain why we should not reject your pull request.
6. The Commits must **use English**, must be test and No useless submissions.
**You MUST delete the content above including this line before posting, otherwise your pull request will be invalid.**
**Please provide Issues links to:**
- Issues: #1
**Provide test code:**
```Go
```
## Description
...

View File

@ -10,10 +10,10 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Set up Go 1.17
- name: Set up Go 1.23.0
uses: actions/setup-go@v1
with:
go-version: 1.17
go-version: 1.23.0
id: go
- name: Check out code into the Go module directory
@ -22,10 +22,6 @@ jobs:
- name: Get dependencies
run: |
go get -v -t -d ./...
# if [ -f Gopkg.toml ]; then
# curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
# dep ensure
# fi
- name: Build
run: go build -v .

View File

@ -7,6 +7,20 @@
[![GoDoc](https://godoc.org/github.com/robotn/gohook?status.svg)](https://godoc.org/github.com/robotn/gohook)
<!-- This is a work in progress. -->
## Requirements (Linux):
[Robotgo-requirements-event](https://github.com/go-vgo/robotgo#requirements)
## Install:
With Go module support (Go 1.11+), just import:
```go
import "github.com/robotn/gohook"
```
## Examples:
```Go
package main
@ -31,7 +45,11 @@ func add() {
fmt.Println("--- Please press w---")
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
fmt.Println("w")
fmt.Println("keyDown: ", "w")
})
hook.Register(hook.KeyUp, []string{"w"}, func(e hook.Event) {
fmt.Println("keyUp: ", "w")
})
s := hook.Start()
@ -39,10 +57,10 @@ func add() {
}
func low() {
EvChan := hook.Start()
evChan := hook.Start()
defer hook.End()
for ev := range EvChan {
for ev := range evChan {
fmt.Println("hook: ", ev)
}
}

View File

@ -34,7 +34,7 @@ environment:
PATH: C:\msys64\mingw32\bin\;C:\Program Files (x86)\NSIS\;%PATH%
# - COMPILER: MINGW_W64
# ARCHITECTURE: x64
GOVERSION: 1.17.1
GOVERSION: 1.23.0
# GOPATH: c:\gopath
# scripts that run after cloning repository

150
event.go Normal file
View File

@ -0,0 +1,150 @@
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/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 hook
import "strconv"
/*
___________ ____ _______ .__ __. .___________.
| ____\ \ / / | ____|| \ | | | |
| |__ \ \/ / | |__ | \| | `---| |----`
| __| \ / | __| | . ` | | |
| |____ \ / | |____ | |\ | | |
|_______| \__/ |_______||__| \__| |__|
*/
// AddEvent add event listener,
//
// parameters for the string type,
// the keyboard corresponding key parameters,
//
// mouse arguments: mleft, center, mright, wheelDown, wheelUp,
// wheelLeft, wheelRight.
//
// Use "hook.AddEvents()" or "gohook" add asynchronous event listener
func AddEvent(key string) bool {
var (
// cs *C.char
mArr = []string{"mleft", "center", "mright", "wheelDown",
"wheelUp", "wheelLeft", "wheelRight"}
mouseBool bool
)
for i := 0; i < len(mArr); i++ {
if key == mArr[i] {
mouseBool = true
}
}
if len(key) > 1 && !mouseBool {
key = strconv.Itoa(int(Keycode[key]))
}
geve := addEvent(key)
// defer C.free(unsafe.Pointer(cs))
return geve == 0
}
// AddEvents add global event hook
//
// hook.AddEvents("q")
// hook.AddEvents("q", "ctrl")
// hook.AddEvents("q", "ctrl", "shift")
func AddEvents(key string, arr ...string) bool {
s := Start()
// defer End()
ct := false
k := 0
for {
e := <-s
l := len(arr)
if l > 0 {
for i := 0; i < l; i++ {
ukey := Keycode[arr[i]]
if e.Kind == KeyHold && e.Keycode == ukey {
k++
}
if k == l {
ct = true
}
if e.Kind == KeyUp && e.Keycode == ukey {
if k > 0 {
k--
}
// time.Sleep(10 * time.Microsecond)
ct = false
}
}
} else {
ct = true
}
if ct && e.Kind == KeyUp && e.Keycode == Keycode[key] {
End()
// k = 0
break
}
}
return true
}
// AddMouse add mouse event hook
//
// mouse arguments: left, center, right, wheelDown, wheelUp,
// wheelLeft, wheelRight.
//
// hook.AddMouse("left")
// hook.AddMouse("left", 100, 100)
func AddMouse(btn string, x ...int16) bool {
s := Start()
ukey := MouseMap[btn]
ct := false
for {
e := <-s
if len(x) > 1 {
if e.Kind == MouseMove && e.X == x[0] && e.Y == x[1] {
ct = true
}
} else {
ct = true
}
if ct && e.Kind == MouseDown && e.Button == ukey {
End()
break
}
}
return true
}
// AddMousePos add listen mouse event pos hook
func AddMousePos(x, y int16) bool {
s := Start()
for {
e := <-s
if e.Kind == MouseMove && e.X == x && e.Y == y {
End()
break
}
}
return true
}

View File

@ -5,8 +5,7 @@
/* Python versions under 2.5 don't support this macro, but it's not
* terribly difficult to replicate: */
#ifndef PyModule_AddIntMacro
#define PyModule_AddIntMacro(module, macro) \
PyModule_AddIntConstant(module, #macro, macro)
#define PyModule_AddIntMacro(module, macro) PyModule_AddIntConstant(module, #macro, macro)
#endif /* PyModule_AddIntMacro */
#if !defined(IS_MACOSX) && defined(__APPLE__) && defined(__MACH__)

View File

@ -73,10 +73,8 @@ struct _MEvent {
typedef struct _MEvent MEvent;
// typedef MMBitmap *MMBitmapRef;
MEvent mEvent;
bool loggerProc(unsigned int level, const char *format, ...) {
bool status = false;

113
examples/event/main.go Normal file
View File

@ -0,0 +1,113 @@
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/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"
hook "github.com/robotn/gohook"
)
func addEvent() {
fmt.Println("--- Please press ctrl + shift + q ---")
ok := hook.AddEvents("q", "ctrl", "shift")
if ok {
fmt.Println("add events...")
}
fmt.Println("--- Please press w---")
ok = hook.AddEvents("w")
if ok {
fmt.Println("add events")
}
// start hook
s := hook.Start()
// end hook
defer hook.End()
for ev := range s {
fmt.Println("hook: ", ev)
}
}
func addMouse() {
fmt.Println("--- Please press left mouse button ---")
ok := hook.AddMouse("left")
if ok {
fmt.Println("add mouse...")
}
fmt.Println("--- Please press left mouse button and move mosue to 100,100 ---")
ok = hook.AddMouse("left", 100, 100)
if ok {
fmt.Println("add mouse and move to 100,100 ...")
}
fmt.Println("--- Please move mosue to 100,100 ---")
ok = hook.AddMousePos(100, 100)
if ok {
fmt.Println(" move mouse to 100,100 ...")
}
}
func add() {
fmt.Println("--- Please press v---")
eve := hook.AddEvent("v")
if eve {
fmt.Println("--- You press v---", "v")
}
fmt.Println("--- Please press k---")
keve := hook.AddEvent("k")
if keve {
fmt.Println("--- You press k---", "k")
}
fmt.Println("--- Please press f1---")
feve := hook.AddEvent("f1")
if feve {
fmt.Println("You press...", "f1")
}
}
func event() {
////////////////////////////////////////////////////////////////////////////////
// Global event listener
////////////////////////////////////////////////////////////////////////////////
add()
fmt.Println("--- Please press left mouse button---")
mleft := hook.AddEvent("mleft")
if mleft {
fmt.Println("--- You press left mouse button---", "mleft")
}
mright := hook.AddEvent("mright")
if mright {
fmt.Println("--- You press right mouse button---", "mright")
}
// stop AddEvent
// hook.StopEvent()
}
func main() {
fmt.Println("test begin...")
addEvent()
addMouse()
event()
}

View File

@ -6,6 +6,15 @@ import (
hook "github.com/robotn/gohook"
)
func main() {
registerEvent()
base()
add()
addMouse()
}
func registerEvent() {
fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
@ -15,7 +24,25 @@ func registerEvent() {
fmt.Println("--- Please press w ---")
hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
fmt.Println("w-")
fmt.Println("KeyDown: ", "w-")
})
hook.Register(hook.KeyUp, []string{"w"}, func(e hook.Event) {
fmt.Println("KeyUp: ", "w")
})
s := hook.Start()
<-hook.Process(s)
}
func addMouse() {
fmt.Println("--- Please press left mouse button to see it's position and the right mouse button to exit ---")
hook.Register(hook.MouseDown, []string{}, func(e hook.Event) {
if e.Button == hook.MouseMap["left"] {
fmt.Printf("mouse left @ %v - %v\n", e.X, e.Y)
} else if e.Button == hook.MouseMap["right"] {
hook.End()
}
})
s := hook.Start()
@ -50,13 +77,8 @@ func base() {
for ev := range evChan {
fmt.Println("hook: ", ev)
if ev.Keychar == 'q' {
break
}
}
}
func main() {
registerEvent()
base()
add()
}

7
go.mod
View File

@ -1,5 +1,8 @@
module github.com/robotn/gohook
go 1.13
go 1.17
require github.com/vcaesar/tt v0.11.0
require (
github.com/vcaesar/keycode v0.10.1
github.com/vcaesar/tt v0.20.1
)

6
go.sum
View File

@ -1,2 +1,4 @@
github.com/vcaesar/tt v0.11.0 h1:obQecjgbnAxxC6OYGY6yDvhGRW2PR5wD8Ma2uJH3WGA=
github.com/vcaesar/tt v0.11.0/go.mod h1:GHPxQYhn+7OgKakRusH7KJ0M5MhywoeLb8Fcffs/Gtg=
github.com/vcaesar/keycode v0.10.1 h1:0DesGmMAPWpYTCYddOFiCMKCDKgNnwiQa2QXindVUHw=
github.com/vcaesar/keycode v0.10.1/go.mod h1:JNlY7xbKsh+LAGfY2j4M3znVrGEm5W1R8s/Uv6BJcfQ=
github.com/vcaesar/tt v0.20.1 h1:D/jUeeVCNbq3ad8M7hhtB3J9x5RZ6I1n1eZ0BJp7M+4=
github.com/vcaesar/tt v0.20.1/go.mod h1:cH2+AwGAJm19Wa6xvEa+0r+sXDJBT0QgNQey6mwqLeU=

27
hook.go
View File

@ -32,7 +32,7 @@ import (
const (
// Version get the gohook version
Version = "v0.30.4.100, Sierra Nevada!"
Version = "v0.40.0.123, Sierra Nevada!"
// HookEnabled honk enable status
HookEnabled = 1 // iota
@ -92,10 +92,12 @@ var (
lck sync.RWMutex
pressed = make(map[uint16]bool, 256)
used = []int{}
pressed = make(map[uint16]bool, 256)
uppressed = make(map[uint16]bool, 256)
used = []int{}
keys = map[int][]uint16{}
upkeys = map[int][]uint16{}
cbs = map[int]func(Event){}
events = map[uint8][]int{}
)
@ -116,12 +118,17 @@ func Register(when uint8, cmds []string, cb func(Event)) {
key := len(used)
used = append(used, key)
tmp := []uint16{}
uptmp := []uint16{}
for _, v := range cmds {
if when == KeyUp {
uptmp = append(uptmp, Keycode[v])
}
tmp = append(tmp, Keycode[v])
}
keys[key] = tmp
upkeys[key] = uptmp
cbs[key] = cb
events[when] = append(events[when], key)
// return
@ -134,6 +141,7 @@ func Process(evChan <-chan Event) (out chan bool) {
for ev := range evChan {
if ev.Kind == KeyDown || ev.Kind == KeyHold {
pressed[ev.Keycode] = true
uppressed[ev.Keycode] = true
} else if ev.Kind == KeyUp {
pressed[ev.Keycode] = false
}
@ -145,6 +153,12 @@ func Process(evChan <-chan Event) (out chan bool) {
if allPressed(pressed, keys[v]...) {
cbs[v](ev)
} else if ev.Kind == KeyUp {
//uppressed[ev.Keycode] = true
if allPressed(uppressed, upkeys[v]...) {
uppressed = make(map[uint16]bool, 256)
cbs[v](ev)
}
}
}
}
@ -253,6 +267,7 @@ func End() {
close(ev)
pressed = make(map[uint16]bool, 256)
uppressed = make(map[uint16]bool, 256)
used = []int{}
keys = map[int][]uint16{}
@ -260,8 +275,8 @@ func End() {
events = map[uint8][]int{}
}
// AddEvent add event listener
func AddEvent(key string) int {
// AddEvent add the block event listener
func addEvent(key string) int {
cs := C.CString(key)
defer C.free(unsafe.Pointer(cs))
@ -271,7 +286,7 @@ func AddEvent(key string) int {
return geve
}
// StopEvent stop event listener
// StopEvent stop the block event listener
func StopEvent() {
C.stop_event()
}

View File

@ -10,103 +10,13 @@
package hook
type uMap map[string]uint16
import "github.com/vcaesar/keycode"
// MouseMap robotgo hook mouse's code map
var MouseMap = uMap{
"left": 1,
"right": 2,
"center": 3,
"wheelDown": 4,
"wheelUp": 5,
"wheelLeft": 6,
"wheelRight": 7,
}
// MouseMap defines the robotgo hook mouse's code map
var MouseMap = keycode.MouseMap
// Keycode robotgo hook key's code map
var Keycode = uMap{
"`": 41,
"1": 2,
"2": 3,
"3": 4,
"4": 5,
"5": 6,
"6": 7,
"7": 8,
"8": 9,
"9": 10,
"0": 11,
"-": 12,
"+": 13,
//
"q": 16,
"w": 17,
"e": 18,
"r": 19,
"t": 20,
"y": 21,
"u": 22,
"i": 23,
"o": 24,
"p": 25,
"[": 26,
"]": 27,
"\\": 43,
//
"a": 30,
"s": 31,
"d": 32,
"f": 33,
"g": 34,
"h": 35,
"j": 36,
"k": 37,
"l": 38,
";": 39,
"'": 40,
//
"z": 44,
"x": 45,
"c": 46,
"v": 47,
"b": 48,
"n": 49,
"m": 50,
",": 51,
".": 52,
"/": 53,
//
"f1": 59,
"f2": 60,
"f3": 61,
"f4": 62,
"f5": 63,
"f6": 64,
"f7": 65,
"f8": 66,
"f9": 67,
"f10": 68,
"f11": 69,
"f12": 70,
// more
"esc": 1,
"delete": 14,
"tab": 15,
"ctrl": 29,
"control": 29,
"alt": 56,
"space": 57,
"shift": 42,
"rshift": 54,
"enter": 28,
//
"cmd": 3675,
"command": 3675,
"rcmd": 3676,
"ralt": 3640,
//
"up": 57416,
"down": 57424,
"left": 57419,
"right": 57421,
}
// Keycode defines the robotgo hook key's code map
var Keycode = keycode.Keycode
// Special defines the special key map
var Special = keycode.Special