Skip to content

Commit

Permalink
Add unique ids for keyboard event pairs (keyUp, keyDown)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenangst committed Mar 15, 2024
1 parent 6ae3573 commit d4279a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
20 changes: 17 additions & 3 deletions Sources/MachPort/MachPortEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import CoreGraphics
import Foundation

public final class MachPortEvent: @unchecked Sendable {
public let id: UUID
public let keyCode: Int64
public let event: CGEvent
public let eventSource: CGEventSource?
public let isRepeat: Bool
public let type: CGEventType
public let lhs: Bool
public var result: Unmanaged<CGEvent>?

internal init(event: CGEvent, eventSource: CGEventSource?,
lhs: Bool, type: CGEventType, result: Unmanaged<CGEvent>?) {
internal init(id: UUID,
event: CGEvent, eventSource: CGEventSource?,
isRepeat: Bool, lhs: Bool, type: CGEventType,
result: Unmanaged<CGEvent>?) {
self.id = id
self.keyCode = event.getIntegerValueField(.keyboardEventKeycode)
self.isRepeat = isRepeat
self.event = event
self.eventSource = eventSource
self.lhs = lhs
Expand All @@ -21,6 +27,14 @@ public final class MachPortEvent: @unchecked Sendable {

public static func empty() -> MachPortEvent? {
guard let event = CGEvent(source: nil) else { return nil }
return MachPortEvent(event: event, eventSource: nil, lhs: false, type: .null, result: nil)
return MachPortEvent(
id: UUID(),
event: event,
eventSource: nil,
isRepeat: false,
lhs: false,
type: .null,
result: nil
)
}
}
34 changes: 31 additions & 3 deletions Sources/MachPort/MachPortEventController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MachPortEventPublisher {
public final class MachPortEventController: MachPortEventPublisher, @unchecked Sendable {
private(set) public var eventSource: CGEventSource!

private var previousId: UUID?
private var machPort: CFMachPort?
private var runLoopSource: CFRunLoopSource?
private static var lhs: Bool = true
Expand Down Expand Up @@ -137,10 +138,37 @@ public final class MachPortEventController: MachPortEventPublisher, @unchecked S
if cgEvent.getIntegerValueField(.eventSourceUserData) == signature {
return Unmanaged.passUnretained(cgEvent)
}

let isRepeat = cgEvent.getIntegerValueField(.keyboardEventAutorepeat) == 1
let id: UUID

switch cgEvent.type {
case .keyUp:
if let previousId {
id = previousId
} else {
id = UUID()
}
previousId = nil
case .keyDown:
if isRepeat, let previousId {
id = previousId
} else {
id = UUID()
previousId = id
}
default:
previousId = nil
id = UUID()
}

let result = Unmanaged.passUnretained(cgEvent)
let newEvent = MachPortEvent(event: cgEvent, eventSource: eventSource,
lhs: Self.lhs, type: type,
result: result)
let newEvent = MachPortEvent(
id: id,
event: cgEvent, eventSource: eventSource,
isRepeat: isRepeat,
lhs: Self.lhs, type: type,
result: result)

if let onAllEventChange {
if type == .flagsChanged {
Expand Down

0 comments on commit d4279a9

Please sign in to comment.