I am working on a macOS application using Swift and Flutter, and I need to create a functionality to temporarily disable the keyboard input, similar to a "keyboard clean tool". I have attempted several approaches but haven't found a viable solution yet.
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()
}
}
}
The solution should be able to block and unblock the keyboard input programmatically within my application.
you can find needed solution in this project source code:
https://github.com/pqrs-org/Karabiner-Elements
as this open-source app is also have a disable keyboard feature: