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.
SuperGabry64
Posts: 20
Joined: Tue Oct 20, 2020 10:38 am

Keyboard input

Post 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.?
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Keyboard input

Post 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.
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Re: Keyboard input

Post 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.
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
SuperGabry64
Posts: 20
Joined: Tue Oct 20, 2020 10:38 am

Re: Keyboard input

Post by SuperGabry64 »

And how you do it ?
StudlyCaps
Member
Member
Posts: 232
Joined: Mon Jul 25, 2016 6:54 pm
Location: Adelaide, Australia

Re: Keyboard input

Post 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.
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Re: Keyboard input

Post 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).
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
SuperGabry64
Posts: 20
Joined: Tue Oct 20, 2020 10:38 am

Re: Keyboard input

Post 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
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Keyboard input

Post 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.
foliagecanine
Member
Member
Posts: 148
Joined: Sun Aug 23, 2020 4:35 pm

Re: Keyboard input

Post 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!
My OS: TritiumOS
https://github.com/foliagecanine/tritium-os
void warranty(laptop_t laptop) { if (laptop.broken) return laptop; }
I don't get it: Why's the warranty void?
SuperGabry64
Posts: 20
Joined: Tue Oct 20, 2020 10:38 am

Re: Keyboard input

Post by SuperGabry64 »

Thanks for the table, but how can i implement it?
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Re: Keyboard input

Post by bloodline »

SuperGabry64 wrote:Thanks for the table, but how can i implement it?
Have you ever heard of something called a “Lookup Table”?
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
SuperGabry64
Posts: 20
Joined: Tue Oct 20, 2020 10:38 am

Re: Keyboard input

Post by SuperGabry64 »

No, i never heard of this
StudlyCaps
Member
Member
Posts: 232
Joined: Mon Jul 25, 2016 6:54 pm
Location: Adelaide, Australia

Re: Keyboard input

Post 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];
}
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Re: Keyboard input

Post 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!
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
SuperGabry64
Posts: 20
Joined: Tue Oct 20, 2020 10:38 am

Re: Keyboard input

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