Cocoa: Handle keyboard dead keys correctly
authorBradley T. Hughes <bradley.hughes@nokia.com>
Tue, 22 May 2012 11:41:50 +0000 (13:41 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 23 May 2012 12:18:34 +0000 (14:18 +0200)
Some keyboard layouts have physical dead keys (like the ¨ key on a
Norwegian keyboard). These do not send any text, so we should not use
[NSString characterAtIndex:0] if the string is empty. When encountering
an empty [NSEvent character] string, use Qt::Key_unknown and
QChar::ReplacementCharacter.

Change-Id: I7281aa9ea6005341c0dcfa5900bfe601e4eac6a9
Reviewed-by: Morten Johan Sørvig <morten.sorvig@nokia.com>
src/plugins/platforms/cocoa/qnsview.mm

index 09ba1c5..e0fc678 100644 (file)
@@ -612,8 +612,18 @@ static QTouchDevice *touchDevice = 0;
     ulong timestamp = [nsevent timestamp] * 1000;
     Qt::KeyboardModifiers modifiers = [self convertKeyModifiers:[nsevent modifierFlags]];
     NSString *charactersIgnoringModifiers = [nsevent charactersIgnoringModifiers];
-    QChar ch([charactersIgnoringModifiers characterAtIndex:0]);
-    int keyCode = [self convertKeyCode:ch];
+
+    QChar ch;
+    int keyCode;
+    if ([charactersIgnoringModifiers length] > 0) {
+        // convert the first character into a key code
+        ch = QChar([charactersIgnoringModifiers characterAtIndex:0]);
+        keyCode = [self convertKeyCode:ch];
+    } else {
+        // might be a dead key
+        ch = QChar::ReplacementCharacter;
+        keyCode = Qt::Key_unknown;
+    }
 
     // we will send a key event unless the input method sets m_sendKeyEvent to false
     m_sendKeyEvent = true;