OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 8:26 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 41 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Keyboard input
PostPosted: Tue Oct 20, 2020 2:13 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Tue Oct 20, 2020 5:56 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
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.


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Wed Oct 21, 2020 1:29 am 
Offline
Member
Member
User avatar

Joined: Tue Sep 15, 2020 8:07 am
Posts: 264
Location: London, UK
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


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Wed Oct 21, 2020 1:41 am 
Offline

Joined: Tue Oct 20, 2020 10:38 am
Posts: 20
And how you do it ?


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Wed Oct 21, 2020 2:31 am 
Offline
Member
Member

Joined: Mon Jul 25, 2016 6:54 pm
Posts: 223
Location: Adelaide, Australia
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.


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Wed Oct 21, 2020 2:55 am 
Offline
Member
Member
User avatar

Joined: Tue Sep 15, 2020 8:07 am
Posts: 264
Location: London, UK
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


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Sun Oct 25, 2020 4:29 pm 
Offline

Joined: Tue Oct 20, 2020 10:38 am
Posts: 20
I finally finished the io for keyboards, but i don't know how to convert the character that inb outputs to the right characters


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Sun Oct 25, 2020 4:36 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
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.


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Sun Oct 25, 2020 5:57 pm 
Offline
Member
Member

Joined: Sun Aug 23, 2020 4:35 pm
Posts: 148
Since the scancodes are a pain to get into an array, if you are using C, you can use this code freely:
Code:
// 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?


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Mon Oct 26, 2020 1:09 am 
Offline

Joined: Tue Oct 20, 2020 10:38 am
Posts: 20
Thanks for the table, but how can i implement it?


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Mon Oct 26, 2020 1:29 am 
Offline
Member
Member
User avatar

Joined: Tue Sep 15, 2020 8:07 am
Posts: 264
Location: London, UK
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


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Mon Oct 26, 2020 2:55 am 
Offline

Joined: Tue Oct 20, 2020 10:38 am
Posts: 20
No, i never heard of this


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Mon Oct 26, 2020 4:01 am 
Offline
Member
Member

Joined: Mon Jul 25, 2016 6:54 pm
Posts: 223
Location: Adelaide, Australia
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:
if(shift_down)
{
    return kbdus_shift[scan_code];
}
else if(caps_enable)
{
    return kbdus_caps[scan_code];
}
else
{
    return kbdus[scan_code];
}


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Mon Oct 26, 2020 5:29 am 
Offline
Member
Member
User avatar

Joined: Tue Sep 15, 2020 8:07 am
Posts: 264
Location: London, UK
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


Top
 Profile  
 
 Post subject: Re: Keyboard input
PostPosted: Mon Oct 26, 2020 8:25 am 
Offline

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 41 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Gigasoft, Google [Bot] and 207 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group