From b351ea63e716ef2f46e5d48df4dd99cc41322b7c Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Sun, 12 May 2024 17:57:57 +0800 Subject: [PATCH] =?UTF-8?q?Ctrl+Shift+Alt+X=20=E7=83=AD=E9=94=AE=E6=B3=A8?= =?UTF-8?q?=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 3 +++ pkg/core/app.go | 13 +++++++++++++ pkg/hotkey/hotkey.go | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 pkg/hotkey/hotkey.go diff --git a/go.mod b/go.mod index 49895fe..897f9f9 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/wailsapp/wails/v2 v2.8.0 go.uber.org/atomic v1.6.0 go.uber.org/zap v1.13.0 + golang.design/x/hotkey v0.4.1 ) require ( diff --git a/go.sum b/go.sum index ce3d91f..2af54a3 100644 --- a/go.sum +++ b/go.sum @@ -178,6 +178,9 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEa go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= go.uber.org/zap v1.13.0 h1:nR6NoDBgAf67s68NhaXbsojM+2gxp3S1hWkHDl27pVU= go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= +golang.design/x/hotkey v0.4.1 h1:zLP/2Pztl4WjyxURdW84GoZ5LUrr6hr69CzJFJ5U1go= +golang.design/x/hotkey v0.4.1/go.mod h1:M8SGcwFYHnKRa83FpTFQoZvPO5vVT+kWPztFqTQKmXA= +golang.design/x/mainthread v0.3.0 h1:UwFus0lcPodNpMOGoQMe87jSFwbSsEY//CA7yVmu4j8= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= diff --git a/pkg/core/app.go b/pkg/core/app.go index d36d8a9..82f15aa 100644 --- a/pkg/core/app.go +++ b/pkg/core/app.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/wailsapp/wails/v2/pkg/runtime" "go.uber.org/zap/zapcore" + "golang.design/x/hotkey" "net" "net/http" "os" @@ -16,10 +17,14 @@ import ( "skapp/pkg/pid" server "skapp/pkg/server" "time" + + hk "skapp/pkg/hotkey" ) var pidFile = "./.pid" +var hotKeys []*hotkey.Hotkey + // App struct type App struct { ctx context.Context @@ -83,6 +88,10 @@ func (a *App) Startup(ctx context.Context) { a.ctx = ctx + hotKeys = append(hotKeys, hk.Register([]hotkey.Modifier{hotkey.ModShift, hotkey.ModCtrl, hotkey.ModAlt}, hotkey.KeyX, func() { + logger.Log.Info("热键触发: Ctrl+Shift+Alt+X") + }, nil)) + go func() { logger.Log.Infof("启动本地后台服务: %s", a.srv.Addr) // 服务连接 @@ -116,6 +125,10 @@ func (a *App) DomReady(ctx context.Context) { } func (a *App) BeforeClose(ctx context.Context) bool { + for _, k := range hotKeys { + k.Unregister() + } + return false } diff --git a/pkg/hotkey/hotkey.go b/pkg/hotkey/hotkey.go new file mode 100644 index 0000000..2c21906 --- /dev/null +++ b/pkg/hotkey/hotkey.go @@ -0,0 +1,38 @@ +package hotkey + +import ( + "golang.design/x/hotkey" + "log" +) + +func Register(mod []hotkey.Modifier, key hotkey.Key, keyDown func(), keyUp func()) *hotkey.Hotkey { + hk := hotkey.New(mod, key) + go func() { + defer func() { + hk.Unregister() + log.Printf("hotkey: %v is unregistered\n", hk) + }() + err := hk.Register() + if err != nil { + log.Printf("hotkey: failed to register hotkey: %v", err) + } + + log.Printf("hotkey: %v is registered\n", hk) + + for { + select { + case <-hk.Keydown(): + log.Printf("hotkey: %v is down\n", hk) + if keyDown != nil { + keyDown() + } + case <-hk.Keyup(): + log.Printf("hotkey: %v is up\n", hk) + if keyUp != nil { + keyUp() + } + } + } + }() + return hk +}