NSPasteboard 整理

NSPasteboard 整理

https://blog.csdn.net/u014600626/article/details/53635192

监听macOS的剪贴板

https://www.jianshu.com/p/61ffee4643ab

tags:swift, macos

对剪贴板的支持是提高用户使用体验的一个重要因素。经常,我们的应用需要监听剪贴板的内容变化,并做出相应的反应。

在iOS上这个目的可以直接通过订阅 UIPasteboardChangedNotification 来完成,而在macOS上,苹果没有提供现成的 API。

不过,实现起来也不是很麻烦。我们可以写一个剪切板的监听器,实现这个目标。

监听器实现

import AppKit

class Clipboard {
  typealias Hook = (String) -> Void

  private let pasteboard = NSPasteboard.general
  private let timerInterval = 1.0

  private var changeCount: Int
  private var hooks: [Hook]

  init() {
    changeCount = pasteboard.changeCount
    hooks = []
  }

  func onNewCopy(_ hook: @escaping Hook) {
    hooks.append(hook)
  }

  func startListening() {
    Timer.scheduledTimer(timeInterval: timerInterval,
                         target: self,
                         selector: #selector(checkForChangesInPasteboard),
                         userInfo: nil,
                         repeats: true)
  }

  func copy(_ string: String) {
    pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
    pasteboard.setString(string, forType: NSPasteboard.PasteboardType.string)
  }

  @objc
  func checkForChangesInPasteboard() {
    guard pasteboard.changeCount != changeCount else {
      return
    }

    if let lastItem = pasteboard.string(forType: NSPasteboard.PasteboardType.string) {
      for hook in hooks {
        hook(lastItem)
      }
    }

    changeCount = pasteboard.changeCount
  }
}

使用

需要在应用启动的时候创建监听器,并启动之。可以在appdelegate中完成:

class AppDelegate: NSObject, NSApplicationDelegate {
    
   let clipboard = Clipboard()
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
  clipboard.startListening()
  clipboard.onNewCopy { (content) in
            print(content)
    }
}

这样只要用户复制了文本到剪切板中, onNewCopy 就会自动触发,打印出复制的内容了。

以上的实现只监听了String 类型,如果需要监听其他类型也可以简单改造之。



作者:MarkNote
链接:https://www.jianshu.com/p/61ffee4643ab
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值