English 中文(简体)
为NSText 外地寻找关键活动
原标题:Get keyDown event for an NSTextField

In xcode last version, I am trying to get the keyDown event of an NSTextField. However, despite following multiple tutorials on the internet (delegates, controllers...), I still can t receive it.

这对我来说是多么容易的?

感谢!

问题回答

I got sick of all the non answers to do it some other way people so I put my nose down and figured out a way to make this work. This isn t using keydown event directly but it is using the keydown in the block. And the behavior is exactly what I wanted.

案文领域

页: 1

@interface LQRestrictedInputTextField : NSTextField 

页: 1

成为首批反应者,举办地方活动

static id eventMonitor = nil;

- (BOOL)becomeFirstResponder {
    BOOL okToChange = [super becomeFirstResponder];
    if (okToChange) {
        [self setKeyboardFocusRingNeedsDisplayInRect: [self bounds]];

        if (!eventMonitor) {
            eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event) {

                NSString *characters = [event characters];
                unichar character = [characters characterAtIndex:0];
                NSString *characterString=[NSString stringWithFormat:@"%c",character];

                NSArray *validNonAlphaNumericArray = @[@" ",@"(",@")",@"[",@"]",@":",@";",@" ",@""",@".",@"<",@">",@",",@"{",@"}",@"|",@"=",@"+",@"-",@"_",@"?",@"#",
                                                   @(NSDownArrowFunctionKey),@(NSUpArrowFunctionKey),@(NSLeftArrowFunctionKey),@(NSRightArrowFunctionKey)];

                if([[NSCharacterSet alphanumericCharacterSet] characterIsMember:character] || character == NSCarriageReturnCharacter || character == NSTabCharacter || character == NSDeleteCharacter || [validNonAlphaNumericArray containsObject:characterString ] ) { //[NSCharacterSet alphanumericCharacterSet]
                }  else {
                    NSBeep();
                    event=nil;
                }
                return event;
            } ];

        }
    }
    NSLog(@"become first responder");
    return okToChange;
}

一俟文本现场编辑结束活动

另外,如果你重新使用ARC I,就会发现你可能需要将描述的文字概略交给地狱。 我的先令和价值被保留。 在没有ns语的情况下,我必须把指向斯特鲁韦的通知标语保留下来,以免其被释放。

-(void) textDidEndEditing:(NSNotification *)notification {
    [NSEvent removeMonitor:eventMonitor];
    eventMonitor = nil;

    NSTextView *textView=[notification object];
    self.stringValue=textView.string;
}

你们可以将NStext Field分类,并使用NSText Field的钥匙。

页: 1 h

@interface MyTextField : NSTextField <NSTextFieldDelegate>

页: 1

-(void)keyUp:(NSEvent *)theEvent {
    NSLog(@"Pressed key in NStextField!");
}




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签