2016-07-31 49 views

回答

1

看看记录模块,它可以双双创下的事件,也重播它们

这里是一个小例子:(变更记录为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}) 

您可以使用在许多其他方面的记录类,请参阅文档: 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? 
+0

感谢您的答复,但我不看回放的动作。我有一个模型可以播放随机生成的游戏,但我需要该模型才能移动鼠标并点击。我可以将“光标”小部件移动到它应该在的位置,但我不理解文档以实际发送动作事件或触摸到该位置 –

+0

@JacobKern查看其[源代码](https:// github .com/kivy/kivy/blob/master/kivy/input/recorder.py)它会帮助您找到启动点击/触摸事件的内容。 – KeyWeeUsr

+1

@KeyWeeUsr - 这不是那么容易,录音机是一个输入提供商,所以它会产生自己的事件 - 而不是向其他提供商“发射”事件。我编辑了我的anwser以包含一个小帮助函数,可能有助于获得所需的结果 –