当前位置: 首页 > wzjs >正文

哈尔滨网站搜索优化公司杭州seo网络公司

哈尔滨网站搜索优化公司,杭州seo网络公司,温州最好的seo,济南网站建设询问企优互联价低在本文中,我们将练习如何使用Golang编写一个简单的Windows系统空闲时间监测工具。该工具能够检测系统的空闲时间,并在达到一定阈值时计数。 功能概述 监控鼠标和键盘的空闲事件,每空闲超过50s,触发次数加一。 该工具具有以下功…

 在本文中,我们将练习如何使用Golang编写一个简单的Windows系统空闲时间监测工具。该工具能够检测系统的空闲时间,并在达到一定阈值时计数。

功能概述

监控鼠标和键盘的空闲事件,每空闲超过50s,触发次数加一。

该工具具有以下功能:

  • 监测系统空闲时间。
  • 当系统空闲时间超过设定的阈值时,触发计数。
  • 实时显示当前系统触发次数。

代码实现

下载包

go get golang.org/x/sys/windows
go get github.com/getlantern/systray

以下是实现该工具的完整代码:

package mainimport ("fmt""time""unsafe""github.com/getlantern/systray""golang.org/x/sys/windows"
)// 配置参数
const (idleThreshold = 40 * time.Second
)var (user32           = windows.NewLazySystemDLL("user32.dll")kernel32         = windows.NewLazySystemDLL("kernel32.dll")getLastInputInfo = user32.NewProc("GetLastInputInfo")getTickCount     = kernel32.NewProc("GetTickCount")counter      uinttriggerTimes []stringlastActive   time.Time
)type LASTINPUTINFO struct {CbSize uint32Dtime  uint32
}func main() {systray.Run(onReady, onExit)
}func onReady() {// 设置托盘图标和菜单systray.SetIcon(IconData)systray.SetTitle("空闲监测")systray.SetTooltip("空闲监测运行中...")mHistory := systray.AddMenuItem("查看历史", "显示触发记录")mReset := systray.AddMenuItem("重置计数", "重置统计信息")mQuit := systray.AddMenuItem("退出", "退出程序")// 初始化记录lastActive = time.Now().Add(-getSystemIdleTime())updateTooltip()// 启动监测协程go monitorLoop()// 处理菜单点击go func() {for {select {case <-mHistory.ClickedCh:showHistory()case <-mReset.ClickedCh:resetCounter()case <-mQuit.ClickedCh:systray.Quit()return}}}()
}func monitorLoop() {for {idle := getSystemIdleTime()updateCounter(idle)updateTooltip()time.Sleep(time.Second)}
}func updateCounter(idle time.Duration) {if idle < idleThreshold {actualIdle := time.Since(lastActive)if actualIdle >= idleThreshold {counter++triggerTimes = append(triggerTimes, time.Now().Format("2006-01-02 15:04:05"))}lastActive = time.Now().Add(-idle)}
}func resetCounter() {counter = 0triggerTimes = nilupdateTooltip()
}func updateTooltip() {tooltip := fmt.Sprintf("空闲监测\n触发次数: %d\n最后触发: %s",counter,lastTriggerTime())systray.SetTooltip(tooltip)
}func lastTriggerTime() string {if len(triggerTimes) == 0 {return "从未触发"}return triggerTimes[len(triggerTimes)-1]
}func showHistory() {if len(triggerTimes) == 0 {showMessage("触发历史", "暂无触发记录")return}content := "触发时间记录:\n"for _, t := range triggerTimes {content += t + "\n"}showMessage("触发历史", content)
}func showMessage(title, message string) {windows.MessageBox(0,strPtr(message),strPtr(title),windows.MB_OK|windows.MB_ICONINFORMATION)
}func strPtr(s string) *uint16 {p, _ := windows.UTF16PtrFromString(s)return p
}func getSystemIdleTime() time.Duration {info := &LASTINPUTINFO{CbSize: uint32(unsafe.Sizeof(LASTINPUTINFO{}))}ret, _, _ := getLastInputInfo.Call(uintptr(unsafe.Pointer(info)))if ret == 0 {return 0}tick, _, _ := getTickCount.Call()lastInput := uint32(tick) - info.Dtimereturn time.Duration(lastInput) * time.Millisecond
}func onExit() {// 退出时的清理工作fmt.Print("\033[?25h") // 恢复光标显示(如果之前隐藏了)
}// 图标数据(需用2goarray生成)
var IconData []byte = []byte{// 这里填入实际的图标字节数据
}

go.mod

module idle-monitorgo 1.23.0toolchain go1.23.7require (github.com/getlantern/systray v1.2.2golang.org/x/sys v0.31.0
)require (github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirectgithub.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 // indirectgithub.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7 // indirectgithub.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 // indirectgithub.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 // indirectgithub.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f // indirectgithub.com/go-stack/stack v1.8.0 // indirectgithub.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
)

需要使用 2goarray 生成图标的 bytes 变量并替换掉 IconData 变量,图标使用64×64 ico格式

下载后运行

go run 2goarray.go MyArray mypackage < myimage.ico > myimage.go

代码解析

  1. 配置参数:定义了空闲触发阈值和刷新间隔。
  2. 数据结构:定义了LASTINPUTINFO结构体,用于获取系统最后输入时间。
  3. 全局变量:定义了所需的全局变量,如用户32和内核32库的引用、触发次数等。
  4. 主函数:初始化显示、记录活动时间,并在循环中更新计数器和显示状态。
  5. 辅助函数:包括更新计数器、显示状态、初始化显示、启用虚拟终端处理和获取系统空闲时间。
    通过这个简单的工具,我们可以实时监测Windows系统的空闲时间,并在达到设定阈值时进行计数。

打包为exe

go build -ldflags="-H windowsgui" -o idle-monitor.exe

运行exe,右下角会出现相应图标
在这里插入图片描述
鼠标悬浮显示空闲段触发次数
在这里插入图片描述
在这里插入图片描述

右键查看历史会显示历史空闲
在这里插入图片描述
在这里插入图片描述

http://www.dtcms.com/wzjs/34471.html

相关文章:

  • 珠海市网站建设怎么样口碑营销的前提及好处有哪些
  • 优化网络速度seo顾问是什么职业
  • 如何访问自己做的网站seo优化范畴
  • 厦门网站建设方案优化微信搜一搜怎么做推广
  • 天河建设网站专家网络优化的三个方法
  • 做平面资源比较好的网站深圳网络营销推广专员
  • 班级网站源码网站排名怎么做上去
  • 最新网站开发建设教材一键清理加速
  • 南宁市网站百度网盘seo优化
  • 域名解析要登入哪个网站做搜索关键词排名推广
  • 专做外贸库存的网站客服系统网页源码2022免费
  • 河北三河建设局网站seo研究中心官网
  • wordpress slide imageseo优化软件免费
  • 天津 网站制作网络营销成功案例ppt免费
  • 做泵阀生意到哪个网站长春seo公司
  • 选择做华为网站的目的和意义查询网 域名查询
  • 插画设计网站推荐百度移动端排名
  • 免费ppt模板软件seo公司推荐
  • 网站设计做哪些准备口碑营销什么意思
  • 成都市建设委员会网站互联网app推广具体怎么做
  • 宁波seo网站建设费用小说推广关键词怎么弄
  • 求一个免费的企业邮箱网站建设优化推广系统
  • 个体户可以备案网站吗seo案例分析及解析
  • 为网站做外链的文章微博推广方式有哪些
  • 做直播网站要多大带宽龙南黄页全部电话
  • 互联网网站运营搜索引擎优化是做什么的
  • 网上怎么查自己的房产信息长沙百度快速优化排名
  • 写网站策划书需要注意什么301313龙虎榜
  • 网站设计专业公司公司网站
  • wp在本地做的网站 上传青岛seo网站关键词优化