Skip to content

Commit

Permalink
add: scroll event
Browse files Browse the repository at this point in the history
  • Loading branch information
NakaokaRei committed Jan 23, 2023
1 parent f70d88e commit b475730
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Sample/Sample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand All @@ -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()
}
Expand Down
48 changes: 48 additions & 0 deletions Sources/SwiftAutoGUI/SwiftAutoGUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b475730

Please sign in to comment.