Bjarke Lund
Bjarke Lund

Reputation: 23

Simulate horizontal scroll in Windows with python

as the title says, I'm looking for a way to simulate horizontal scrolling (specifically in OneNote). I know it is possible to do it in AutoHotKey with a script, but I'm trying to keep the program as localized as possible. I also know it is possible with PyAutoGui on mac and linux, but I've come up empty handed with anything related to windows. If you have any leads, I would greatly appreciate it:)

Upvotes: 0

Views: 1134

Answers (2)

Windows 10 already has an out-of-the-box shortcut for horizontal scroll: Hold Shift down and use the mouse wheel for side scroll. (This shortcut seems to also work on Ubuntu und MacOS)

Considering this shortcut exists it is possible to emulate it with PyAutoGui like so

import pyautogui
offset = 100
pyautogui.keyDown('shift')
pyautogui.scroll(offset)
pyautogui.keyUp('shift')

Upvotes: 2

Bjarke Lund
Bjarke Lund

Reputation: 23

For anyone running into a similar problem in the future, here's my solution:

import win32api, time, pyautogui as pag, keyboard
from win32con import *


running = True
lastX, lastY = pag.position()

while running:

    while keyboard.is_pressed("shift"):
        x, y = pag.position()
        if lastX!=x:
            win32api.mouse_event(MOUSEEVENTF_HWHEEL, 0, 0, x-lastX, 0) # Horizontal scrolling
            lastX=x

        if lastY!=y:
            win32api.mouse_event(MOUSEEVENTF_WHEEL, 0, 0, lastY-y, 0) # Vertical scrolling
            lastY=y

Hope this can help anyone in the future:)

Upvotes: 1

Related Questions