How can I take full control of the Option/Alt key from within a PyQt application?

203 views Asked by At

I'm creating a text editor in PyQt. I want to handle all keyboard inputs entirely on my own, so I've reimplemented event handlers in my QTextEdit class like so:

class GorgTextEdit(QtGui.QTextEdit):                                                   
# customized QTextEdit class with reimplemented event handles

    gorg_key_press_signal = pyqtSignal(str, int, QtGui.QWidget)
    gorg_key_release_signal = pyqtSignal(str, int, QtGui.QWidget)

    def __init__(self):

        super(GorgTextEdit, self).__init__()

    def keyPressEvent(self, e):
        self.gorg_key_press_signal.emit("p", e.key(), self)

    def keyReleaseEvent(self, e):
        self.gorg_key_release_signal.emit("r", e.key(), self)

Basically I'm sending a signal from every key event in a GorgTextEdit directly to a Commander object, which then calls functions based on key sequences, some of which insert plain text back into the GorgTextEdit object by calling its methods directly. I've written commands for inserting plain text and for handling the shift and delete keys which are called by the Commander object, with the destination widget (the source of the original key sequences) passed into those functions as an argument.

The problem is whenever I hold down the option key and then hit a text character, it immediately inserts an esoteric unicode character instead of waiting for action from my Commander. I want my option key to basically be mapped to Meta - I'm hoping to implement M-x as an Emacs style command invocation. This doesn't work if the option key causes OSX to bypass my code and insert characters directly into whatever text window has focus without permission. Right now M-x (option x) inserts , the approximation character, which is nothing that I've told it to do.

Is there some way of overwriting these OSX text insertion features from within Python?

1

There are 1 answers

0
The Compiler On

You might want to use an event filter (either on the widget, or even a global one).