使用 Kivy 正确创建鼠标事件
Properly creating a mouse event with Kivy
我正在尝试创建一个程序来控制我的 Kivy 应用程序上的鼠标。创建提供商并将其发送到我想要移动和点击的位置的正确方法是什么?
看看记录器模块,它既可以记录事件也可以重放它们
这是一个小例子:(将 RECORD 更改为 False 以在录制后观看重播...)
import kivy
from kivy.uix.button import Button
from kivy.app import App
from kivy.input.recorder import Recorder
rec = Recorder(filename='myrecorder.kvi',
record_attrs=['is_touch', 'sx', 'sy', 'angle', 'pressure'],
record_profile_mask=['pos', 'angle', 'pressure'])
def funky(b):
print("Hello!!!")
if RECORD:
rec.record = False
else:
rec.play = False
exit(0)
class MyApp(App):
def build(self):
if RECORD:
rec.record = True
else:
rec.play = True
return Button(text="hello", on_release=funky)
if __name__ == '__main__':
RECORD = True # False for replay
MyApp().run()
现在您可以看到文件 myrecorder.kvi:
#RECORDER1.0
(1.1087048053741455, 'begin', 1, {'profile': ['pos'], 'sx': 0.65875, 'is_touch': True, 'sy': 0.51})
(1.1346497535705566, 'update', 1, {'profile': ['pos'], 'sx': 0.66, 'is_touch': True, 'sy': 0.51})
(1.1994667053222656, 'end', 1, {'profile': ['pos'], 'sx': 0.66, 'is_touch': True, 'sy': 0.51})
您可以通过许多其他方式使用记录器 class,请参阅文档:
https://kivy.org/docs/api-kivy.input.recorder.html
可以将录音机封装在一个函数中,做一个小帮手:
#not tested
def click(x, y):
with open("clicker.kvi", 'w') as f:
f.write("""\#RECORDER1.0
(0.1087048053741455, 'begin', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})
(0.1346497535705566, 'update', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})
(0.1994667053222656, 'end', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})""".format(x=x, y=y))
rec = Recorder(filename='clicker.kvi',
record_attrs=['is_touch', 'sx', 'sy', 'angle', 'pressure'],
record_profile_mask=['pos', 'angle', 'pressure'])
rec.play = True
#should call rec.play = False somewhere?
我正在尝试创建一个程序来控制我的 Kivy 应用程序上的鼠标。创建提供商并将其发送到我想要移动和点击的位置的正确方法是什么?
看看记录器模块,它既可以记录事件也可以重放它们
这是一个小例子:(将 RECORD 更改为 False 以在录制后观看重播...)
import kivy
from kivy.uix.button import Button
from kivy.app import App
from kivy.input.recorder import Recorder
rec = Recorder(filename='myrecorder.kvi',
record_attrs=['is_touch', 'sx', 'sy', 'angle', 'pressure'],
record_profile_mask=['pos', 'angle', 'pressure'])
def funky(b):
print("Hello!!!")
if RECORD:
rec.record = False
else:
rec.play = False
exit(0)
class MyApp(App):
def build(self):
if RECORD:
rec.record = True
else:
rec.play = True
return Button(text="hello", on_release=funky)
if __name__ == '__main__':
RECORD = True # False for replay
MyApp().run()
现在您可以看到文件 myrecorder.kvi:
#RECORDER1.0
(1.1087048053741455, 'begin', 1, {'profile': ['pos'], 'sx': 0.65875, 'is_touch': True, 'sy': 0.51})
(1.1346497535705566, 'update', 1, {'profile': ['pos'], 'sx': 0.66, 'is_touch': True, 'sy': 0.51})
(1.1994667053222656, 'end', 1, {'profile': ['pos'], 'sx': 0.66, 'is_touch': True, 'sy': 0.51})
您可以通过许多其他方式使用记录器 class,请参阅文档: https://kivy.org/docs/api-kivy.input.recorder.html
可以将录音机封装在一个函数中,做一个小帮手:
#not tested
def click(x, y):
with open("clicker.kvi", 'w') as f:
f.write("""\#RECORDER1.0
(0.1087048053741455, 'begin', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})
(0.1346497535705566, 'update', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})
(0.1994667053222656, 'end', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})""".format(x=x, y=y))
rec = Recorder(filename='clicker.kvi',
record_attrs=['is_touch', 'sx', 'sy', 'angle', 'pressure'],
record_profile_mask=['pos', 'angle', 'pressure'])
rec.play = True
#should call rec.play = False somewhere?