Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pynput #2

Open
wants to merge 12 commits into
base: cli-drawing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Touchpaddraw

This is a simple python script that converts touchpad to graphics tablet...

> :warning: Still this is in /prototype/ phase and still require *root* privilages... use this *AT YOUR OWN RISK*... i am not getting time to work on this (need to rewrite this in rust or c)...
> :warning: NO support for wayland(yet)

## Demo

[![demo yt video for touchpad draw](https://img.youtube.com/vi/jfI_lGY1dHM/0.jpg)](https://www.youtube.com/watch?v=jfI_lGY1dHM)

## how to use

clone this repo
```
git clone https://github.com/Vaisakhkm2625/touchpaddraw.git
```
or
```
git clone [email protected]:Vaisakhkm2625/touchpaddraw.git
```

install `evtest`

Run `sudo evtest` and find the event of your touchpad

```
/dev/input/event8: MSFT0001:01 06CB:CE2D Touchpad
```
means you have `event8`

install python packages
```
sudo pip install evdev
sudo pip install pynput
```
(if you are not able to install, see: https://github.com/gvalkov/python-evdev/issues/164)


TODO: (libevdev or evdev)

then run

```bash
sudo python main.py -d event8

```
accouding to your event number



45 changes: 24 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import os
import time

#get trackpad absolute coords
from evdev import InputDevice

import argparse

from pynput import mouse
from pynput.mouse import Button

# Initialize parser
parser = argparse.ArgumentParser()
Expand All @@ -20,21 +23,17 @@
args = parser.parse_args()

#SET THIS TO YOUR DEVICE
if args.device:
device = InputDevice('/dev/input/event6')

device = InputDevice('/dev/input/'+ args.device if args.device else 'event7')

# width = 80
# height = 30
touchpad_x_max = 1224
touchpad_y_max = 804

max_x = 1920
max_y = 1080

width = 160
height = 60
x = 0
y = 0

mat = [ [' ']*width for i in range(height)]

def get_xy_coords(e):
#you may need to change this number here; i don't know
if e.code == 53:
Expand All @@ -50,19 +49,23 @@ def mapFromTo(x,a,b,c,d):
y=(x-a)/(b-a)*(d-c)+c
return y

x_pos =0
y_pos =0

def draw(mat):
os.system('clear')
for i in mat:
for j in i:
print(j, end='')
print()
# time.sleep(20)

mouse_controller = mouse.Controller()
for event in device.read_loop():
#rows, cols = stdscr.getmaxyx()
get_xy_coords(event)

if event.code == 54:
mat[math.floor(mapFromTo(y,0,804,0,height-1))][math.floor(mapFromTo(x,0,1224,0,width-1))] = '#'
# mat[mapFromTo(y,0,804,0,height-1)][mapFromTo(x,0,1224,0,width-1)] = '#'
draw(mat)
prev_x_pos = x_pos
prev_y_pos = y_pos
x_pos =math.floor(mapFromTo(x,0,touchpad_x_max,0,max_x))
y_pos =math.floor(mapFromTo(y,0,touchpad_y_max,0,max_y))
if (abs(prev_x_pos-x_pos)>15 or abs(prev_y_pos-y_pos)>15):
mouse_controller.release(Button.left)
mouse_controller.position = (x_pos,y_pos);
mouse_controller.press(Button.left)
mouse_controller.position = (x_pos,y_pos);

#stdscr.addstr(math.floor(mapFromTo(y,0,touchpad_y_max,0,max_y)),math.floor(mapFromTo(x,0,touchpad_x_max,0,max_x)),char)