How do I take action based on the user pressing 'enter' from within a Matlab text box?

Posted on 2009-02-23
Last Modified: 2016-02-10
I would like users to be able to enter stuff in a TextBox and hit enter, and have action taken - BUT - matlab's TextBox_Callback is called on either hitting enter OR changing focus. I can't see any way of verifying that they really pressed enter, rather than just clicking outside of the TextBox. Is there any way to either take action when they hit enter (and ONLY when they hit enter) or to find the value of the last key pressed when focus was in the TextBox?
Question by:sgood3
    LVL 15

    Accepted Solution

    I tried and failed too.

    The problem is that eventdata structure, which suppose to hold data on key pressed, is not (hopefully yet) implemented for Callback function. So there is no way to check, if Callback function was run by pressing enter or changing focus. Eventdata however is implemented for TextBox_KeyPressFcn, but when user press enter, Callback called first. You may not implement it and do all assignments in KeyPressFcn:
    But what to do with the string left in TextBox after changing focus? Just leave it there without validation (not good, right?) or restore previous value (no function on focus changed)?

    So, my conclusion is it's not possible with current implementation.

    You may do some weird staff like controlling mouse clicks in figure and checking status of all textboxes, but ask yourself, does it worth it?


    Author Closing Comment

    Hard to believe that a simple thing would be impossible, but hey, thanks at least for letting me go on to other things :)

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now