Page 1 of 3

Keyboard input

Posted: Tue Oct 20, 2020 2:13 pm
by SuperGabry64
I just finished the Meaty Skeleton tutorial, and i want to know, there is a way of make user interact with the os trough keyboard inputs for commands and etc.?

Re: Keyboard input

Posted: Tue Oct 20, 2020 5:56 pm
by Octocontrabass
Yes, there is a way. I'm using a keyboard to interact with an OS while I write this post.

What part are you confused about? Accessing the hardware? Moving data between the driver and the UI? Using the UI to control your OS?

If you haven't already, I suggest reading this post.

Re: Keyboard input

Posted: Wed Oct 21, 2020 1:29 am
by bloodline
SuperGabry64 wrote:I just finished the Meaty Skeleton tutorial, and i want to know, there is a way of make user interact with the os trough keyboard inputs for commands and etc.?
I recommend just polling the keyboard for data to begin with, this way you can understand how to interact with the PS/2 hardware and learn how to interpret the data which the keyboard controller sends.

Re: Keyboard input

Posted: Wed Oct 21, 2020 1:41 am
by SuperGabry64
And how you do it ?

Re: Keyboard input

Posted: Wed Oct 21, 2020 2:31 am
by StudlyCaps
The most straight forward way is to access the keyboard as a PS/2 device.

https://wiki.osdev.org/%228042%22_PS/2_Controller - this page has all the information you should need to communicate with any generic PS/2 device.
https://wiki.osdev.org/PS/2_Keyboard - this contains the commands you can send to the keyboard, and the way to interpret data sent from the keyboard to the system.

http://www.brokenthorn.com/Resources/OSDev19.html this tutorial describes one way to implement a keyboard controller. These tutorials are often criticized for being buggy or incomplete but I think they give a good starting point and explain the thinking process used to take specifications and create a minimal implementation.

You won't be able to just copy/paste code and have it work like Meaty Skeleton though, you will need to determine what functions you need and figure out how to implement them using your current OS as a starting point. People here can help you understand the specs or debug code, but we can't tell you exactly what to write.

Re: Keyboard input

Posted: Wed Oct 21, 2020 2:55 am
by bloodline
SuperGabry64 wrote:And how you do it ?
The trickiest part is that the PS/2 device is connected to the x86 io port, which was a bit odd for me as I’m used to memory mapped io. So you need to read about the x86’s “port instructions”, this will need a bit of assembler, but I found using gcc’s inline asm intrinsics a relativity painless to wrap the required instructions in C functions.

There are plenty of examples online if you use Google (I’m pretty sure I actually used an example on the osdev wiki to figure it out).

Re: Keyboard input

Posted: Sun Oct 25, 2020 4:29 pm
by SuperGabry64
I finally finished the io for keyboards, but i don't know how to convert the character that inb outputs to the right characters

Re: Keyboard input

Posted: Sun Oct 25, 2020 4:36 pm
by Octocontrabass
If you haven't reconfigured the PS/2 controller or keyboard, then you will receive bytes in scan code set 1.

By default, the keyboard sends data in scan code set 2, and the PS/2 controller translates it to scan code set 1, so things can get funny depending on how you've configured the hardware.

There is a list of scan codes on the wiki.

Re: Keyboard input

Posted: Sun Oct 25, 2020 5:57 pm
by foliagecanine
Since the scancodes are a pain to get into an array, if you are using C, you can use this code freely:

Code: Select all

// This specific snippet of code, originally written by foliagecanine, is available for use under the CC0 license
// https://creativecommons.org/publicdomain/zero/1.0/

char kbdus[] = {
	0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0,
	'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
	'\n', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
	0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0,
	'*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9',
	'-', '4', '5', '6', '+', '1', '2', '3', '0', '.'
};
char kbdus_shift[] = {
	0, 0, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0,
	'\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}',
	'\n', 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '\"', '~',
	0, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0,
	'*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9',
	'-', '4', '5', '6', '+', '1', '2', '3', '0', '.'
};
char kbdus_caps[] = {
	0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0,
	'\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']',
	'\n', 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', '\'', '`',
	0, '\\', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 0,
	'*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9',
	'-', '4', '5', '6', '+', '1', '2', '3', '0', '.'
};
This code is a table so you can convert PS/2 scancodes into C-style chars that can be printed.
However, do note that it ignores any modifiers such as the numpad modifier.
Also, it does not handle keys like shift, alt, control, escape, backspace, etc.

As you can see, if you organize it right it almost looks like a standard US QWERTY keyboard.

Also, I've found this information very helpful: https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html

Hope this post helps!

Re: Keyboard input

Posted: Mon Oct 26, 2020 1:09 am
by SuperGabry64
Thanks for the table, but how can i implement it?

Re: Keyboard input

Posted: Mon Oct 26, 2020 1:29 am
by bloodline
SuperGabry64 wrote:Thanks for the table, but how can i implement it?
Have you ever heard of something called a “Lookup Table”?

Re: Keyboard input

Posted: Mon Oct 26, 2020 2:55 am
by SuperGabry64
No, i never heard of this

Re: Keyboard input

Posted: Mon Oct 26, 2020 4:01 am
by StudlyCaps
SuperGabry64 wrote:No, i never heard of this
I don't mean to be rude, but how much programming experience do you have? OS development can be very difficult, and you need to know very advanced programming techniques, a lookup table is a quite elementary technique.

Regardless, here is Wikipedia's description of a look up table. https://en.wikipedia.org/wiki/Lookup_table
It should explain the theory.

As for implementation; you get the scancode from your keyboard interface, you use a state machine to keep track of whether the shift key or caps key is down and write something like this to figure out what character to print.

Code: Select all

if(shift_down)
{
    return kbdus_shift[scan_code];
}
else if(caps_enable)
{
    return kbdus_caps[scan_code];
}
else
{
    return kbdus[scan_code];
}

Re: Keyboard input

Posted: Mon Oct 26, 2020 5:29 am
by bloodline
StudlyCaps wrote:
SuperGabry64 wrote:No, i never heard of this
I don't mean to be rude, but how much programming experience do you have? OS development can be very difficult, and you need to know very advanced programming techniques, a lookup table is a quite elementary technique.
Indeed, programming operating systems is generally second only to compiler writing in terms of difficulty.

I applaud your desire to learn this fascinating topic, but it is important to remember this is not a forum to teach general programming. You are expected to have done the programming groundwork yourself, either via formal qualification or private/professional study.

Don’t give up, but I recommend you attempt some less ambitious projects before embarking on writing an entire operating system!

-edit- it’s taken me 25 years to build up the confidence to attempt a desktop OS!

Re: Keyboard input

Posted: Mon Oct 26, 2020 8:25 am
by SuperGabry64
The fact is that the only programming i've done is object oriented, so something like ansi c or assembly is completely new to me