From b4757306fbc36762c02fc662a84b7bd75e0b1b44 Mon Sep 17 00:00:00 2001 From: NakaokaRei Date: Tue, 24 Jan 2023 00:49:08 +0900 Subject: [PATCH] add: scroll event --- Sample/Sample/ContentView.swift | 21 ++++++++++- Sources/SwiftAutoGUI/SwiftAutoGUI.swift | 48 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/Sample/Sample/ContentView.swift b/Sample/Sample/ContentView.swift index 10d03ea..78d1883 100644 --- a/Sample/Sample/ContentView.swift +++ b/Sample/Sample/ContentView.swift @@ -10,7 +10,10 @@ import SwiftAutoGUI struct ContentView: View { var body: some View { - VStack { + ScrollView { + ForEach(0..<10) { + Text("\($0)").font(.title) + } Button("key event") { SwiftAutoGUI.sendKeyShortcut([.control, .leftArrow]) } @@ -24,6 +27,22 @@ struct ContentView: View { Button("click") { SwiftAutoGUI.leftClick() } + Button("vscroll") { + SwiftAutoGUI.vscroll(clicks: 10) + } + ForEach(0..<10) { + Text("\($0)").font(.title) + } + ScrollView(.horizontal) { + HStack { + ForEach(0..<10) { + Text("\($0)").font(.title) + } + Button("hscroll") { + SwiftAutoGUI.hscroll(clicks: 10) + } + } + } } .padding() } diff --git a/Sources/SwiftAutoGUI/SwiftAutoGUI.swift b/Sources/SwiftAutoGUI/SwiftAutoGUI.swift index 41bf4db..8ab8b84 100644 --- a/Sources/SwiftAutoGUI/SwiftAutoGUI.swift +++ b/Sources/SwiftAutoGUI/SwiftAutoGUI.swift @@ -83,6 +83,54 @@ public class SwiftAutoGUI { leftClickUp(position: to) } + public static func vscroll(clicks: Int) { + for _ in 0...Int(abs(clicks) / 10) { + let scrollEvent = CGEvent( + scrollWheelEvent2Source: nil, + units: .line, + wheelCount: 1, + wheel1: clicks >= 10 ? 10 : -10, + wheel2: 0, + wheel3: 0 + ) + scrollEvent?.post(tap: .cghidEventTap) + } + + let scrollEvent = CGEvent( + scrollWheelEvent2Source: nil, + units: .line, + wheelCount: 1, + wheel1: Int32(clicks >= 10 ? clicks % 10 : -1 * (-clicks % 10)), + wheel2: 0, + wheel3: 0 + ) + scrollEvent?.post(tap: .cghidEventTap) + } + + public static func hscroll(clicks: Int) { + for _ in 0...Int(abs(clicks) / 10) { + let scrollEvent = CGEvent( + scrollWheelEvent2Source: nil, + units: .line, + wheelCount: 2, + wheel1: 0, + wheel2: clicks >= 10 ? 10 : -10, + wheel3: 0 + ) + scrollEvent?.post(tap: .cghidEventTap) + } + + let scrollEvent = CGEvent( + scrollWheelEvent2Source: nil, + units: .line, + wheelCount: 2, + wheel1: 0, + wheel2: Int32(clicks >= 10 ? clicks % 10 : -1 * (-clicks % 10)), + wheel3: 0 + ) + scrollEvent?.post(tap: .cghidEventTap) + } + private static func leftClickDown(position: CGPoint) { let source = CGEventSource(stateID: CGEventSourceStateID.hidSystemState) let event = CGEvent(mouseEventSource: source, mouseType: CGEventType.leftMouseDown,