touch-bar-simulator/Touch Bar Simulator/util.swift
2017-08-23 23:41:36 +07:00

22 lines
632 B
Swift

import Cocoa
extension NSWindow {
var toolbarView: NSView? {
return standardWindowButton(.closeButton)?.superview
}
}
extension NSView {
func addSubviews(_ subviews: NSView...) {
subviews.forEach { addSubview($0) }
}
}
func pressKey(keyCode: CGKeyCode, flags: CGEventFlags = []) {
let eventSource = CGEventSource(stateID: .hidSystemState)
let keyDown = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: true)
let keyUp = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: false)
keyDown?.flags = flags
keyDown?.post(tap: .cghidEventTap)
keyUp?.post(tap: .cghidEventTap)
}