捕获全局 Keydown 事件(不仅仅是观察) [英] Capture Global Keydown Events (not simply observe)

查看:80
本文介绍了捕获全局 Keydown 事件(不仅仅是观察)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些全局热键.但是使用 addGlobalMonitorForEventsMatchingMask 我只能观察按键事件.如果我想捕获事件并阻止其他应用程序接收它怎么办?

I've created some global hotkeys. However using addGlobalMonitorForEventsMatchingMask I can only observe keydown events. What if I want to capture the event and prevent other applications from receiving it?

这是我观察按键事件的方法:

Here's what I have for observing keydown events:

    class AppDelegate: NSObject, NSApplicationDelegate {
        func applicationDidFinishLaunching(aNotification: NSNotification) {
            NSEvent.addGlobalMonitorForEventsMatchingMask(.KeyDownMask, handler: keyDown)
            NSEvent.addLocalMonitorForEventsMatchingMask(.KeyDownMask) { (event) -> NSEvent! in
                self.keyDown(event)
                return event
            }
        }
        func keyDown(event : NSEvent) {
            if event.modifierFlags.contains(.ControlKeyMask) && event.modifierFlags.contains(.AlternateKeyMask) && event.modifierFlags.contains(.CommandKeyMask) && event.keyCode == 126 {
                print("⌃⌥⌘↑ pressed")
            }
        }
    }

我不确定从哪里开始捕获 keydown 事件.任何帮助都会很棒.谢谢.

I'm not sure where to start to capture keydown events. Any help would be great. Thanks.

推荐答案

DDHotKey 可以让你捕捉keydown 事件并使用它们来触发应用程序中的功能.我最近在这篇文章中发现了它.如果您还不知道如何将 Objective-C 代码集成到 Swift 项目中,您可以找到 Apple 文档 此处.

DDHotKey allows you to capture keydown events and use them to trigger function in you app. I discovered it recently in this post. If you don't already know how to integrate Objective-C code into a Swift project, you can find the Apple documentation here.

这篇关于捕获全局 Keydown 事件(不仅仅是观察)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆