Repository: matplotlib/matplotlib
File: lib/matplotlib/widgets.py (Python)

Code before:
            self.text_disp.remove()
            self.text_disp = self._make_text_disp(self.text)
            self._rendercursor()
            self._notify_change_observers()
            if key == "enter":
                self._notify_submit_observers()

    def set_val(self, val):
        newval = str(val)
        if self.text == newval:
            return
        self.text = newval
        self.text_disp.remove()
        self.text_disp = self._make_text_disp(self.text)
        self._rendercursor()
        self._notify_change_observers()
        self._notify_submit_observers()

    def _notify_change_observers(self):
        if self.eventson:
            for cid, func in self.change_observers.items():
                func(self.text)

    def begin_typing(self, x):
        self.capturekeystrokes = True
        if rcParams['toolbar'] != 'toolmanager':
            # disable command keys so that the user can type without
            # command keys causing figure to be saved, etc
            self.reset_params = {}
            for key in self.params_to_disable:
                self.reset_params[key] = rcParams[key]
                rcParams[key] = []
        else:
            self.ax.figure.canvas.manager.toolmanager.keypresslock(self)

    def stop_typing(self):
        notifysubmit = False
        # because _notify_submit_users might throw an error in the
        # user's code, we only want to call it once we've already done
        # our cleanup.
        if self.capturekeystrokes:
            if rcParams['toolbar'] != 'toolmanager':
                # since the user is no longer typing,
                # reactivate the standard command keys
                for key in self.params_to_disable:
                    rcParams[key] = self.reset_params[key]
            else:
                toolmanager = self.ax.figure.canvas.manager.toolmanager
                toolmanager.keypresslock.release(self)
            notifysubmit = True
        self.capturekeystrokes = False

Review comment (bug):
Should rather check whether the figure manager's key_press_handler_id is not None, otherwise this check becomes wrong if the value of the rcParam is changed between the time the canvas is created and the time begin_typing is called.

Same below.

Code after:
            self.text_disp.remove()
            self.text_disp = self._make_text_disp(self.text)
            self._rendercursor()
            self._notify_change_observers()
            if key == "enter":
                self._notify_submit_observers()

    def set_val(self, val):
        newval = str(val)
        if self.text == newval:
            return
        self.text = newval
        self.text_disp.remove()
        self.text_disp = self._make_text_disp(self.text)
        self._rendercursor()
        self._notify_change_observers()
        self._notify_submit_observers()

    def _notify_change_observers(self):
        if self.eventson:
            for cid, func in self.change_observers.items():
                func(self.text)

    def begin_typing(self, x):
        self.capturekeystrokes = True
        # Check for toolmanager handling the keypress
        if self.ax.figure.canvas.manager.key_press_handler_id is not None:
            # disable command keys so that the user can type without
            # command keys causing figure to be saved, etc
            self.reset_params = {}
            for key in self.params_to_disable:
                self.reset_params[key] = rcParams[key]
                rcParams[key] = []
        else:
            self.ax.figure.canvas.manager.toolmanager.keypresslock(self)

    def stop_typing(self):
        notifysubmit = False
        # because _notify_submit_users might throw an error in the
        # user's code, we only want to call it once we've already done
        # our cleanup.
        if self.capturekeystrokes:
            # Check for toolmanager handling the keypress
            if self.ax.figure.canvas.manager.key_press_handler_id is not None:
                # since the user is no longer typing,
                # reactivate the standard command keys
                for key in self.params_to_disable:
                    rcParams[key] = self.reset_params[key]
            else:
                toolmanager = self.ax.figure.canvas.manager.toolmanager
                toolmanager.keypresslock.release(self)