如何通过Python访问tablet笔数据?

2024-06-17 09:09:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要通过Python访问windows平板电脑笔数据(比如表面)。我主要需要位置,压力和倾斜值。在

我知道如何访问Wacom笔数据,但windows笔不同。在

有一个名为Kivy的Python库可以处理多点触控,但它将我的笔识别为手指(WM_touch)而不是笔(WM_pen)。在

这是我的Kivy代码(不报告压力和倾斜):

 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()

有一个很棒的processing库,名为Tablet,它只适用于具有简单API的Wacom平板电脑(例如tablet.getPressure()

我需要这样的东西。在


Tags: 数据fromselfonwindowsdeftouchprint
1条回答
网友
1楼 · 发布于 2024-06-17 09:09:49

这个解决方案适用于我,它适用于here中的Python3。在

工作原理:笔,橡皮擦,笔按钮,两个压力传感器。在

首先安装pyglet库:pip install pyglet。然后使用代码:

import pyglet

window = pyglet.window.Window()
tablets = pyglet.input.get_tablets()
canvases = []

if tablets:
    print('Tablets:')
    for i, tablet in enumerate(tablets):
        print('  (%d) %s' % (i + 1, tablet.name))
    print('Press number key to open corresponding tablet device.')
else:
    print('No tablets found.')

@window.event
def on_text(text):
    try:
        index = int(text) - 1
    except ValueError:
        return

    if not (0 <= index < len(tablets)):
        return

    name = tablets[i].name

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

    print('Opened %s' % name)

    @canvas.event
    def on_enter(cursor):
        print('%s: on_enter(%r)' % (name, cursor))

    @canvas.event
    def on_leave(cursor):
        print('%s: on_leave(%r)' % (name, cursor))

    @canvas.event
    def on_motion(cursor, x, y, pressure, a, b):  # if you know what "a" and "b" are tell me (tilt?)
        print('%s: on_motion(%r, x=%r, y=%r, pressure=%r, %s, %s)' % (name, cursor, x, y, pressure, a, b))

@window.event
def on_mouse_press(x, y, button, modifiers):
    print('on_mouse_press(%r, %r, %r, %r' % (x, y, button, modifiers))

@window.event
def on_mouse_release(x, y, button, modifiers):
    print('on_mouse_release(%r, %r, %r, %r' % (x, y, button, modifiers))

pyglet.app.run()

相关问题 更多 >