如何在 macOS 应用程序中暂时禁用键盘输入?

问题描述 投票:0回答:1

我正在使用 Swift 和 Flutter 开发 macOS 应用程序,我需要创建一个功能来暂时禁用键盘输入,类似于“键盘清理工具”。我尝试了多种方法,但尚未找到可行的解决方案。

import Cocoa
import FlutterMacOS

class KeyboardServicePlugin: NSObject, FlutterPlugin {
    private var isBlockingKeyboard: Bool = false

    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(
            name: "com.example.app/keyboard_service",
            binaryMessenger: registrar.messenger
        )
        let instance = KeyboardServicePlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }

    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        switch call.method {
            case "disableKeyboard":
                disableKeyboard()
                result(nil)
            case "enableKeyboard":
                enableKeyboard()
                result(nil)
            case "getKeyboardStatus":
                result(isBlockingKeyboard)
            case "toggleKeyboard":
                toggleKeyboard()
                result(nil)
            default:
                result(FlutterMethodNotImplemented)
        }
    }

    private func disableKeyboard() {
        // I'm not sure how to disable keyboard input in macOS
    }

    private func enableKeyboard() {
         // I'm not sure how to enable keyboard input in macOS
    }

    private func toggleKeyboard() {
        if isBlockingKeyboard {
            enableKeyboard()
        } else {
            disableKeyboard()
        }
    }
}

该解决方案应该能够在我的应用程序中以编程方式阻止和取消阻止键盘输入。

swift flutter macos keyboard
1个回答
0
投票

您可以在该项目源代码中找到所需的解决方案:

https://github.com/pqrs-org/Karabiner-Elements

因为这个开源应用程序还具有禁用键盘功能:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.