How can I access tablet pen data via Python?

2024/10/10 4:19:11

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 Appfrom kivy.uix.widget import Widgetclass 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.

Answer

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].nametry: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()
https://en.xdnf.cn/q/69936.html

Related Q&A

Read Celery configuration from Python properties file

I have an application that needs to initialize Celery and other things (e.g. database). I would like to have a .ini file that would contain the applications configuration. This should be passed to th…

numpys tostring/fromstring --- what do I need to specify to restore the array

Given a raw binary representation of a numpy array, what is the complete set of metadata needed to unambiguously restore the array? For example, >>> np.fromstring( np.array([42]).tostring())…

How to limit width of column headers in Pandas

How can I limit the column width within Pandas when displaying dataframes, etc? I know about display.max_colwidth but it doesnt affect column names. Also, I do not want to break the names up, but rath…

Django + Auth0 JWT authentication refusing to decode

I am trying to implement Auth0 JWT-based authentication in my Django REST API using the django-rest-framework. I know that there is a JWT library available for the REST framework, and I have tried usin…

How to measure the angle between 2 lines in a same image using python opencv?

I have detected a lane boundary line which is not straight using hough transform and then extracted that line separately. Then blended with another image that has a straight line. Now I need to calcula…

How to modify variables in another python file?

windows 10 - python 3.5.2Hi, I have the two following python files, and I want to edit the second files variables using the code in the first python file.firstfile.pyfrom X.secondfile import *def edit(…

SciPy optimizer ignores one of the constraints

I am trying to solve an optimization problem where I need to create a portfolio that with a minimum tracking error from benchmark portfolio and its subject to some constraints:import scipy.optimize as …

How can one use HashiCorp Vault in Airflow?

I am starting to use Apache Airflow and I am wondering how to effectively make it use secrets and passwords stored in Vault. Unfortunately, search does not return meaningful answers beyond a yet-to-be-…

List all words in a dictionary that start with user input

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string?Ex: User: "abd" Program:abdicate, abdomen, abduct..…

Python version of C#s conditional operator (?)

I saw this question but it uses the ?? operator as a null check, I want to use it as a bool true/false test.I have this code in Python:if self.trait == self.spouse.trait:trait = self.trait else:trait…