Simulate horizontal scroll in Windows with python

Question:

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:)

Asked By: Bjarke Lund

||

Answers:

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:)

Answered By: Bjarke Lund

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')
Answered By: Nikolay Manolov
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.