I'm writing a program for DOS and I want to get keyboard input. The thing is that I don't want to get that input using BIOS or DOS. What's the proper way to get keyboard input without using DOS or BIOS? I mean what's the way to get keyboard input in the lowest level of programming using I/O ports. Thanks!
Asked
Active
Viewed 2,378 times
0
-
See http://wiki.osdev.org/Main_Page – Basile Starynkevitch Oct 22 '12 at 19:25
-
@BasileStarynkevitch there isn't much info about this topic. – Arnas Oct 22 '12 at 19:26
-
Check out [this answer](http://stackoverflow.com/a/10525397/968261). – Alexey Frunze Oct 23 '12 at 02:42
-
@AlexeyFrunze thanks, but that's a little bit too hard for me I'd say. I need something easier... – Arnas Oct 23 '12 at 10:59
-
@Arnas Come on, it's a tiny program. You will learn something from it. And that's the "proper way". – Alexey Frunze Oct 23 '12 at 16:52
-
@AlexeyFrunze thank you! I've analyzed that program, I pretty much understand it now. Thanks again! – Arnas Oct 23 '12 at 21:41
1 Answers
1
You need to do an inb
instruction on port 0x60 to read the scancode from the keyboard.
static inline uint8_t inportb(uint16_t port)
{
uint8_t ret;
asm volatile ("inb %1, %0" : "=a" (ret) : "dN" (port));
return ret;
}
scancode = inportb(0x60);
If you want to know when there's new input you need to setup an interrupt handler to listen for PS/2 interrupts or use USB polling depending on your keyboard.

Jesus Ramos
- 22,940
- 10
- 58
- 88
-
Hello, I tried to read port 60h port, but whenever I read it, it instantly returns 28 (scancode of ENTER key). It doesn't even let me to give any input. Maybe I need to clear a buffer? If so, could you please tell me how can I do that? – Arnas Oct 22 '12 at 21:23
-
@Arnas I believe that if you read from the data port on the keyboard it always contains the last key pressed (which is probably enter). Is it USB or PS/2 keyboard by any chance? – Jesus Ramos Oct 22 '12 at 23:10
-
-
For PS/2 you need to setup an interrupt handler for when new data is available. For USB you need to do some extra work which I am not so familiar with currently. – Jesus Ramos Oct 23 '12 at 18:12