Keyboard input

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.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Keyboard input

Post 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.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
Techel
Member
Member
Posts: 215
Joined: Fri Jan 30, 2015 4:57 pm
Location: Germany
Contact:

Re: Keyboard input

Post by Techel »

And you have really not seen the big banner on top of the page ;)
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard input

Post by osdever »

I seen it, but i need a full code.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
glauxosdev
Member
Member
Posts: 119
Joined: Tue Jan 20, 2015 9:01 am
Libera.chat IRC: glauxosdever

Re: Keyboard input

Post by glauxosdev »

To read a scan code from the keyboard use:

Code: Select all

in al, 60h
Based on the scan code you got, you will need to convert it to a ascii/unicode/etc character.

Hope this helps,
glauxosdev
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard input

Post by osdever »

glauxosdev wrote:To read a scan code from the keyboard use:

Code: Select all

in al, 60h
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.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
glauxosdev
Member
Member
Posts: 119
Joined: Tue Jan 20, 2015 9:01 am
Libera.chat IRC: glauxosdever

Re: Keyboard input

Post 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
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Keyboard input

Post by Roman »

catnikita255 wrote:
glauxosdev wrote:To read a scan code from the keyboard use:

Code: Select all

in al, 60h
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.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
ExeTwezz
Member
Member
Posts: 104
Joined: Sun Sep 21, 2014 7:16 am
Libera.chat IRC: exetwezz

Re: Keyboard input

Post 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.
Last edited by ExeTwezz on Fri Apr 24, 2015 11:43 am, edited 2 times in total.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard input

Post by osdever »

Roman wrote:
catnikita255 wrote:
glauxosdev wrote:To read a scan code from the keyboard use:

Code: Select all

in al, 60h
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.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: Keyboard input

Post 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);
}
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Keyboard input

Post 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.
glauxosdev
Member
Member
Posts: 119
Joined: Tue Jan 20, 2015 9:01 am
Libera.chat IRC: glauxosdever

Re: Keyboard input

Post by glauxosdev »

Removed: Plain wrong.
Last edited by glauxosdev on Sat May 02, 2015 8:59 am, edited 1 time in total.
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Keyboard input

Post 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).
glauxosdev
Member
Member
Posts: 119
Joined: Tue Jan 20, 2015 9:01 am
Libera.chat IRC: glauxosdever

Re: Keyboard input

Post by glauxosdev »

Removed: Plain wrong.
Last edited by glauxosdev on Sat May 02, 2015 8:59 am, edited 1 time in total.
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Keyboard input

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