使用golang在windows上设置全局快捷键的操作

2024-03-01 0 232
目录
  • hotkey热键
    • 1.需求
    • 2.使用包
    • 3.开发文档
    • 4.具体实现
    • 5.fyne结合hotkey 开发一个辅助工具

hotkey热键

1.需求

最近在工作中,总是重复的做事,想着自己设置一个快捷键实现windows 剪贴板的功能,网上找资料,用了一天时间实现热键功能

2.使用包

golang.design/x/hotkey
golang.design/x/clipboard

这两都要魔法下载

3.开发文档

github的star不太多,上面的文档写得也很少

clipboard

使用golang在windows上设置全局快捷键的操作

hotkey

使用golang在windows上设置全局快捷键的操作

4.具体实现

package main

import (
\”fmt\”
\”log\”
\”os\”
\”os/signal\”
\”regexp\”
\”strings\”
\”syscall\”
\”time\”

\”golang.design/x/clipboard\”

\”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() {

for {
err := listenHotkey(hotkey.KeyQ, hotkey.ModShift)
if err != nil {
log.Println(err)
}
textData := clipboard.Read(clipboard.FmtText)
//log.Println(string(textData))
_ = clipboard.Write(clipboard.FmtText, []byte(processString(string(textData))))

}
}()
go func() {

for {
err := listenHotkey(hotkey.KeyW, hotkey.ModShift)
if err != nil {
log.Println(err)
}
//textData := clipboard.Read(clipboard.FmtText)
//log.Println(string(textData))
_ = clipboard.Write(clipboard.FmtText, []byte(processString(\”<font color=red>**(核心步骤)**</font>\”)))

}
}()
go func() {

for {
err := listenHotkey(hotkey.KeyE, hotkey.ModShift)
if err != nil {
log.Println(err)
}
//textData := clipboard.Read(clipboard.FmtText)
//log.Println(string(textData))
_ = clipboard.Write(clipboard.FmtText, []byte(processString(\”<font color=red>**(核心步骤)**</font>(1)报错问题的解释:\”)))

}
}()
go func() {

for {
err := listenHotkey(hotkey.KeyR, hotkey.ModShift)
if err != nil {
log.Println(err)
}
//textData := clipboard.Read(clipboard.FmtText)
//log.Println(string(textData))
_ = clipboard.Write(clipboard.FmtText, []byte(processString(\”<font color=red>**(核心步骤)**</font>(2)问题的解决方法:\”)))

}
}()

sigChan := make(chan os.Signal, 1)

// 注册多个信号
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)

// 启动 goroutine 处理信号
go func() {
for {
// 等待信号
sig := <-sigChan

switch sig {
case os.Interrupt:
fmt.Println(\”Received interrupt signal (Ctrl+C)\”)
case syscall.SIGTERM:
fmt.Println(\”Received termination signal\”)
case syscall.SIGQUIT:
fmt.Println(\”Received quit signal (Ctrl+\\\\)\”)
}

// 执行清理工作,例如关闭资源
// 在实际应用中,你可能需要在这里添加自定义的清理逻辑

// 模拟一些清理工作
time.Sleep(2 * time.Second)

// 退出程序
os.Exit(0)
}
}()
for {
// 在实际应用中,这里可能是程序的主要工作逻辑
time.Sleep(1 * time.Second)
}
}

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
}

func processString(input string) string {
// 使用正则表达式去除字符串两边的“
re := regexp.MustCompile(\”`(.*)`\”)
matches := re.FindStringSubmatch(input)
if len(matches) > 1 {
input = matches[1]
}

// 去除两边空格
input = strings.TrimSpace(input)

// 替换 <font color=red>**{}**</font>
replacedContent := strings.ReplaceAll(\”<font color=red>**{}**</font>\”, \”{}\”, input)
//input = strings.ReplaceAll(, \”<font color=red>**{}**</font>\”, \”replacement_text\”)

return replacedContent
}

总结就是要多看issues上面得回答,在加上自己得经验。我还结合fyne写了一个小工具。等下面文章会给出地址。看下效果吧

5.fyne结合hotkey 开发一个辅助工具

使用golang在windows上设置全局快捷键的操作

以上就是使用golang在windows上设置全局快捷键的操作的详细内容,更多关于golang windows设置快捷键的资料请关注悠久资源网其它相关文章!

您可能感兴趣的文章:

  • windows下使用vscode搭建golang环境并调试的过程
  • Golang如何调用windows下的dll动态库中的函数
  • golang 在windows中设置环境变量的操作
  • Golang在Mac、Linux、Windows下如何交叉编译的实现
  • mac下golang安装了windows编译环境后编译变慢

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Golang 使用golang在windows上设置全局快捷键的操作 https://www.u-9.cn/jiaoben/golang/179593.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务