Ctrl+Shift+Alt+X 热键注册
This commit is contained in:
parent
b570d80058
commit
b351ea63e7
1
go.mod
1
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 (
|
||||
|
3
go.sum
3
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=
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
38
pkg/hotkey/hotkey.go
Normal file
38
pkg/hotkey/hotkey.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user