LoginSignup
3

More than 5 years have passed since last update.

[OSX][Swift]ウィンドウがアクティブかを判定する方法

Last updated at Posted at 2017-09-21

ウィンドウが画面のトップにあるかを判定する方法。

参考コード

example.swift
    public func isWindowActive() -> Bool {
        //ウィンドウ群を配列にて取得する
        let windows: NSArray = NSApplication.shared().windows as NSArray
        //サンプルのため、0番目のウィンドウを判定
        let window: NSWindow = windows.firstObject as! NSWindow

        return window.isKeyWindow  // true : トップにある, false : トップでない
    }

備考

画面が複数ある場合は「NSApplication.shared().windows」の中身を取り出した上でisKeyWindowプロパティを参照する。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3