PS/2 Keyboard: Difference between revisions

Deleted the broken example code that should've been a broken example on a PS/2 controller page
[unchecked revision][unchecked revision]
(Deleted the broken example code that should've been a broken example on a PS/2 controller page)
Line 224:
* Various other key states (shift, alt, control, etc)
* Various "toggle" states (CapsLock, ScrollLock, NumberLock, etc)
 
== Enough, give me code! ==
 
So, first read the scan code set table, and now start to code. If you too lazy, here's the full code:
 
(inb/outb can be found here: [[Inline Assembly/Examples#I.2FO access|Inline Assembly Examples - I/O Access]])
<source lang="c">
/*
PS/2 keyboard code.
Dependencies:
inb function and scancode table.
*/
char getScancode() {
char c=0;
do {
if(inb(0x60)!=c) {
c=inb(0x60);
if(c>0)
return c;
}
} while(1);
}
 
char getchar() {
return scancode[getScancode()+1];
}
</source>
 
If you have not set up interrupts yet, the code for the function that reads scancodes will be slightly different:
 
<source lang="C">
char getScancode()
{
while (!(inb(0x64) & 1));
return inb(0x60);
}
</source>
The reason we are reading port 0x64 first is because in an environment without interrupts, one must first read the status port (0x64) and see if the first bit is set to one. If the first bit is set to one, the loop ends and we read port 0x60, returning a scancode. If it is not set to one, we do not read it and loop until it is set to one. This will assure that we are not infinitely releasing scancodes (since we do not have interrupts yet to make sure they are only pressed once).
 
== Scan Code Sets ==
250

edits