NSTextfield + NSMenu and first responder











up vote
4
down vote

favorite
1












I'm trying to implement my own autocomplemention system (result is pull from an sqlite database)



I've set up a NSTextField and the appropriate delegate. Each time the text in the NSTextField change, it call - (void)controlTextDidChange:(NSNotification *)aNotification method



It work fine, in this method I build a menu programtically and finally I call/show it with that code:



 NSRect frame = [address frame];
NSPoint menuOrigin = [[address superview] convertPoint:NSMakePoint(frame.origin.x, frame.origin.y+frame.size.height-25)
toView:nil];

NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
location:menuOrigin
modifierFlags:NSLeftMouseDownMask // 0x100
timestamp:0
windowNumber:[[address window] windowNumber]
context:[[address window] graphicsContext]
eventNumber:0
clickCount:1
pressure:1];
[NSMenu popUpContextMenu:menu withEvent:event forView:address];


Where address is my NSTextField and menu is my NSMenu.
The problem is that the menu take the focus, so you can type only 1 letter in the text field and then you can't type text anymore because the menu is now the first responder.



My questions is how to show the menu and keep the text field as first responder so you can type in it while the menu is reloaded at each text change in the field.



It should be like in Safari or chrome address bar in fact for example.










share|improve this question




























    up vote
    4
    down vote

    favorite
    1












    I'm trying to implement my own autocomplemention system (result is pull from an sqlite database)



    I've set up a NSTextField and the appropriate delegate. Each time the text in the NSTextField change, it call - (void)controlTextDidChange:(NSNotification *)aNotification method



    It work fine, in this method I build a menu programtically and finally I call/show it with that code:



     NSRect frame = [address frame];
    NSPoint menuOrigin = [[address superview] convertPoint:NSMakePoint(frame.origin.x, frame.origin.y+frame.size.height-25)
    toView:nil];

    NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
    location:menuOrigin
    modifierFlags:NSLeftMouseDownMask // 0x100
    timestamp:0
    windowNumber:[[address window] windowNumber]
    context:[[address window] graphicsContext]
    eventNumber:0
    clickCount:1
    pressure:1];
    [NSMenu popUpContextMenu:menu withEvent:event forView:address];


    Where address is my NSTextField and menu is my NSMenu.
    The problem is that the menu take the focus, so you can type only 1 letter in the text field and then you can't type text anymore because the menu is now the first responder.



    My questions is how to show the menu and keep the text field as first responder so you can type in it while the menu is reloaded at each text change in the field.



    It should be like in Safari or chrome address bar in fact for example.










    share|improve this question


























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      I'm trying to implement my own autocomplemention system (result is pull from an sqlite database)



      I've set up a NSTextField and the appropriate delegate. Each time the text in the NSTextField change, it call - (void)controlTextDidChange:(NSNotification *)aNotification method



      It work fine, in this method I build a menu programtically and finally I call/show it with that code:



       NSRect frame = [address frame];
      NSPoint menuOrigin = [[address superview] convertPoint:NSMakePoint(frame.origin.x, frame.origin.y+frame.size.height-25)
      toView:nil];

      NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
      location:menuOrigin
      modifierFlags:NSLeftMouseDownMask // 0x100
      timestamp:0
      windowNumber:[[address window] windowNumber]
      context:[[address window] graphicsContext]
      eventNumber:0
      clickCount:1
      pressure:1];
      [NSMenu popUpContextMenu:menu withEvent:event forView:address];


      Where address is my NSTextField and menu is my NSMenu.
      The problem is that the menu take the focus, so you can type only 1 letter in the text field and then you can't type text anymore because the menu is now the first responder.



      My questions is how to show the menu and keep the text field as first responder so you can type in it while the menu is reloaded at each text change in the field.



      It should be like in Safari or chrome address bar in fact for example.










      share|improve this question















      I'm trying to implement my own autocomplemention system (result is pull from an sqlite database)



      I've set up a NSTextField and the appropriate delegate. Each time the text in the NSTextField change, it call - (void)controlTextDidChange:(NSNotification *)aNotification method



      It work fine, in this method I build a menu programtically and finally I call/show it with that code:



       NSRect frame = [address frame];
      NSPoint menuOrigin = [[address superview] convertPoint:NSMakePoint(frame.origin.x, frame.origin.y+frame.size.height-25)
      toView:nil];

      NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown
      location:menuOrigin
      modifierFlags:NSLeftMouseDownMask // 0x100
      timestamp:0
      windowNumber:[[address window] windowNumber]
      context:[[address window] graphicsContext]
      eventNumber:0
      clickCount:1
      pressure:1];
      [NSMenu popUpContextMenu:menu withEvent:event forView:address];


      Where address is my NSTextField and menu is my NSMenu.
      The problem is that the menu take the focus, so you can type only 1 letter in the text field and then you can't type text anymore because the menu is now the first responder.



      My questions is how to show the menu and keep the text field as first responder so you can type in it while the menu is reloaded at each text change in the field.



      It should be like in Safari or chrome address bar in fact for example.







      objective-c cocoa autocomplete nstextfield nsmenu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 16:38









      Marek H

      2,2971222




      2,2971222










      asked May 20 '11 at 10:19









      Dimillian

      2,02432346




      2,02432346
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          I don't believe this is possible with NSMenu. NSMenu implementation is controlled by the system at a quite low level, and it is designed to take keyboard focus. What you need is to create your own view, or window, that looks somewhat like a menu, but is not using NSMenu. Notice for example that that the menu in the chrome address bar does not look like a standard NSMenu. You need to create a view that will appear and draw, and receive callbacks or notifications to update as the user types, but will not take keyboard focus. There are methods on NSView (NSResponder actually) that control whether a view accepts first responder status.






          share|improve this answer





















          • I already trashed the NSMenu and go for a TableView view.The problem is that if I give the firstResponder to the TableView I will not be able to type anymore chars into the text field. The solution would be to forward only the arrow up and down key to the TableView and do the appropriate actions.
            – Dimillian
            May 22 '11 at 9:04




















          up vote
          1
          down vote













          As mgorbach stated it is not really possible with NSMenu.
          I've switched to NSTableView and customized my textfield. The textfield forward the up and down arrow to the Table view and that work fine !






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6070492%2fnstextfield-nsmenu-and-first-responder%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            I don't believe this is possible with NSMenu. NSMenu implementation is controlled by the system at a quite low level, and it is designed to take keyboard focus. What you need is to create your own view, or window, that looks somewhat like a menu, but is not using NSMenu. Notice for example that that the menu in the chrome address bar does not look like a standard NSMenu. You need to create a view that will appear and draw, and receive callbacks or notifications to update as the user types, but will not take keyboard focus. There are methods on NSView (NSResponder actually) that control whether a view accepts first responder status.






            share|improve this answer





















            • I already trashed the NSMenu and go for a TableView view.The problem is that if I give the firstResponder to the TableView I will not be able to type anymore chars into the text field. The solution would be to forward only the arrow up and down key to the TableView and do the appropriate actions.
              – Dimillian
              May 22 '11 at 9:04

















            up vote
            4
            down vote



            accepted










            I don't believe this is possible with NSMenu. NSMenu implementation is controlled by the system at a quite low level, and it is designed to take keyboard focus. What you need is to create your own view, or window, that looks somewhat like a menu, but is not using NSMenu. Notice for example that that the menu in the chrome address bar does not look like a standard NSMenu. You need to create a view that will appear and draw, and receive callbacks or notifications to update as the user types, but will not take keyboard focus. There are methods on NSView (NSResponder actually) that control whether a view accepts first responder status.






            share|improve this answer





















            • I already trashed the NSMenu and go for a TableView view.The problem is that if I give the firstResponder to the TableView I will not be able to type anymore chars into the text field. The solution would be to forward only the arrow up and down key to the TableView and do the appropriate actions.
              – Dimillian
              May 22 '11 at 9:04















            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            I don't believe this is possible with NSMenu. NSMenu implementation is controlled by the system at a quite low level, and it is designed to take keyboard focus. What you need is to create your own view, or window, that looks somewhat like a menu, but is not using NSMenu. Notice for example that that the menu in the chrome address bar does not look like a standard NSMenu. You need to create a view that will appear and draw, and receive callbacks or notifications to update as the user types, but will not take keyboard focus. There are methods on NSView (NSResponder actually) that control whether a view accepts first responder status.






            share|improve this answer












            I don't believe this is possible with NSMenu. NSMenu implementation is controlled by the system at a quite low level, and it is designed to take keyboard focus. What you need is to create your own view, or window, that looks somewhat like a menu, but is not using NSMenu. Notice for example that that the menu in the chrome address bar does not look like a standard NSMenu. You need to create a view that will appear and draw, and receive callbacks or notifications to update as the user types, but will not take keyboard focus. There are methods on NSView (NSResponder actually) that control whether a view accepts first responder status.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 21 '11 at 9:12









            Michael Gorbach

            47923




            47923












            • I already trashed the NSMenu and go for a TableView view.The problem is that if I give the firstResponder to the TableView I will not be able to type anymore chars into the text field. The solution would be to forward only the arrow up and down key to the TableView and do the appropriate actions.
              – Dimillian
              May 22 '11 at 9:04




















            • I already trashed the NSMenu and go for a TableView view.The problem is that if I give the firstResponder to the TableView I will not be able to type anymore chars into the text field. The solution would be to forward only the arrow up and down key to the TableView and do the appropriate actions.
              – Dimillian
              May 22 '11 at 9:04


















            I already trashed the NSMenu and go for a TableView view.The problem is that if I give the firstResponder to the TableView I will not be able to type anymore chars into the text field. The solution would be to forward only the arrow up and down key to the TableView and do the appropriate actions.
            – Dimillian
            May 22 '11 at 9:04






            I already trashed the NSMenu and go for a TableView view.The problem is that if I give the firstResponder to the TableView I will not be able to type anymore chars into the text field. The solution would be to forward only the arrow up and down key to the TableView and do the appropriate actions.
            – Dimillian
            May 22 '11 at 9:04














            up vote
            1
            down vote













            As mgorbach stated it is not really possible with NSMenu.
            I've switched to NSTableView and customized my textfield. The textfield forward the up and down arrow to the Table view and that work fine !






            share|improve this answer

























              up vote
              1
              down vote













              As mgorbach stated it is not really possible with NSMenu.
              I've switched to NSTableView and customized my textfield. The textfield forward the up and down arrow to the Table view and that work fine !






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                As mgorbach stated it is not really possible with NSMenu.
                I've switched to NSTableView and customized my textfield. The textfield forward the up and down arrow to the Table view and that work fine !






                share|improve this answer












                As mgorbach stated it is not really possible with NSMenu.
                I've switched to NSTableView and customized my textfield. The textfield forward the up and down arrow to the Table view and that work fine !







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 23 '11 at 14:40









                Dimillian

                2,02432346




                2,02432346






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6070492%2fnstextfield-nsmenu-and-first-responder%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)