backspace question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
McZ

backspace question

Post 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"
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Re:backspace question

Post 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.
Dex4u

Re:backspace question

Post 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.
McZ

Re:backspace question

Post 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.
Dex4u

Re:backspace question

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:backspace question

Post 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).
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:backspace question

Post 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.
Post Reply