Swift NSEvent不起作用 [英] Swift NSEvent not working

查看:113
本文介绍了Swift NSEvent不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的简单 AppDelegate.swift 类.

//  AppDelegate.swift

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        NSEvent.addGlobalMonitorForEventsMatchingMask(NSEventMask.KeyDownMask, handler: keyDown);
    }

    func keyDown(event:NSEvent!) {
        print("key down is \(event.keyCode)");
    }

}

为什么不打印按键事件?我想念什么? 我已经尝试过搜索,但是找不到.

How come key down events are not being printed? What am I missing? I've tried searching around but can't figure it out.

推荐答案

根据addGlobalMonitorForEventsMatchingMask:handler:上的NSEvent.h:

仅在启用辅助功能的情况下,才可以监视与键有关的事件 或者您的应用程序是否受访问权限的信任(请参阅 AXUIElement.h中的AXIsProcessTrusted).请注意,您的处理程序不会 被发送到您自己的应用程序的事件被调用.

Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted in AXUIElement.h). Note that your handler will not be called for events that are sent to your own application.

这篇关于Swift NSEvent不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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