Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotkey shortcut monitoring will hijack information #26

Open
Esword618 opened this issue Aug 8, 2023 · 0 comments
Open

Hotkey shortcut monitoring will hijack information #26

Esword618 opened this issue Aug 8, 2023 · 0 comments

Comments

@Esword618
Copy link

Hotkey shortcut monitoring will hijack the information of the shortcut key. When I run the following code, I copy a piece of text and paste it in the browser's search box. The information is hijacked, and it can only be paused when I end the program.

package main

import (
	"golang.design/x/clipboard"
	"log"
	"sync"

	"golang.design/x/hotkey"
	"golang.design/x/hotkey/mainthread"
)

func main() {
	mainthread.Init(fn)
}

func fn() {
	err := clipboard.Init()
	if err != nil {
		panic(err)
	}
	wg := sync.WaitGroup{}
	wg.Add(2)
	go func() {
		defer wg.Done()
		for {
			err := listenHotkey(hotkey.KeyC, hotkey.ModCtrl)
			if err != nil {
				log.Println(err)
			}
		}
	}()
	go func() {
		defer wg.Done()
		for {
			err := listenHotkey(hotkey.KeyV, hotkey.ModCtrl)
			if err != nil {
				log.Println(err)
			}
			//imageData := clipboard.Read(clipboard.FmtImage)
			//base64String := base64.StdEncoding.EncodeToString(imageData)
			//log.Println(base64String)
			textData := clipboard.Read(clipboard.FmtText)
			log.Println(string(textData))
		}
	}()
	wg.Wait()
}

func listenHotkey(key hotkey.Key, mods ...hotkey.Modifier) (err error) {
	var ms []hotkey.Modifier
	ms = append(ms, mods...)
	hk := hotkey.New(ms, key)

	err = hk.Register()
	if err != nil {
		return
	}

	<-hk.Keydown()
	log.Printf("hotkey: %v is down\n", hk)
	<-hk.Keyup()
	log.Printf("hotkey: %v is up\n", hk)
	hk.Unregister()
	return
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant