Which keyboard character set do you use?
-
- Member
- Posts: 52
- Joined: Mon Oct 11, 2010 11:37 pm
- Location: Milwaukee, Wisconsin
Which keyboard character set do you use?
I have finally gotten around to writing the interrupt code, handlers and tests for the keyboard. I accept the character set 1 scan codes that the keyboard gives by default. Everything works well, I've even implemented the alt-keypad method of building up an extended ASCII code so I can even type in the umlaut in my surname. I have looked at some other programmer's code such as VisOpSys and Menuet and even Linux 2.3.12 and I see that they also use character set 1.
But several other keys, such as the two logo keys, the menu keys, the so-called ACPI keys (Power, Wake, Sleep) and the various multi-media keys do not have codes in character set 1. (Maybe logo and menu do.) In order to access these keys it is necessary to turn off the translation performed by the keyboard controller. By the end of this weekend I hope to have my test code switching back and forth between the two character sets. I am using your Wiki page on keyboards to supplement the tables in Frank van Gilluwe's book Undocumented PC.
Which of the two keyboard character sets are you using in your system? (Character set three doesn't count.)
A related question, what about the Fn key that many laptops have? My development system is not a laptop but its keyboard has an Fn key. It does not appear to do anything except gobble up most key presses but there are a few function keys that are not gobbled. Eventually the keyboard stopped working completely and I had to use the mouse to shut down programs and reboot. Does the Fn key have a real scan code? It must if a keyboard such as mine is plugged into a laptop.
CelestialMechanic
But several other keys, such as the two logo keys, the menu keys, the so-called ACPI keys (Power, Wake, Sleep) and the various multi-media keys do not have codes in character set 1. (Maybe logo and menu do.) In order to access these keys it is necessary to turn off the translation performed by the keyboard controller. By the end of this weekend I hope to have my test code switching back and forth between the two character sets. I am using your Wiki page on keyboards to supplement the tables in Frank van Gilluwe's book Undocumented PC.
Which of the two keyboard character sets are you using in your system? (Character set three doesn't count.)
A related question, what about the Fn key that many laptops have? My development system is not a laptop but its keyboard has an Fn key. It does not appear to do anything except gobble up most key presses but there are a few function keys that are not gobbled. Eventually the keyboard stopped working completely and I had to use the mouse to shut down programs and reboot. Does the Fn key have a real scan code? It must if a keyboard such as mine is plugged into a laptop.
CelestialMechanic
Microsoft is over if you want it.
- BrightLight
- Member
- Posts: 901
- Joined: Sat Dec 27, 2014 9:11 am
- Location: Maadi, Cairo, Egypt
- Contact:
Re: Which keyboard character set do you use?
I set scancode set 2 in my PS/2 keyboard initialization code. It's sufficient for most all keys I need to support. Here is a reference if you need the scancode to character tables. You basically want to look at everything whose label starts with the prefix "ps2", as the first two labels are USB-specific.
Notice that some BIOSes allow you to manipulate the function of the Fn key. For example, my laptop's BIOS allows me to set "hotkey mode" or "function mode." In hotkey mode, pressing one of the keys with an "Fn" mark will send the hotkey scancodes (i.e. mute, volume up, volume down, exit, etc.) Then when you press one of the hotkeys with Fn at the same time, it will send the function scancodes (i.e. F1, F2, F3, F4, etc.) In function mode, this is vice versa. Your OS doesn't need to be aware of this either, and as far as it is concerned, the user either pressed one the "multimedia/ACPI keys" or the one of the "function keys."
Just for completeness, are you aware that some keys (probably the multimedia/ACPI keys) have scancodes larger than one byte?
The Fn key shouldn't send a scancode, and in fact, it doesn't. It merely changes the scancodes that are made by other keys (also known as hotkeys). For example, my laptop has an F1 key with the mute key. When you press it without Fn, it sends a Mute make code. When you release it, it sends a Mute break code. If you press Fn, nothing will happen and the driver will not become aware. When you press Fn and the mute key, it will send an F1 make code. When you release the mute key then, it will send an F1 break code. So basically, your code doesn't need to be aware of the existence of the Fn key.CelestialMechanic wrote:A related question, what about the Fn key that many laptops have?
Notice that some BIOSes allow you to manipulate the function of the Fn key. For example, my laptop's BIOS allows me to set "hotkey mode" or "function mode." In hotkey mode, pressing one of the keys with an "Fn" mark will send the hotkey scancodes (i.e. mute, volume up, volume down, exit, etc.) Then when you press one of the hotkeys with Fn at the same time, it will send the function scancodes (i.e. F1, F2, F3, F4, etc.) In function mode, this is vice versa. Your OS doesn't need to be aware of this either, and as far as it is concerned, the user either pressed one the "multimedia/ACPI keys" or the one of the "function keys."
Does this happen in your OS, or in your host OS? As far as I'm aware, the Fn/hotkey combinations all work fine under Linux and Windows. What does gobble up the key presses mean?CelestialMechanic wrote:My development system is not a laptop but its keyboard has an Fn key. It does not appear to do anything except gobble up most key presses but there are a few function keys that are not gobbled. Eventually the keyboard stopped working completely and I had to use the mouse to shut down programs and reboot.
Just for completeness, are you aware that some keys (probably the multimedia/ACPI keys) have scancodes larger than one byte?
You know your OS is advanced when you stop using the Intel programming guide as a reference.
Re: Which keyboard character set do you use?
Unfortunately a lot of kernels use scan code set 1 (since they don't bother to turn off translation and copy/paste the set from Bran's tutorial) and the more advanced kernels use keyboard layouts. I used a tool to map scan code set 1 to scan code set 2, then manually replaced all of the values with their corresponding ASCII since the tool generates complex designated initializer lists ([index] = value) and GCC doesn't support that, then I turned off translation in my PS/2 initialization code.
I'm sure there is an easier way to do things, but it seems any approach eventually involves manual labor.
I'm sure there is an easier way to do things, but it seems any approach eventually involves manual labor.
Re: Which keyboard character set do you use?
Hi,
Cheers,
Brendan
I use scancode set 2, because:CelestialMechanic wrote:I have finally gotten around to writing the interrupt code, handlers and tests for the keyboard. I accept the character set 1 scan codes that the keyboard gives by default. Everything works well, I've even implemented the alt-keypad method of building up an extended ASCII code so I can even type in the umlaut in my surname. I have looked at some other programmer's code such as VisOpSys and Menuet and even Linux 2.3.12 and I see that they also use character set 1.
- it's the only scancode set that a keyboard must support
- (with translation disabled in the PS/2 controller) it allows the keyboard driver to work exactly the same regardless of which PS/2 port the keyboard is plugged into
Normally a keyboard uses scancode set 2, but then the PS/2 controller mangles the bytes (using a relatively simple table) to translate the bytes into scancode set 1. This means that those keys must produce something for scancode set 1.CelestialMechanic wrote:But several other keys, such as the two logo keys, the menu keys, the so-called ACPI keys (Power, Wake, Sleep) and the various multi-media keys do not have codes in character set 1. (Maybe logo and menu do.)
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re: Which keyboard character set do you use?
I remember trying the logo and menu keys on a DOS program that expected set 1 scancodes and it'd see scancodes 91, 92 and 93 for them (I don't remember exactly which was which). I wouldn't guarantee this to work everywhere however, not to mention that I'm going by memory.
-
- Member
- Posts: 52
- Joined: Mon Oct 11, 2010 11:37 pm
- Location: Milwaukee, Wisconsin
Re: Which keyboard character set do you use?
I got the code for character set 2 working last night. I am now able to switch between the character sets and type away. I tried it on a Windows 8 computer using Virtual Box that has the multimedia keys but to no avail. The emulator does not pass all of the keystrokes through, I will have to try it bare metal in order to see all the keys.
There are still a few kinks to be worked out, I need to check my key class tables. I have divided the keys into six classes as follows:
0: The alphabetic keys, may be affected by the caps lock mode.
1: Digits and other punctuation symbols in the main part of the keyboard.
2: The numeric keypad, may be affected by the num lock mode.
3: Other keys that do not generate ASCII codes, such as the function keys.
4: Shift keys, including the logo keys. Only generates messages for alt and logo keys pressed and released without any intervening key presses. Generally does not post cooked messages.
5: Mode keys, caps lock, scroll lock, num lock and insert. Does not post cooked messages.
I use the same code for both character sets. The software part of the switch is done by changing pointers to the key tables and setting a few variables.
There are still a few kinks to be worked out, I need to check my key class tables. I have divided the keys into six classes as follows:
0: The alphabetic keys, may be affected by the caps lock mode.
1: Digits and other punctuation symbols in the main part of the keyboard.
2: The numeric keypad, may be affected by the num lock mode.
3: Other keys that do not generate ASCII codes, such as the function keys.
4: Shift keys, including the logo keys. Only generates messages for alt and logo keys pressed and released without any intervening key presses. Generally does not post cooked messages.
5: Mode keys, caps lock, scroll lock, num lock and insert. Does not post cooked messages.
I use the same code for both character sets. The software part of the switch is done by changing pointers to the key tables and setting a few variables.
Microsoft is over if you want it.
Re: Which keyboard character set do you use?
Hi,
The problem is that there's over 100 different keyboard layouts, and for each one there's multiple variations. To deal with that you need a table driven approach where the tables and other metadata are loaded from a file (plus hopefully/eventually some kind of "keyboard layout editor" so that it's easy for people to create/modify/customise keyboard layouts).
The basic idea would be to convert the (multi-byte) scancodes into some sort of (single integer) "key code" that can be used for array indexing; then check if the key corresponds to a special key (and if it changes "current table number" and/or keyboard LEDs), and if it doesn't use the table number and key code to find that key's information (a unicode sequence, or some kind of standardised identifier for all the keys that don't have a unicode sequence like "page up" or "decrease volume" or "insert").
I'd be tempted to have a 16-bit "state" variable where the lowest 8 bits correspond to keyboard LEDs (there can be more than 3 LEDs), and where special keys are either "set bit when key is being held down" or "toggle bit when key is pressed". Then I'd have a table to convert that "16-bit state" into a table number.
For example, in the keyboard layout file you might have something saying:
Cheers,
Brendan
This approach lacks "necessary extensibility".CelestialMechanic wrote:There are still a few kinks to be worked out, I need to check my key class tables. I have divided the keys into six classes as follows:
0: The alphabetic keys, may be affected by the caps lock mode.
1: Digits and other punctuation symbols in the main part of the keyboard.
2: The numeric keypad, may be affected by the num lock mode.
3: Other keys that do not generate ASCII codes, such as the function keys.
4: Shift keys, including the logo keys. Only generates messages for alt and logo keys pressed and released without any intervening key presses. Generally does not post cooked messages.
5: Mode keys, caps lock, scroll lock, num lock and insert. Does not post cooked messages.
I use the same code for both character sets. The software part of the switch is done by changing pointers to the key tables and setting a few variables.
The problem is that there's over 100 different keyboard layouts, and for each one there's multiple variations. To deal with that you need a table driven approach where the tables and other metadata are loaded from a file (plus hopefully/eventually some kind of "keyboard layout editor" so that it's easy for people to create/modify/customise keyboard layouts).
The basic idea would be to convert the (multi-byte) scancodes into some sort of (single integer) "key code" that can be used for array indexing; then check if the key corresponds to a special key (and if it changes "current table number" and/or keyboard LEDs), and if it doesn't use the table number and key code to find that key's information (a unicode sequence, or some kind of standardised identifier for all the keys that don't have a unicode sequence like "page up" or "decrease volume" or "insert").
I'd be tempted to have a 16-bit "state" variable where the lowest 8 bits correspond to keyboard LEDs (there can be more than 3 LEDs), and where special keys are either "set bit when key is being held down" or "toggle bit when key is pressed". Then I'd have a table to convert that "16-bit state" into a table number.
For example, in the keyboard layout file you might have something saying:
- key number 0x20 configured as "toggle bit 1 when presed" (for the capslock key)
- key number 0x10 configured as "set bit 8 when held down" (for the left shift key)
- key number 0x1B configured as "set bit 9 when held down" (for the right shift key)
- State 0x0000 selects table 0 (no shift, no capslock)
- State 0x0002 selects table 1 (no shift keys, with capslock)
- State 0x0102 selects table 2 (left shift key, with capslock)
- State 0x0202 selects table 2 (right shift key, with capslock)
- State 0x0302 selects table 1 (both shift keys, with capslock)
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
-
- Member
- Posts: 52
- Joined: Mon Oct 11, 2010 11:37 pm
- Location: Milwaukee, Wisconsin
Re: Which keyboard character set do you use?
Brendan,
This weekend I started work on a scheme similar to the one you proposed.
I preprocess the received bytes so that the break code is represented as bit 12 in both cases so that the scan code is two bytes whether it came from set 1 or set 2. For example, the break code for the Insert key in the cursor key group is E0 D2 in set 1 and is changed to F0 52, and in set 2 it is E0 F0 70 which is changed to F0 70. My former keyclass table is now a table of keyflags, 16 bits each, with the upper byte identifying which of the eight shift keys (shift, control, alt, logo, left or right), the upper nybble of the lower byte identifying which of the "mode" keys (caps lock, scroll lock, num lock and insert), and the bottom nybble containing flags for whether the key generates ASCII, is affected by the num lock mode or by the caps lock mode. This left one bit unaccounted for, but literally as I was writing this I realized it should be a flag to indicate a dead key.
By next weekend I expect to have this in place.
I've also noticed something about the various emulators that I use, and that is they do not truly virtualize the keyboard. That is, when I press caps lock it in the emulator, the LED lights up, even though I have not written code to do so. My naive expectation is that the host OS will preserve its keyboard state and restore it if the focus switches to a non-emulator window, and then back again to the emulator state when the emulator gets input focus back.
Another odd thing, on some keyboards only the caps lock and num lock keys change even though I have not explicitly changed them, but the scroll lock does not. (I think the Wiki article mentions this.)
You are correct about the tables, of course I will need tables for foreign keyboards someday.
Thank you for your suggestions.
This weekend I started work on a scheme similar to the one you proposed.
I preprocess the received bytes so that the break code is represented as bit 12 in both cases so that the scan code is two bytes whether it came from set 1 or set 2. For example, the break code for the Insert key in the cursor key group is E0 D2 in set 1 and is changed to F0 52, and in set 2 it is E0 F0 70 which is changed to F0 70. My former keyclass table is now a table of keyflags, 16 bits each, with the upper byte identifying which of the eight shift keys (shift, control, alt, logo, left or right), the upper nybble of the lower byte identifying which of the "mode" keys (caps lock, scroll lock, num lock and insert), and the bottom nybble containing flags for whether the key generates ASCII, is affected by the num lock mode or by the caps lock mode. This left one bit unaccounted for, but literally as I was writing this I realized it should be a flag to indicate a dead key.
By next weekend I expect to have this in place.
I've also noticed something about the various emulators that I use, and that is they do not truly virtualize the keyboard. That is, when I press caps lock it in the emulator, the LED lights up, even though I have not written code to do so. My naive expectation is that the host OS will preserve its keyboard state and restore it if the focus switches to a non-emulator window, and then back again to the emulator state when the emulator gets input focus back.
Another odd thing, on some keyboards only the caps lock and num lock keys change even though I have not explicitly changed them, but the scroll lock does not. (I think the Wiki article mentions this.)
You are correct about the tables, of course I will need tables for foreign keyboards someday.
Thank you for your suggestions.
Microsoft is over if you want it.
-
- Member
- Posts: 52
- Joined: Mon Oct 11, 2010 11:37 pm
- Location: Milwaukee, Wisconsin
Re: Which keyboard character set do you use?
A few thoughts about the Fn key.
The Fn key is used on laptops as a sort of shift key, usually in conjunction with the function keys to provide several hardware services such as brightening/dimming the screen, cycling through various output screen options, muting/unmuting the volume, raising/lowering the volume etc. I've also seen it used to provide a replacement for the numeric keypad, similar to the way digits were entered with the help of the alphanumeric key on old keypunch machines. (That really puts a date on me!)
These functions have to be available no matter what brand of dog food from Redmond the laptop is running, so these key presses must not go to the computer. They must be "gobbled up". But what if someone attaches an external keyboard with its Fn key? How does the hardware transmit the signal that the Fn key has been pressed so that the hardware function can be carried out? My guess is that maybe the Fn key holds a line on one of its pins high or some such thing. Does anyone know?
The Fn key is used on laptops as a sort of shift key, usually in conjunction with the function keys to provide several hardware services such as brightening/dimming the screen, cycling through various output screen options, muting/unmuting the volume, raising/lowering the volume etc. I've also seen it used to provide a replacement for the numeric keypad, similar to the way digits were entered with the help of the alphanumeric key on old keypunch machines. (That really puts a date on me!)
These functions have to be available no matter what brand of dog food from Redmond the laptop is running, so these key presses must not go to the computer. They must be "gobbled up". But what if someone attaches an external keyboard with its Fn key? How does the hardware transmit the signal that the Fn key has been pressed so that the hardware function can be carried out? My guess is that maybe the Fn key holds a line on one of its pins high or some such thing. Does anyone know?
Microsoft is over if you want it.
Re: Which keyboard character set do you use?
I just happen to be using a laptop (with an Fn key) via an external keyboard that also has an Fn key... After performing a couple of tests, it's pretty clear that the Fn key only affects the keyboard it's on. It simply isn't transmitted in any way.CelestialMechanic wrote: But what if someone attaches an external keyboard with its Fn key? How does the hardware transmit the signal that the Fn key has been pressed so that the hardware function can be carried out? My guess is that maybe the Fn key holds a line on one of its pins high or some such thing. Does anyone know?
e.g. Pressing Fn+F11 on the external keyboard is "volume up". If I press the Fn key on the built-in keyboard and F11 on the external, I get F11 (browser goes full-screen).
Pressing Fn+F1 on the built-in keyboard is "mute". If I press the Fn key on the external keyboard and F1 on the built-in, I get F1 (browser loads opens help).