使用golang在windows上設置全局快捷鍵的操作
hotkey熱鍵
1.需求
最近在工作中,總是重復的做事,想著自己設置一個快捷鍵實現(xiàn)windows 剪貼板的功能,網(wǎng)上找資料,用了一天時間實現(xiàn)熱鍵功能
2.使用包
golang.design/x/hotkey golang.design/x/clipboard
這兩都要魔法下載
3.開發(fā)文檔
github的star不太多,上面的文檔寫得也很少
clipboard

hotkey

4.具體實現(xiàn)
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+\\)")
}
// 執(zhí)行清理工作,例如關閉資源
// 在實際應用中,你可能需要在這里添加自定義的清理邏輯
// 模擬一些清理工作
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上面得回答,在加上自己得經(jīng)驗。我還結合fyne寫了一個小工具。等下面文章會給出地址??聪滦Ч?/p>
5.fyne結合hotkey 開發(fā)一個輔助工具

以上就是使用golang在windows上設置全局快捷鍵的操作的詳細內容,更多關于golang windows設置快捷鍵的資料請關注腳本之家其它相關文章!
相關文章
golang 交叉編譯C++ dll配置文件的實現(xiàn)
本文探討了在64位環(huán)境下調用32位C++ DLL的的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-07-07
golang jsoniter extension 處理動態(tài)字段的實現(xiàn)方法
這篇文章主要介紹了golang jsoniter extension 處理動態(tài)字段的實現(xiàn)方法,我們使用實例級別的 extension, 而非全局,可以針對不同業(yè)務邏輯有所區(qū)分,jsoniter 包提供了比較完善的定制能力,通過例子可以感受一下擴展性,需要的朋友可以參考下2023-04-04
Go語言strings.Repeat中5個高效用法(附性能對比)
本文深入探討Go語言中strings.Repeat的5個高效用法,包括CLI工具視覺增強、測試數(shù)據(jù)生成、文本對齊優(yōu)化、二進制協(xié)議處理及性能優(yōu)化場景,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2026-03-03
Go手寫數(shù)據(jù)庫ZiyiDB的實現(xiàn)
本文主要介紹了Go手寫數(shù)據(jù)庫ZiyiDB的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-05-05

