How to detect user inactivity in OS X writing in Swift Cocoa?
Asked Answered
S

1

5

I have searched answers in stackoverflow and none of them matches my needs. I am creating time tracking app on Swift Cocoa macOS, like Hubstaff time tracking app. At the moment runs a timer and I want to detect user's inactivity after x period of time and to send a Notification that he has been Idle x period of time. I'm new to iOS and macOS development. Can I have an example of how to do it?

Here is my code:

import Cocoa

class ViewController: NSViewController {

@IBOutlet weak var label: NSTextField!
@IBOutlet weak var playImage: NSButton!

var timer : Timer!
var isTimerWorking : Bool = false
var startTime : Date!

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func playPause(_ sender: NSButton) {

    if isTimerWorking {
        endTimer()
        playImage.image = NSImage(named: NSImage.Name("play"))
        sender.state = .off
    } else {
        startTimer()
        playImage.image = NSImage(named: NSImage.Name("stop"))
        sender.state = .off
    }
}

func startTimer() {
    startTime = Date()
    timer = Timer.scheduledTimer(
        timeInterval: 1.0,
        target: self,
        selector: #selector(self.timerCounter),
        userInfo: nil,
        repeats: true
    )
    isTimerWorking = true
}

func endTimer() {
    if timer != nil {
        timer.invalidate()
        label.stringValue = "00:00:00"
    }
    isTimerWorking = false
}

@objc func timerCounter() {
    let currentTime = Date().timeIntervalSince(startTime)
    let hour   = Int(fmod(currentTime/3600, 60))
    let minute = Int(fmod(currentTime/60,   60))
    let second = Int(fmod(currentTime,      60))
    let hourValue   = String(format:"%02d", hour)
    let minuteValue = String(format:"%02d", minute)
    let secondValue = String(format:"%02d", second)
    label.stringValue = "\(hourValue):\(minuteValue):\(secondValue)"
}
}
Sumikosumma answered 30/11, 2018 at 14:8 Comment(9)
You probably want to use Date(). If you insist on using a Timer() see this link. #42319672Spue
Possible duplicate of Detect user activity in Cocoa app (taps, clicks, ...)Toby
@Toby I want to detect User's activity on a Mac instead on app. App will work minimized.Sumikosumma
You need some code somewhere, if not in an app where does the code run?Toby
@Toby of course in app. I'm new to this, that why it would be awesome if I'll get an example of how can I do it.Sumikosumma
Add a global event monitor, start a timer and replace the timer or adjust the fire date when an event is received.Toby
@Toby Thank you very much, I'll give it a try.Sumikosumma
@Willeke: I think, the OP's question is different from the question you link to, at least the headline suggests that.Bowdlerize
@gabriel The solution is the same. The accepted answer to this question is a translation of one of the answers to the linked question.Toby
S
19

In my own time tracking app I am using

var lastEvent:CFTimeInterval = 0
lastEvent = CGEventSource.secondsSinceLastEventType(CGEventSourceStateID.hidSystemState, eventType: CGEventType(rawValue: ~0)!)

print(lastEvent)

to get the user idle time.

Staging answered 30/11, 2018 at 17:27 Comment(3)
Any idea why this would cause my app (a launch daemon) to freeze if it gets called before a user logs into the OS?Hypostasize
@Hypostasize ever figure it out? thx for the warningCovert
@Covert no sorry. I just modified my code so that it only does the above call if a user is logged into the system.Hypostasize

© 2022 - 2024 — McMap. All rights reserved.