From 8f90dbce9912890a04409f39cd3617d0b61eb21e Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Thu, 10 Nov 2011 00:52:38 +0000 Subject: [PATCH] Allow almost anything as a key equivalent unless a text field is the first responder. After testing in Cocoa, even unexpected keys such as tab can be used when a text field isn't selected. Some keys such as the arrow keys and escape act like key equivalents even with a text field active. --- AppKit/CPEvent.j | 15 ++++++--------- Tests/Manual/KeyEquivalents/AppController.j | 12 ++++++++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/AppKit/CPEvent.j b/AppKit/CPEvent.j index dc3f918a0..80d931f76 100644 --- a/AppKit/CPEvent.j +++ b/AppKit/CPEvent.j @@ -571,10 +571,12 @@ var _CPEventPeriodicEventPeriod = 0, if (_modifierFlags & (CPCommandKeyMask | CPControlKeyMask)) return YES; - // Cocoa does not consider space, backspace, or escape a key equivalent - // if the first responder is a text field (presumably a subclass of NSText). + // Cocoa allows almost any key as a key equivalent unless the first responder is a + // text field (presumably a subclass of NSText.) var firstResponderIsText = [[_window firstResponder] isKindOfClass:[CPTextField class]]; + // Some keys are accepted as key equivalents even if the first responder is a text + // field. for (var i = 0; i < characterCount; i++) { var c = _characters.charAt(i); @@ -583,18 +585,13 @@ var _CPEventPeriodicEventPeriod = 0, c === CPEnterCharacter || c === CPNewlineCharacter || c === CPCarriageReturnCharacter || - c === CPEscapeFunctionKey || - (!firstResponderIsText && - (c === CPSpaceFunctionKey || - c === CPDeleteCharacter || - c === CPBackspaceCharacter))) + c === CPEscapeFunctionKey) { return YES; } } - // FIXME: More cases? - return NO; + return !firstResponderIsText; } /*! diff --git a/Tests/Manual/KeyEquivalents/AppController.j b/Tests/Manual/KeyEquivalents/AppController.j index 2ce94fea8..f32bddf20 100644 --- a/Tests/Manual/KeyEquivalents/AppController.j +++ b/Tests/Manual/KeyEquivalents/AppController.j @@ -60,7 +60,6 @@ [contentView addSubview:button]; } - var label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()]; [label setStringValue:@"Press the appropriate key on the keyboard for each button and verify that it reacts."]; [label setFont:[CPFont boldSystemFontOfSize:14.0]]; @@ -86,7 +85,7 @@ ["down arrow", CPDownArrowFunctionKey], ["home", CPHomeFunctionKey], ["end", CPEndFunctionKey] - ]; + ]; for (var i = 0, buttonsWide = 6, yOffset = 0; i < functionKeysToTest.length; i++) { @@ -102,6 +101,15 @@ [contentView addSubview:button]; } + var label2 = [[CPTextField alloc] initWithFrame:CGRectMake(10, 140 + yOffset + 24, 100, 24)]; + [label2 setStringValue:@"If a text field is the first responder, some key equivalents are ignored."]; + [label2 sizeToFit]; + [contentView addSubview:label2]; + + var textField = [CPTextField textFieldWithStringValue:"" placeholder:"" width:100]; + [textField setFrameOrigin:CGPointMake(10, CGRectGetMaxY([label2 frame]) + 10)]; + [contentView addSubview:textField]; + [theWindow orderFront:self]; }