robotn / gohook

GoHook, Go global keyboard and mouse listener hook
MIT License
324 stars 43 forks source link

Example in README is wrong. #22

Closed pinpox closed 2 years ago

pinpox commented 4 years ago

Hi, just a quick hint: The example in the README.md does not work.

The import should probably be:

    hook "github.com/robotn/gohook"

instead of just:

     "github.com/robotn/gohook"

EDIT: I just saw there is a examples/main.go file, with the correct code, but I can't get it to work as expected. Could you help me out? I modified it like this (just to add more visible output):

package main

import (
    "fmt"

    hook "github.com/robotn/gohook"
)

func addEvent() {
    fmt.Println("--- Please press ctrl + shift + q to stop hook ---")
    hook.Register(hook.KeyDown, []string{"q", "ctrl", "shift"}, func(e hook.Event) {
        fmt.Println("ctrl-shift-q")
        hook.End()
    })

    fmt.Println("--- Please press w---")
    hook.Register(hook.KeyDown, []string{"w"}, func(e hook.Event) {
        fmt.Println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww")
    })

    s := hook.Start()
    <-hook.Process(s)
}

// hook listen and return values using detailed examples
func add() {
    fmt.Println("hook add...")
    s := hook.Start()
    defer hook.End()

    ct := false
    for {
        i := <-s

        if i.Kind == hook.KeyHold && i.Rawcode == 59 {
            ct = true
        }

        if ct && i.Rawcode == 12 {
            break
        }
    }
}

// base hook example
func base() {
    fmt.Println("hook start...")
    evChan := hook.Start()
    defer hook.End()

    for ev := range evChan {
        fmt.Println("hook: ", ev)
    }
}

func main() {
    addEvent()

    base()

    add()
}

The application runs, but whatever I do or press, I can't manage to print out the wwwwwwwwwwwwwwwwwwwwwwww string. What is supposed to happen? Also, ctrl+shift+q isn't doing anything either. Here is my output as I press w a few times, then random other keys and lastly ctrl+shift+q and ctrl+c to quit: asciicast

cauefcr commented 4 years ago

Thanks!

On Sat, Sep 5, 2020, 7:09 PM Pablo Ovelleiro Corral < notifications@github.com> wrote:

Hi, just a quick hint: The example in the REAME.md does not work.

The import should probably be:

hook "github.com/robotn/gohook"

instead of just:

"github.com/robotn/gohook"

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/robotn/gohook/issues/22, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACY42HOOV63SJEJJWOV4HLLSEI3U5ANCNFSM4Q24XGYQ .

vcaesar commented 2 years ago

Closed because of new version and time long.