Page 1 of 1

backspace question

Posted: Thu Mar 02, 2006 12:42 pm
by McZ
I have just implemented my keyboard handler altough I can't figure out how backspace can be implemented..

lets say you have a prompt like this

Code: Select all

Kernel>
And the input starts there then it would be nice to erase a character if you type something wrong but NOT erase the prompt or anything else which doesn't belong to the "input string"

Re:backspace question

Posted: Thu Mar 02, 2006 1:04 pm
by nick8325
Well, what you should do with a backspace probably depends on what the thing reading it want to do - if you have a full-screen text editor, for example, it will probably want to handle backspace itself.

So a good way might be to have a read_line function, which reads a line from the keyboard, printing it to the screen and handling backspaces as it goes, and then returns a string when you press return.

That way, you'll know when to accept backspaces - if the string you've read in so far is empty, ignore it, otherwise remove the last character from it and from the screen.

Re:backspace question

Posted: Thu Mar 02, 2006 2:03 pm
by Dex4u
All you need is a var that starts as 0, when a key is pressed that would be printed to screen, the var is inc, but if backspace is pressed, this var is dec, before erasing a char, the var is checked for 0, if it is then the backspace is ignored.

NOTE: This is done locally, eg: command .com, not in the keyboard handler, the keyboard handle just converts scan code to ASCII etc.

Re:backspace question

Posted: Thu Mar 02, 2006 2:33 pm
by McZ
so the character erase actually happens in "input" functions like scanf and getline or whatever function I have. This would make sense because that function will know when it gets input and how many characters it has recived and can erase.

Re:backspace question

Posted: Thu Mar 02, 2006 3:04 pm
by Dex4u
The way i do it (which is for a full asm OS) , is like this
First i have a separate buff for cli input, my keyboard function just gets the key and converts it etc.
in the cli would be a input loop one of the things in the loop would be getkeypressed, that key is tested for keys like backspace, enter, etc, if it not one of them its printed and the var inc, if its a backspace we check for 0, if above we move the screen pointer back one and print a space (0x20) and dec the var, (same with the screen buffer, if you use a differant buffer).
This is all for a single tasking pmode OS, written in asm.

Re:backspace question

Posted: Mon Mar 06, 2006 1:15 am
by Solar
McZ wrote: so the character erase actually happens in "input" functions like scanf and getline or whatever function I have.
Until you press the return button, the code being executed is your console driver, shell, whatever, but certainly not [tt]scanf()[/tt] - that is idly waiting for a line of input to be sent to it (i.e., console / shell having done its job).

Re:backspace question

Posted: Mon Mar 06, 2006 3:16 am
by Pype.Clicker
in other words, in virtually all the OSes, you have

- a keyboard driver delivering "raw" keystrokes to a buffer (or the "raw keyboard device")
- a OS system program (the shell, the terminal emulator, or even the GUI server) that takes care of constructing a representation of the "cooked" ASCII string corresponding to what the user types.
- a user program that waits on "terminal device" for "cooked" strings.

keystrokes such as LFT_ARROW or BKSPACE are handled by the OS system program, which updates the cooked string accordingly, handles insertions, perhaps completion and updates the display on the "raw screen device" accordingly.

In a singletasking system such as DOS, you still had similar differences except that it now appears as if performed by an "OS library" rather than by some external entity.