golang通过智能手机控制鼠标和键盘的远程触控插件remote-touchpad的使用
Golang通过智能手机控制鼠标和键盘的远程触控插件remote-touchpad的使用
Remote Touchpad
通过智能手机(或任何其他带有触摸屏的设备)的网页浏览器控制鼠标和键盘。要开始控制,请打开显示的URL或扫描二维码。
支持Flatpak的RemoteDesktop portal(用于Wayland)、Windows和X11。
安装
-
Golang安装方式:
-
Portal & uinput & X11:
go install -tags portal,uinput,x11 github.com/unrud/remote-touchpad@latest
-
Windows:
go install github.com/unrud/remote-touchpad@latest
-
截图
示例代码
以下是一个简单的Golang示例,展示如何使用remote-touchpad的基本功能:
package main
import (
"log"
"net/http"
"github.com/unrud/remote-touchpad/pkg/server"
)
func main() {
// 创建远程触控板服务器
srv, err := server.New()
if err != nil {
log.Fatal(err)
}
// 设置HTTP路由
http.Handle("/", srv)
// 启动服务器
log.Println("Server starting on :8080")
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
代码说明:
- 导入必要的包,包括
remote-touchpad
的服务器包 - 创建一个新的远程触控板服务器实例
- 设置HTTP路由,将根路径"/"映射到触控板服务器
- 启动HTTP服务器监听8080端口
要使用此代码,您需要先安装remote-touchpad库:
go get github.com/unrud/remote-touchpad
运行程序后,您可以在智能手机浏览器中访问显示的URL或扫描二维码来控制电脑的鼠标和键盘。
更多关于golang通过智能手机控制鼠标和键盘的远程触控插件remote-touchpad的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang通过智能手机控制鼠标和键盘的远程触控插件remote-touchpad的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
使用remote-touchpad实现Golang远程控制鼠标键盘
remote-touchpad是一个允许通过智能手机控制电脑鼠标和键盘的开源项目。下面我将介绍如何在Golang中使用和集成这个功能。
基本概念
remote-touchpad通常由两部分组成:
- 服务器端 - 运行在电脑上,接收控制指令
- 客户端 - 运行在智能手机上,发送控制指令
Golang实现方案
方案1: 使用现有库
package main
import (
"fmt"
"net/http"
"github.com/gorilla/websocket"
"github.com/go-vgo/robotgo"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func main() {
http.HandleFunc("/ws", handleWebSocket)
fmt.Println("Server started at :8080")
http.ListenAndServe(":8080", nil)
}
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("WebSocket upgrade error:", err)
return
}
defer conn.Close()
for {
_, message, err := conn.ReadMessage()
if err != nil {
fmt.Println("Read error:", err)
break
}
// 解析控制指令并执行相应操作
handleControlCommand(string(message))
}
}
func handleControlCommand(cmd string) {
// 这里简化处理,实际应该解析JSON指令
switch cmd {
case "left_click":
robotgo.MouseClick("left")
case "right_click":
robotgo.MouseClick("right")
case "move_up":
x, y := robotgo.GetMousePos()
robotgo.MoveMouse(x, y-10)
case "move_down":
x, y := robotgo.GetMousePos()
robotgo.MoveMouse(x, y+10)
// 添加更多控制指令...
default:
fmt.Println("Unknown command:", cmd)
}
}
方案2: 完整实现
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/gorilla/websocket"
"github.com/go-vgo/robotgo"
)
type Command struct {
Action string `json:"action"`
X float64 `json:"x,omitempty"`
Y float64 `json:"y,omitempty"`
Key string `json:"key,omitempty"`
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true // 允许所有跨域请求,生产环境应限制
},
}
func main() {
// 静态文件服务
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
// WebSocket端点
http.HandleFunc("/ws", handleConnections)
port := "8080"
if len(os.Args) > 1 {
port = os.Args[1]
}
fmt.Printf("Remote Touchpad server running on :%s\n", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
fmt.Println("Server error:", err)
}
}
func handleConnections(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Upgrade error:", err)
return
}
defer ws.Close()
for {
_, msg, err := ws.ReadMessage()
if err != nil {
fmt.Println("Read error:", err)
break
}
var cmd Command
if err := json.Unmarshal(msg, &cmd); err != nil {
fmt.Println("JSON decode error:", err)
continue
}
processCommand(cmd)
}
}
func processCommand(cmd Command) {
switch cmd.Action {
case "move":
screenWidth, screenHeight := robotgo.GetScreenSize()
x := int(cmd.X * float64(screenWidth))
y := int(cmd.Y * float64(screenHeight))
robotgo.MoveMouse(x, y)
case "click":
robotgo.MouseClick(cmd.Key)
case "scroll":
robotgo.ScrollMouse(int(cmd.Y*10), "up")
case "key":
robotgo.KeyTap(cmd.Key)
default:
fmt.Println("Unknown command:", cmd.Action)
}
}
客户端实现
你需要一个简单的HTML/JavaScript客户端来发送指令:
<!-- static/index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Remote Touchpad</title>
<style>
#touchpad {
width: 100%;
height: 300px;
background-color: #f0f0f0;
touch-action: none;
}
</style>
</head>
<body>
<div id="touchpad"></div>
<button id="leftClick">Left Click</button>
<button id="rightClick">Right Click</button>
<script>
const touchpad = document.getElementById('touchpad');
const leftBtn = document.getElementById('leftClick');
const rightBtn = document.getElementById('rightClick');
const ws = new WebSocket(`ws://${window.location.host}/ws`);
touchpad.addEventListener('touchmove', (e) => {
e.preventDefault();
const touch = e.touches[0];
const rect = touchpad.getBoundingClientRect();
const x = (touch.clientX - rect.left) / rect.width;
const y = (touch.clientY - rect.top) / rect.height;
ws.send(JSON.stringify({
action: 'move',
x: x,
y: y
}));
});
touchpad.addEventListener('touchend', (e) => {
ws.send(JSON.stringify({action: 'click', key: 'left'}));
});
leftBtn.addEventListener('click', () => {
ws.send(JSON.stringify({action: 'click', key: 'left'}));
});
rightBtn.addEventListener('click', () => {
ws.send(JSON.stringify({action: 'click', key: 'right'}));
});
</script>
</body>
</html>
部署与使用
- 编译并运行服务器程序:
go build -o remote-touchpad
./remote-touchpad
-
在手机浏览器中访问电脑的IP地址和端口(如
http://192.168.1.100:8080
) -
使用触摸板区域控制鼠标移动,按钮控制点击
安全注意事项
- 生产环境中应添加认证机制
- 限制可访问的IP范围
- 使用HTTPS/WSS加密通信
- 考虑实现会话超时
扩展功能
- 添加键盘输入功能
- 实现多点触控手势
- 添加剪贴板同步
- 实现文件传输功能
以上代码提供了基本的远程控制功能框架,你可以根据需要进行扩展和完善。