Updated 2010-07-25 15:22:42 by dkf

This page now describes several problems related to the Backspace key.

Problem 1: insert mark ("cursor") remains still as characters are deleted to its left

With a Tcl program I use the backspace key and instead of going backwards, the function stays still and I get the impression that letters are falling in a sort of hole.

How could I fix the problem?

Thanks once more teclers!

FW: To me it sounds like you're saying the insertion cursor stays at the same place in the line when you backspace. This doesn't happen for me, so it may be specific to the program you're using (perhaps alters the bindings?) If you try a simple text widget "pack [text .t]" does it still happen?

LES: My guess is that text in his widget (a Tk entry widget, I suppose) is aligned "center". So the text gets shorter on the "edges" and it looks like the cursor (the editing point) does not ever move.

Thanks gentlemen for your answers. You are both right: the backspace feels like it is centered and it stays in the same location, in the same column. I'll try to put the code you suggest FW. Where do I enter it (excuse my ignorance :-))

If I put the text widget you suggest, will it also solve the problem of the home function that returns to the beginning of the paragraph instead of going to the beginning of the line?

Thanks again for your answers.

2nd April 2005

On an editor, when pressing the backspace key, the previous character is deleted, and then the cursor is moved to the right one space. I'm just uncertain whether the problem is the mark set insert or the
uplevel #0 ... delete insert

The lines of code causing the problem are in the hypertext.instance proc.

The lines are:
   } else {
    #.w delete/insert n.n ?n.n?
    set old_cur [${w}_orig index insert]
    ${w}_orig mark set insert $i
    set s [${w}_orig index "insert linestart"]
    if {"insert" eq $cmd} {
     uplevel #0 [concat [list ${w}_orig] insert insert [lrange $args 2 end]]
    } else {
     uplevel #0 [concat [list ${w}_orig] delete insert [lrange $args 2 end]]
    set e [${w}_orig index "insert lineend"]
    ${w}_orig mark set insert $old_cur

I believe that the $args value that is causing the problem is:
delete insert-1c

What could be done to fix the problem?


MG April 2 2005 - If you remove the line
     ${w}_orig mark set insert $old_cur

near the very end, the cursor should stop moving after deletion.

It works exactly as you recommended Mike... Many many thanks! You are a heck of a programmer... A magician even... Say, what was the intention of the programmer when he wrote the line:
 ${w}_orig mark set insert $old_cur

What was the line for originally?

Thanks again Mike!

Problem 2: in tclsh, Backspace writes the characters ^H instead of erasing the previous character

AMG: This is a different sort of backspace problem, but I encounter it frequently enough. Most likely I'm not the only one with such badly misconfigured machines as to experience it, so I imagine others may come to this page looking for a solution. In tclsh sometimes backspace just writes a ^H. Fix? I type stty erase ^H. And I generate that ^H by hitting my backspace key rather than typing ^ followed by H. I usually need to press control-V first, when using readline, so that the next character typed will be inserted verbatim without interpretation.

This isn't tclsh-specific, of course. tclsh is just one of many, many programs that don't use readline but rather depend on the terminal to do all line editing and buffering. So all we do is reconfigure the terminal via stty.

LV AMG, this is rather weird. When you are seeing this, are you saying that in an xterm or other terminal console, that your backspace works fine, but when you start up tclsh, the backspace begins acting differently?

AMG: Yes, it is weird, but I can explain it. When using readline, the terminal is in raw mode, and readline itself interprets the metacharacters for backspace, delete, and arrow keys. When not using readline, the terminal is in cooked mode, and the terminal driver does the interpretation. readline is configured using the .inputrc file, and the terminal driver is configured with stty. It's certainly possible for one to be right and the other to be wrong. Thankfully it's easy to fix either one, but you have to know how first.

LV Ah ha! What I didn't think of was that you were using a shell that made use of readline! I assumed, wrongly of course, that others just used a normal shell (sh, ksh, whatever) as their terminal interface.

[EW] The following program 'readline front-end' provides input line-editing for any program that does not provide their own. Running 'rfle tclsh' for example will allow the arrow keys to work and also seems to fix backspace issues that I had.


Problem 3: when running expect and a text widget

I have a problem similar to the above whereby the backspace key inserts the cursor to the right. I have a telnet session running in the background (expect_background), and my tk text widget used for sending commands. I have used all sorts of methods to get the backspace working but it does not. The code I have so far is as follows:
bind .top23.tex37 <BackSpace> {
exp_send -i $telnet -- "%A"; 
%W mark set limit insert;  
%W mark gravity limit left;  
        if {[%W tag nextrange sel 1.0 end] != ""} {
                %W delete sel.first sel.last
        } elseif {[%W compare insert > limit]} {
                  %W delete insert-1c
                  %W mark set insert limit 
                  %W see insert

The backspace seems to work fine on the background telnet session but is not working correctly on the tk text widget. Your help with this would be much appreciated. I have spent days working on this to no avail. Thanks!

MG Most likely you want to remove the line:
  %W mark set insert limit

which I think just moves the insertion cursor to the same line/character position it was at before deletion.

Problem 4: confusion of Delete and Backspace keys on some machines

LV Mar 25, 2005. I'm looking at someone else's program that is using a text widget. For some reason, when I press the Del key and the Back Space key, on my sparc solaris keyboard, the text widget deletes the character to the right. The application does not appear to have any bindings that deal with this key.

What other kinds of operations should I examine to determine why this is happening?

rdt I suspect the configuration of the Xwindowing system and/or the stty setup for erase.

LV good idea - however, I don't think that's it. The program (too long and I'm under a non-disclosure to show) behaves differently than standard Tk; i.e. when I create a plain Tk text box in a seperate interpreter, the keys work as I expect. So there's some way, other than bind, to alter the keystroke behavior going into a text box. Weird...

KJN IIRC the Del and Backspace keys give "incorrect" behaviour on many old Sun machines - not just in Tk. Sorry that's not much help, but you could look on the Solaris lists for this problem.

MG Have you checked bindtags to make sure there aren't any additional bindings operating on the widget, beside those on the Text class and the widget itself?