Skip to content

Instantly share code, notes, and snippets.

@xr1337
Created March 19, 2020 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xr1337/0271c9a7bc33bfb9a311321ddeaba3cd to your computer and use it in GitHub Desktop.
Save xr1337/0271c9a7bc33bfb9a311321ddeaba3cd to your computer and use it in GitHub Desktop.
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