[Interest] [osx] cmd-period mapping

John Weeks john at wavemetrics.com
Thu Sep 4 00:17:19 CEST 2014


Tim-

On Macintosh, cmd-. is the cancel signal, like ctrl-break in Windows, or Ctrl-C for Linux. In fact, when building with Qt and Cocoa, you don't get it at all because Cocoa eats it up. We went to somewhat extreme lengths to intercept cmd-. so that we can use it to cancel user programs (our application includes a user programming language).

You really should NOT use cmd-. as anything other than a cancel signal.

Here is what we did to get access to cmd-. key events:

In a .mm file:

 bool WMNSEventIsCmdPeriodKeyDown(void * event)
{
	NSEvent * nsevent = reinterpret_cast<NSEvent *>(event);
	if ([nsevent type] == NSKeyDown)
	{
		if ([nsevent modifierFlags] & NSCommandKeyMask)
		{
			NSString * chars = [nsevent charactersIgnoringModifiers];
			return [chars characterAtIndex:0] == 0x2E;					// Unicode encoding for period
		}
	}
	return false;
}

bool WMNSEventIsCmdPeriodKeyUp(void * event)
{
	NSEvent * nsevent = reinterpret_cast<NSEvent *>(event);
	if ([nsevent type] == NSKeyUp)
	{
		if ([nsevent modifierFlags] & NSCommandKeyMask)
		{
			NSString * chars = [nsevent charactersIgnoringModifiers];
			return [chars characterAtIndex:0] == 0x2E;
		}
	}
	return false;
}

Then, with Qt 5, we define a macintosh event filter class:

 		class macEventFilterClass : public QObject, public QAbstractNativeEventFilter
		{
			Q_OBJECT

			public:
				bool				nativeEventFilter(const QByteArray&, void* message, long*);

			signals:
				void				caughtCancelEvent(bool isDown);
		};

 bool macEventFilterClass::nativeEventFilter(const QByteArray &, void * message, long *)
{
	bool result = false;

	if (WMNSEventIsCmdPeriodKeyDown(reinterpret_cast<void *>(message)))
	{
		emit caughtCancelEvent(true);
		result = true;
	}
	else if (WMNSEventIsCmdPeriodKeyUp(reinterpret_cast<void *>(message)))
	{
		emit caughtCancelEvent(false);
		result = true;
	}

	return result;
}

and in our QApplication-derived class constructor, we install it on the application:

 			_macEventFilterObject = new macEventFilterClass;
			connect(_macEventFilterObject, SIGNAL(caughtCancelEvent(bool)), this, SLOT(_caughtMacCancelEvent(bool)));
			installNativeEventFilter(_macEventFilterObject);


On Sep 3, 2014, at 2:38 PM, Tim Blechmann <tim at klingt.org> wrote:

> hi all,
> 
> i wonder, does ctrl-. (as in cmd-.) have any special treatment in qt5?
> i'd like to use it a shortcut of an action which is available from the
> menu bar, but it does not get triggered. otoh, in QFileDialog and the
> like get seem to trigger the "cancel" button.
> 
> any idea how use it for application-defined shortcuts?
> 
> thnx,
> tim
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

Regards,
John Weeks

WaveMetrics, Inc.
Phone (503) 620-3001
Fax   (503) 620-6754
email   support at WaveMetrics.com




More information about the Interest mailing list