[Interest] Key_Cancel on Macintosh

John Weeks john at wavemetrics.com
Tue Mar 20 21:09:13 CET 2012


On 20-Mar-2012, at 10:53 AM, Nikos Chantziaras wrote:

> Maybe this is relevant:
> 
> http://qt-project.org/doc/qt-4.8/qapplication.html#macEventFilter


OK- that was a good lead. It seems that at that point the Cmd-period key event *IS* available. Here is my code:

In my subclass of QApplication:

#ifdef MACIGOR
bool IgorAppObject::macEventFilter(EventHandlerCallRef caller, EventRef event)
{
	if (WMNSEventIsCmdPeriod(reinterpret_cast<void *>(event)))
		emit cancelEvent();
	
	return false;
}
#endif

In a .mm (objective C++) file:

bool
WMNSEventIsCmdPeriod(void *event)
{
	NSEvent * nsevent = reinterpret_cast<NSEvent *>(event);
	if ([nsevent type] == NSKeyUp) // presumably, if we see the key up it was preceded by a key down. Action should take place on key up.
	{
		std::cout << "NSEvent keydown event" << std::endl;
		if ([nsevent modifierFlags] & NSCommandKeyMask)
		{
			NSString * chars = [nsevent charactersIgnoringModifiers];
			return [chars characterAtIndex:0] == 0x2E; // Unicode encoding for period
		}
	}
	return false;
}

The header containing the prototype for this function declares it to be extern "C". It is my understanding that that is required because Objective C++ mangles names differently than regular C++.

In addition to the above, I have regular QApplication::notify event filter looking for QKeyEvents that match Qt::Key_Cancel. That will provide the same functionality on platforms where Qt allows this access (empirically, Windows works this way).

Seems like since QApplication::macEventFilter() can see the relevant key events, it would be possible for Qt on Cocoa/Macintosh could just Do the Right Thing. But maybe not...

-John Weeks

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120320/01050627/attachment.html>


More information about the Interest mailing list