欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用golang在windows上設(shè)置全局快捷鍵的操作

 更新時(shí)間:2024年02月01日 09:30:58   作者:Aisre  
最近在工作中,總是重復(fù)的做事,想著自己設(shè)置一個(gè)快捷鍵實(shí)現(xiàn)windows 剪貼板的功能,所以本文小編給大家分享了使用golang在windows上設(shè)置全局快捷鍵的操作,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下

hotkey熱鍵

1.需求

最近在工作中,總是重復(fù)的做事,想著自己設(shè)置一個(gè)快捷鍵實(shí)現(xiàn)windows 剪貼板的功能,網(wǎng)上找資料,用了一天時(shí)間實(shí)現(xiàn)熱鍵功能

2.使用包

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

這兩都要魔法下載

3.開發(fā)文檔

github的star不太多,上面的文檔寫得也很少

clipboard

在這里插入圖片描述

hotkey

在這里插入圖片描述

4.具體實(shí)現(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)報(bào)錯(cuò)問題的解釋:")))

		}
	}()
	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)

	// 注冊(cè)多個(gè)信號(hào)
	signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)

	// 啟動(dòng) goroutine 處理信號(hào)
	go func() {
		for {
			// 等待信號(hào)
			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í)行清理工作,例如關(guān)閉資源
			// 在實(shí)際應(yīng)用中,你可能需要在這里添加自定義的清理邏輯

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

			// 退出程序
			os.Exit(0)
		}
	}()
	for {
		// 在實(shí)際應(yīng)用中,這里可能是程序的主要工作邏輯
		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 {
	// 使用正則表達(dá)式去除字符串兩邊的``
	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
}

總結(jié)就是要多看issues上面得回答,在加上自己得經(jīng)驗(yàn)。我還結(jié)合fyne寫了一個(gè)小工具。等下面文章會(huì)給出地址??聪滦Ч?/p>

5.fyne結(jié)合hotkey 開發(fā)一個(gè)輔助工具

在這里插入圖片描述

以上就是使用golang在windows上設(shè)置全局快捷鍵的操作的詳細(xì)內(nèi)容,更多關(guān)于golang windows設(shè)置快捷鍵的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論