Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A viewcontroller to get last event time on the system
//
// ViewController.swift
//
// Created by Sufiyan Yasa on 19/03/2020.
// Copyright © 2020 Sufiyan Yasa. All rights reserved.
//
import Cocoa
import CoreGraphics
class ViewController: NSViewController {
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print("hi there")
}
override func viewDidAppear() {
self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
}
override func viewDidDisappear() {
self.timer?.invalidate()
}
@objc func fireTimer() {
let anyEventTypeInput = CGEventType(rawValue: ~0)!
let count = CGEventSource.counterForEventType(.combinedSessionState, eventType: anyEventTypeInput)
let seconds = CGEventSource.secondsSinceLastEventType(.combinedSessionState, eventType: anyEventTypeInput)
print("found count \(count) seconds since \(seconds)")
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment