MIT License

项目介绍

micmonay/keybd_event 是一个托管在 GitHub 上的开源项目,主要使用 Go 编写,采用 MIT License 许可证,目前获得 410 个 Star、61 次 Fork。

仓库全名
micmonay/keybd_event
开源许可证
MIT License
Star 数
410
Fork 数
61
未关闭 Issue
15

README 摘要

keybdevent This library simulates the key press on a keyboard. It runs on Linux, Windows and Mac. Important : The keys change in the different keyboard layouts of the target computer(s). I have tested this code on my system and I don't find any errors. If you have a bug, please create an issue. Example : For easy access to all the keys on the virtual keyboard, I have added more special keycodes constants VKSP. Below…

完整内容见 GitHub 原始 README 或本页 。

常见问题

keybd_event 用什么语言开发?

主要使用 Go 编写,采用 MIT License 许可证,目前有 410 个 Star。

keybd_event 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 keybd_event 的源码?

可以直接克隆仓库:git clone https://github.com/micmonay/keybd_event.git,默认分支为 master;安装与运行步骤见仓库 README。

keybd_event 最近还在维护吗?

仓库最近一次更新为 2023年12月12日,当前有 15 个未关闭 Issue;Star 数为 410,可作为社区活跃度的参考。

关于

For simulate key press in Linux, Windows and Mac in golang

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
533
价值分
263
活跃状态
归档

语言占比

Go

Release

README

keybd_event

go.dev reference

This library simulates the key press on a keyboard. It runs on Linux, Windows and Mac.

Important :

  • The keys change in the different keyboard layouts of the target computer(s).
  • I have tested this code on my system and I don’t find any errors. If you have a bug, please create an issue.

Example :

package main

import (
	"runtime"
	"time"
	"github.com/micmonay/keybd_event"
)

func main() {
	kb, err := keybd_event.NewKeyBonding()
	if err != nil {
		panic(err)
	}

	// For linux, it is very important to wait 2 seconds
	if runtime.GOOS == "linux" {
		time.Sleep(2 * time.Second)
	}
	
	// Select keys to be pressed
	kb.SetKeys(keybd_event.VK_A, keybd_event.VK_B) 

	// Set shift to be pressed
	kb.HasSHIFT(true) 

	// Press the selected keys
	err = kb.Launching() 
	if err != nil {
		panic(err)
	}
	
	// Or you can use Press and Release
	kb.Press()
	time.Sleep(10 * time.Millisecond)
	kb.Release()

	// Here, the program will generate "ABAB" as if they were pressed on the keyboard.
}

For easy access to all the keys on the virtual keyboard, I have added more special keycodes constants VK_SP*.

Below is an illustration showing the “VK_” symbols for each key on the keyboard: keyboard.png

Linux

On Linux this library uses uinput, which on the major distributions requires root permissions.

The easy solution is executing through root user (by using sudo). A worse way is by changing the executable’s permissions by using chmod.

Secure Linux Example

sudo groupadd uinput
sudo usermod -a -G uinput my_username
sudo udevadm control --reload-rules
echo "SUBSYSTEM==\"misc\", KERNEL==\"uinput\", GROUP=\"uinput\", MODE=\"0660\"" | sudo tee /etc/udev/rules.d/uinput.rules
echo uinput | sudo tee /etc/modules-load.d/uinput.conf

Another subtlety on Linux: it is important after creating the keybd_event to wait 2 seconds before running first keyboard actions.

Darwin (MacOS)

This library depends on Apple’s frameworks, and I did not find a solution to cross-compile from another OS to MacOS.

评论

暂无评论