Page 1 of 2
Keyboard input
Posted: Sat Apr 04, 2015 12:04 pm
by osdever
How to make the keyboard input in my OS? All is working fine, except the keyboard input. I tried getch, getchar, scanf, but nothing of them is existing in the OS, because of freestanding. Please, help.
Re: Keyboard input
Posted: Sat Apr 04, 2015 12:07 pm
by Techel
And you have really not seen the big banner on top of the page
Re: Keyboard input
Posted: Sat Apr 04, 2015 12:17 pm
by osdever
I seen it, but i need a full code.
Re: Keyboard input
Posted: Sat Apr 04, 2015 12:29 pm
by glauxosdev
To read a scan code from the keyboard use:
Based on the scan code you got, you will need to
convert it to a ascii/unicode/etc character.
Hope this helps,
glauxosdev
Re: Keyboard input
Posted: Sat Apr 04, 2015 12:43 pm
by osdever
glauxosdev wrote:To read a scan code from the keyboard use:
Based on the scan code you got, you will need to
convert it to a ascii/unicode/etc character.
Hope this helps,
glauxosdev
I'm making OS in C, thank you, but give C example, please.
Re: Keyboard input
Posted: Sat Apr 04, 2015 12:59 pm
by glauxosdev
If you don't know Assembly, where are you going? This line of code reads a byte from port 60h (which is the keyboard port) to register al. I don't think it is now hard to translate it to C.
Regards,
glauxosdev
Re: Keyboard input
Posted: Sat Apr 04, 2015 12:59 pm
by Roman
catnikita255 wrote:glauxosdev wrote:To read a scan code from the keyboard use:
Based on the scan code you got, you will need to
convert it to a ascii/unicode/etc character.
Hope this helps,
glauxosdev
I'm making OS in C, thank you, but give C example, please.
You need to understand assembly. You can write some C wrappers for port I/O. An example is
here.
Re: Keyboard input
Posted: Sat Apr 04, 2015 1:03 pm
by ExeTwezz
You know...
Introduction
Required Knowledge
Beginner Mistakes
How To Ask Questions
You must be experienced enough in userspace in order to write a kernel. Also, you must read the manuals for the architecture you want your kernel to run on to write a good kernel.
Re: Keyboard input
Posted: Sat Apr 04, 2015 1:23 pm
by osdever
Roman wrote:catnikita255 wrote:glauxosdev wrote:To read a scan code from the keyboard use:
Based on the scan code you got, you will need to
convert it to a ascii/unicode/etc character.
Hope this helps,
glauxosdev
I'm making OS in C, thank you, but give C example, please.
You need to understand assembly. You can write some C wrappers for port I/O. An example is
here.
Thank you, i'll check it.
Re: Keyboard input
Posted: Sun Apr 05, 2015 1:46 am
by osdever
I did the input, but it don't waits the input, it's just typing some blank character and capital S, like that (B is the blank char):
BSBSBSBSBSBSBS
Whet it reaches the bottom of the screen, OS is rebooting. How to fix it?
There's a code:
Code: Select all
char oldkey;
char key;
for(;;)
{
oldkey = key;
key = inb(60);
if(!oldkey = key)
terminal_writestring(key);
}
Re: Keyboard input
Posted: Sun Apr 05, 2015 2:07 am
by iansjack
I'm afraid that you have been given an oversimplified explanation of how to read from a keyboard, which is worse than useless. There is more than one way of doing this, and to read directly from a USB keyboard is quite complicated, so a beginner will almost always use legacy methods (unless you ar programming a Raspberry Pi or suchlike; reading from the keyboard there is something of a stumbling block for OS development). But for a look at what is involved for a normal PC have a look at
http://www.brokenthorn.com/Resources/OSDev19.html . I'd say that any serious keyboard driver is likely to be interrupt-driven, so first you need to be familiar with the interrupt mechanism and be comfortable with writing interrupt handlers. This is turn will involve an understanding of descriptor tables (unless you are staying in real mode - in which case why not just use the BIOS to read the keyboard?).
If you ever ask an OS question "How do I do such and such?" and the answer is "Just read port xx." you can be fairly sure that it is wrong. Device drivers tend to be a little more involved than that.
Re: Keyboard input
Posted: Sun Apr 05, 2015 2:16 am
by glauxosdev
Removed: Plain wrong.
Re: Keyboard input
Posted: Sun Apr 05, 2015 2:25 am
by iansjack
I supposed he could further develop this, but I said he needs to convert those scan codes to actual characters.
Let's not put the cart before the horse. First you have to know that there is a character waiting to be read. Else you get a meaningless string of characters. Hey! there's a coincidence....
You need to read from the keyboard either by interrupts, either by polling. This has to be done every some interval (eg 25 times a second). If a key is still pressed you will read the same scan code, so you will want to ignore it for another interval (eg 1 second), else your result will be like this because you can't press a key for such a short time:
Come on! Be serious. You don't write drivers this way (by just assuming that if you get the same result twice the input is invalid and if you get different results that they are valid input).
Re: Keyboard input
Posted: Sun Apr 05, 2015 2:30 am
by glauxosdev
Removed: Plain wrong.
Re: Keyboard input
Posted: Sun Apr 05, 2015 3:25 am
by iansjack
You don't think it's a good idea to check if there is valid data to be read before getting a character from the port?