Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Bad simulated keyboard event received in Qt application under Mac OS
I'm developing a Mac OS application, an input method, which purpose is to send text to other targeted application. I'm using this portion of code to do that:
CGEventRef vKeyDown = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, true);
// Get the unicode String to use to send
int stringLength = [string length];
UniChar *unicodeString = (UniChar )malloc(stringLengthsizeof(UniChar));
NSRange range = NSMakeRange(0, stringLength);
[string getCharacters:unicodeString range:range];
CGEventKeyboardSetUnicodeString(vKeyDown, stringLength, unicodeString);
It's working fine appart from Qt based application. Indeed according to Azerty or Querty keyboard layout the characters received by the Qt applications are always "a" or "q". I'm wondering if this could be a bug in Qt not well supporting simulated keyboard event.
I also posted a request on Apple forums and had a reply telling me that Qt is not managing the keyboard event as other native applications.
In fact as Apple documentation said the CGEventKeyboardSetUnicodeString(…, …, unicodeString) method call can lead to different behavior once the event is posted: "Note that application frameworks may ignore the Unicode string in a keyboard event and do their own translation based on the virtual Keycode and perceived event state.". So I assume this is what happens with Qt and I went to use InputMethodKit framework as a "filter" between my application sending the keyboard event and the target application receiving it. Indeed having my own Input Method installed let me managed the keyboard event as requested propagating it to a Qt application. In order to manage input method Text Input Source Services can be use so it is transparent for the user.
Indeed, it does not do the trick completely... After deep testing we found that when targeting Qt application built against Qt framework under 4.7 it does not work.
Does anyone know why CGEventKeyboardSetUnicodeStringis not taken into account by Qt event monitoring loop ?