Question

I am using Quartz Event Services to send a mouse wheel event, like so:

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)

This works but is pretty choppy - it jumps instead of scrolling.

I've tried a few things to smooth it, like scrolling down at a slower speed after scrolling up. I also tried to "decelerate" the scrolling speed:

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)

Neither was successful. Is there any way to programmatically scroll a window smoothly using Quartz or maybe PyObjC bindings?

Était-ce utile?

La solution

Immediately after posting it occurred to me to add a small delay between scroll events:

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)

This is a lot smoother and works to my satisfaction. If someone has another way to do this I'd love to hear it though!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top