获取 NSTextField 的 keyDown 事件

发布于 2024-12-27 02:56:04 字数 130 浏览 3 评论 0原文

在 xcode 最新版本中,我试图获取 NSTextField 的 keyDown 事件。 然而,尽管遵循了互联网上的多个教程(代表、控制器......),我仍然无法收到它。

对我来说有什么简单的提示吗?

谢谢 !

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.

Any easy hint for me ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长发绾君心 2025-01-03 02:56:04

我厌倦了人们以其他方式做这件事的所有无答案的人,所以我埋头苦干,想出了一种方法来完成这项工作。这不是直接使用 keydown 事件,而是使用块中的 keydown。这种行为正是我想要的。

子类化文本字段

.h

@interface LQRestrictedInputTextField : NSTextField 

.m

在成为第一响应者设置中,本地事件

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,我注意到您可能需要将 textview 字符串分配给 stringValue。我对 stringValue 进行了 nslog,并且该值被保留。如果没有 nslog,我必须将通知对象字符串分配给 stringValue 以防止其被释放。

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

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

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.

Subclass the text field

.h

@interface LQRestrictedInputTextField : NSTextField 

.m

In the become first responder setup a local event

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;
}

remove the event once the textfield editing ends

Also if you're using ARC I noticed you might need to assign the textview string to the stringValue. I nslog'd the stringValue and the value was retained. Without the nslog I had to assign the notification object string to the stringValue to keep it from getting released.

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

    NSTextView *textView=[notification object];
    self.stringValue=textView.string;
}
把时间冻结 2025-01-03 02:56:04

您可以继承 NStextField 并使用适用于 NSTextField 的 keyUp 。

英寸 .h

@interface MyTextField : NSTextField <NSTextFieldDelegate>

英寸 .m

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

You can subclass NStextField and use keyUp that works for NSTextField.

in .h

@interface MyTextField : NSTextField <NSTextFieldDelegate>

in .m

-(void)keyUp:(NSEvent *)theEvent {
    NSLog(@"Pressed key in NStextField!");
}
﹂绝世的画 2025-01-03 02:56:04

像这样将 UITextFieldDelegate 添加到您的 .h

@interface ViewController :  UIViewController <UITextFieldDelegate> {

然后您可以使用它来检测文本字段中的每个按键

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

返回 YES 以允许按下的字符插入到字段中,但您可以在此处添加您需要的任何代码。

Add UITextFieldDelegate to your .h like this

@interface ViewController :  UIViewController <UITextFieldDelegate> {

Then you can use this to detect every key press in a text field

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

Return YES to allow the character that was pressed to be inserted into the field but you can add whatever code you need in here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文