HID Mouse: absolute mouse support

This commit is contained in:
Petr Kracik 2020-06-05 15:04:27 +02:00
parent f22f2eeb67
commit 06519e7009
2 changed files with 66 additions and 5 deletions

View File

@ -17,12 +17,14 @@ app = Flask(__name__)
hiddev = None
hidmouse = None
hidmouseabs = None
def hid_init():
global hiddev, hidmouse
global hiddev, hidmouse, hidmouseabs
hiddev = open('/dev/hidg0', 'rb+')
hidmouse = open('/dev/hidg1', 'rb+')
hidmouseabs = open('/dev/hidg2', 'rb+')
def hid_write(data):
@ -47,6 +49,20 @@ def hid_mouse_write(btn, x, y, wheel):
hidmouse.flush()
def hid_mouse_writeabs(btn, x, y, wheel):
if hidmouseabs is None:
return False
data = bytearray(6)
data[0] = btn
data[1:3] = x.to_bytes(2, byteorder='little')
data[3:5] = y.to_bytes(2, byteorder='little')
data[5] = wheel
hidmouseabs.write(data)
hidmouseabs.flush()
def send_key(hidkey, shift, alt, ctlr, mod):
data = bytearray(8)
if shift:
@ -91,7 +107,6 @@ def index():
def mouseindex():
return render_template("mouse.html")
@app.route("/hid/mouse", methods=["POST"])
def mouse():
mouseevent = json.loads(request.data)
@ -111,6 +126,22 @@ def mouse():
return Response("", mimetype="text/plain")
@app.route("/hid/mouseabs", methods=["POST"])
def mouseabs():
mouseevent = json.loads(request.data)
print(mouseevent)
btn = mouseevent['btn']
x = mouseevent['x']
y = mouseevent['y']
wheel = mouseevent['wheel']
print("X: {}, Y: {}".format(x, y))
hid_mouse_writeabs(btn, x, y, wheel)
return Response("", mimetype="text/plain")
@app.route("/hid/keyboard", methods=["POST"])
def keypress():

View File

@ -18,9 +18,37 @@
<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>
var btn=0;
var posX=0;
var posY=0;
$(document).ready(function(){
$("canvas").mousedown(function(event){
btn=event.buttons;
handleMouse();
});
$("canvas").mouseup(function(event){
btn=event.buttons;
handleMouse();
});
$("canvas").mousemove(function(event){
// sendMouse(0, event.originalEvent.movementX, event.originalEvent.movementY);
posX=event.offsetX;
posY=event.offsetY;
handleMouse();
});
});
function handleMouse() {
$("span").text(btn +", "+ posX + ", " + posY);
sendMouse(btn, posX, posY);
}
function sendMouse(btn, x, y) {
var obj = {
x: x,
@ -31,7 +59,7 @@ function sendMouse(btn, x, y) {
var ret = $.ajax({
type: "POST",
url: "hid/mouse",
url: "hid/mouseabs",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json"
@ -40,6 +68,8 @@ function sendMouse(btn, x, y) {
};
</script>
<span></span><br/><br/>
<canvas width=500 height=500 style="border: 1px solid black" oncontextmenu="return false"></canvas>
<!--<img src="placeholder.png" width=1920 height=1080 onmousemove='console.log("aaa");' />-->
</body>
</html>