Mouse Hid: working demo

This commit is contained in:
Petr Kracik 2020-05-25 02:35:10 +02:00
parent e319aab0c4
commit bd2540cbc0
2 changed files with 26 additions and 11 deletions

View File

@ -24,7 +24,7 @@ hidmouse = None
def hid_init(): def hid_init():
global hiddev, hidmouse global hiddev, hidmouse
hiddev=open('/dev/hidg0', 'rb+') hiddev=open('/dev/hidg0', 'rb+')
hiddev=open('/dev/hidg1', 'rb+') hidmouse=open('/dev/hidg1', 'rb+')
def hid_write(data): def hid_write(data):
if hiddev is None: if hiddev is None:
@ -37,9 +37,13 @@ def hid_mouse_write(btn, x, y, wheel):
if hidmouse is None: if hidmouse is None:
return False return False
data = chr(btn)+chr(x)+chr(y)+chr(wheel) data = bytearray(4)
data[0] = btn
data[1] = x
data[2] = y
data[3] = wheel
hidmouse.write(data.encode()) hidmouse.write(data)
hidmouse.flush() hidmouse.flush()
@ -82,19 +86,25 @@ def index():
return render_template("hid.html") return render_template("hid.html")
@app.route("/mouse.html") @app.route("/mouse.html")
def index(): def mouseindex():
return render_template("mouse.html") return render_template("mouse.html")
@app.route("/hid/mouse", methods=["POST"]) @app.route("/hid/mouse", methods=["POST"])
def mouse(): def mouse():
mouseevent = json.loads(request.data) mouseevent = json.loads(request.data)
print(mouseevent)
btn = mouseevent['btn'] btn = mouseevent['btn']
x = mouseevent['x'] x = mouseevent['x']
y = mouseevent['y'] y = mouseevent['y']
wheel = mouseevent['wheel'] wheel = mouseevent['wheel']
x = x if x >= 0 else 255-abs(x)
y = y if y >= 0 else 255-abs(y)
print ("X: {}, Y: {}".format(x,y))
hid_mouse_write(btn, x, y, wheel) hid_mouse_write(btn, x, y, wheel)
return Response("", mimetype = "text/plain") return Response("", mimetype = "text/plain")

View File

@ -2,7 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Klávesnice remote SSH</title> <title>Mouse remote</title>
<style> <style>
</style> </style>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script> <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
@ -12,15 +12,20 @@
<div> <div>
</div> </div>
<button onclick="sendMouse()" value="Test mouse"/> <input type="button" onclick="sendMouse(0, 0, -10);" value="^" />
<input type="button" onclick="sendMouse(0, -10, 0);" value="<" />
<input type="button" onclick="sendMouse(0, 10, 0);" value=">" />
<input type="button" onclick="sendMouse(0, 0, 10);" value="v" /><br />
<input type="button" onclick="sendMouse(1, 0, 0);" value="LEFT" />
<input type="button" onclick="sendMouse(2, 0, 0);" value="RIGHT" />
<script> <script>
function sendMouse() { function sendMouse(btn, x, y) {
var obj = { var obj = {
x: 0, x: x,
y: 0, y: y,
btn: 0, btn: btn,
wheel: 0, wheel: 0,
} }