PyObjC InputMethodKit Framework

12.1 · active · verified Tue Apr 14

PyObjC-framework-InputMethodKit provides Python wrappers for Apple's InputMethodKit framework on macOS, allowing developers to create custom input methods using Python. It is part of the larger PyObjC project, which acts as a bridge between Python and Objective-C. The library is actively maintained with frequent releases, often synchronized with new macOS SDK versions and Python language updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import the `InputMethodKit` framework and instantiate its core classes. Note that building a functional macOS Input Method requires additional setup, including creating an application bundle (e.g., via `py2app`) and configuring a proper `Info.plist` file with specific Input Method related keys. This code snippet focuses on the PyObjC API usage, not the complete application deployment.

import InputMethodKit
import objc
from Foundation import NSObject, NSLog, NSBundle

# Define a simple input controller, subclassing IMKInputController
class MyInputController(InputMethodKit.IMKInputController):
    def init(self):
        self = objc.super(MyInputController, self).init()
        if self is None:
            return None
        NSLog("MyInputController initialized!")
        return self

    def activateServer_(self, sender):
        NSLog("Input Method activated!")
        return True

    def deactivateServer_(self, sender):
        NSLog("Input Method deactivated!")
        return True

    # In a real input method, you'd implement methods like handleEvent_, keyUp_ etc.

def main():
    # In a real Input Method, the name and bundleIdentifier are crucial
    # and typically defined in the application's Info.plist.
    # For this conceptual example, we use placeholders.
    input_method_name = "MyPyIM"
    # This bundle ID *must* match the bundle ID in your Info.plist
    # for a functional Input Method application.
    bundle_id = "com.example.MyPyIM"

    # Create an IMKServer instance.
    # The server manages client connections and dispatches events to input controllers.
    server = InputMethodKit.IMKServer.alloc().initWithName_bundleIdentifier_(
        input_method_name,
        bundle_id
    )

    NSLog("IMKServer created for name: %@, bundleIdentifier: %@", input_method_name, bundle_id)
    NSLog("\n*** IMPORTANT ***\n")
    NSLog("To create a functional macOS Input Method, this Python script must be packaged")
    NSLog("inside a .app bundle with a correctly configured Info.plist (e.g., using py2app).")
    NSLog("This quickstart only demonstrates the basic class instantiation and setup.")
    
    # A real Input Method would then typically run an AppHelper.runEventLoop()
    # from PyObjCTools to handle events and keep the application running.
    # For simplicity, we omit the blocking event loop here.

if __name__ == '__main__':
    main()

view raw JSON →