使用Python中的Quartz.CoreGraphics进行平滑滚动

时间:2013-04-20 16:51:33

标签: python scroll quartz-graphics smooth-scrolling

我正在使用Quartz Event Services发送鼠标滚轮事件,如下所示:

from Quartz.CoreGraphics import CGEventCreateScrollWheelEvent, CGEventPost, kCGHIDEventTap

def scroll_wheel_up(num_times):
    for _ in xrange(num_times):
        # Scroll up 1 pixel
        event = CGEventCreateScrollWheelEvent(None, 0, 1, 1)
        CGEventPost(kCGHIDEventTap, event)

这可行,但非常不稳定 - 它跳转而不是滚动。

我已经尝试了一些方法来平滑它,比如向上滚动后向下滚动速度较慢。我还试图“减速”滚动速度:

def scroll_wheel_up(num_times):
    for i in xrange(1, num_times):
        multiplier = 1 - (float(i) / num_times)
        speed = 4 * multiplier
        event = CGEventCreateScrollWheelEvent(None, 0, 1, speed)
        CGEventPost(kCGHIDEventTap, event)

两者都没有成功。有没有办法以编程方式使用Quartz或PyObjC绑定顺利滚动窗口?

1 个答案:

答案 0 :(得分:1)

发布后,我立即想要在滚动事件之间添加一个小延迟:

def scroll_wheel_up(num_times):
    for i in xrange(1, num_times):
        time.sleep(.005)
        multiplier = 1 - (float(i) / num_times)
        speed = 4 * multiplier
        event = CGEventCreateScrollWheelEvent(None, 0, 1, speed)
        CGEventPost(kCGHIDEventTap, event)

这更顺畅,令我满意。如果有人有另一种方法可以做到这一点,我很乐意听到它!