Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access tablet pen data via Python?

I need to access a windows tablet pen data (such as the surface) via Python. I mainly need the position, pressure, and tilt values.

I know how to access the Wacom pen data but the windows pen is different.

There is a Python library named Kivy that can handle multi-touch but it recognizes my pen as a finger (WM_TOUCH) and not as a pen (WM_PEN).

This is my Kivy code (that doesnt report pressure and tilt):

 from kivy.app import App
 from kivy.uix.widget import Widget

class TouchInput(Widget):

def on_touch_down(self, touch):
    print(touch)
def on_touch_move(self, touch):
    print(touch)
def on_touch_up(self, touch):
    print("RELEASED!",touch)

class SimpleKivy4(App):

def build(self):
    return TouchInput()

There is a wonderful processing library named Tablet that only works with the Wacom tablet with a simple API (e.g., tablet.getPressure())

I need something like this.

like image 584
Roi Yozevitch Avatar asked Jul 02 '26 19:07

Roi Yozevitch


1 Answers

if you want to see the devices:

import pyglet
pyglet.input.get_devices()

if you want to see the devices controls:

tablets = pyglet.input.get_devices() #tablets is a list of input devices
tablets[0].get_controls() #get_controls gives a list of possible controls of the device  

And now to get the data. I have a xp-pen g640 tablet and don't have tilt sensor, but if yours have it it will be easy to modify the code:

if tablets:
    print('Tablets:')
    for i, tablet in enumerate(tablets):
        print('  (%d) %s' % (i , tablet.name))
i = int(input('type the index of the tablet.'))

device = tablets[i]
controls = device.get_controls()
df = pd.DataFrame()
window = pyglet.window.Window(1020, 576)

# Here you will have to write a line like "control_tilt_x = controls[j]" where j is
# the controls list index of the tilt control.
control_presion = controls[7]
Button = controls[3]
control_x =controls[5]
control_y =controls[6]
control_punta = controls[4]
control_alcance = controls [0]
name = tablets[9].name

try:
    canvas = device.open(window)
except pyglet.input.DeviceException:
    print('Failed to open tablet %d on window' % index)

print('Opened %s' % name)

    
@control_presion.event
def on_change(presion):
    global df   
    df_temp = pd.DataFrame({'x':[control_x.value/(2**15)],
                           'y':[control_y.value/(2**16)],
                           'p':[control_presion.value/8],
                           't':[time.time()]})
    df = pd.concat([df,df_temp])
          
pyglet.app.run()
like image 81
Francisco Iaconis Avatar answered Jul 04 '26 09:07

Francisco Iaconis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!