【问题标题】:How can I access tablet pen data via Python?如何通过 Python 访问数位板笔数据?
【发布时间】:2019-05-28 21:58:17
【问题描述】:

我需要通过 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()

我需要这样的东西。

【问题讨论】:

    标签: python tablet surface stylus-pen


    【解决方案1】:

    如果您想查看设备:

    import pyglet
    pyglet.input.get_devices()
    

    如果您想查看设备控件:

    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  
    

    现在来获取数据。我有一台 xp-pen g640 平板电脑,但没有倾斜传感器,但如果你有,修改代码很容易:

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

    【讨论】:

      【解决方案2】:

      此解决方案适用于我,适用于来自 here 的 Python 3。

      有用的东西:笔、橡皮擦、笔按钮、两个压力传感器。

      首先安装 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()
      

      【讨论】:

      • 谢谢。我试过你的代码,但是当我移动表面笔时,我只得到鼠标事件(按下、释放)而没有运动事件,另外,我得到“没有找到平板电脑”没有找到平板电脑。 on_mouse_press(448, 150, 1, 16 on_mouse_release(273, 324, 1, 16 on_mouse_press(252, 318, 1, 16 on_mouse_release(237, 151, 1, 16
      • @RoiYozevitch,“没有平板电脑”意味着坏消息。也许您需要驱动程序,或者您的设备太新。我用的是 Wacom Bamboo。
      猜你喜欢
      • 2018-11-13
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多