0

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!

Arnas
  • 229
  • 1
  • 15

1 Answers1

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
  • The one I was using was USB, but I have another one with PS/2. – Arnas Oct 23 '12 at 10:31
  • 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