Memblers wrote: Mon Apr 06, 2020 5:28 am
Looking into the USB specs, USB returns those scancodes as an array of currently pressed keys, it must be the USB host driver and OS generating press and release events (along with key repeats, sticky keys, etc.). It seems best to maybe take this USB scancode set and customize it into a synchronous PS/2-like interface. See the attached file for what I'm thinking. One byte per key press, using D7 to select press/release for up to 127 keys.
NES keyboard DRAFT.txt
I cut out stuff that one wouldn't see on most keyboards, like F12-F24 keys, copy, paste, help, find, power, etc. I kept some of the media controls because there was going to be a little room left over, and those seem to be the only "extra" keys I use sometimes. I'm open to changing it, if it can be improved. I have some other projects to complete first, but after that I'd like to work on keyboard support.
After going over this to create an emulator implementation, and the test ROMs, I feel that this sufficiently covers the needs of an NES keyboard, so I don't think there's any need to make changes to it. A brief overview of the chosen keys, trying to highlight the parts that might be worth reviewing.
- $01 - Rollover. Probably not a lot of use to most applications. Maybe could help diagnose keyclash, but I feel like the user can figure that out anyway by what key doesn't respond.
- $02 - Post Fail. Not useful. The USB host device should be dealing with this, not the NES software.
- $03 - Undefined error. Also not likely to be useful.
- $04-$64 - Matches the standard and I see no reason to change or omit any of those.
- $65 - Compose. Standard. Edit: For typing accent combinations on some language keyboards, but may also be the "context menu" key on some US keyboards?
- $66 - Ro (International1). Older 109-key Japanese keyboards have this as an extra key next to a shortened right shift.
- $67 - Kana (International2). I think this is good to have, because the Family Basic keyboard has this.
- $68 - Yen (International3). Another 109 extra, next to a shortened backspace. Is probably also Won on Korean keyboards?
- $69 - Henkan (International4). 109 extra, right of the space bar. Converts current kana to kanji.
- $6A - Muhenkan (International5). 109 extra, left of the space bar. Keeps current kana without converting.
- $6B - Kp Jp Comma (International6). Couldn't find any information about keyboards that might have this?
- $6C - Hangeul or Han/Yeong (LANG1). Toggles Hangeul entry for Korean.
- $6D - Hanja (LANG2). Converts Hangeul to Hanja characters for Korean, though often right ctrl has this function instead of a dedicated key.
- $6E - Katakana (LANG3). Dedicated Katakana key for Japanese keyboards. Uncommon/specialized?
- $6F - Hiragana (LANG4). Dedicated Hiragana key for Japanese keyboards. Uncommon/specialized?
- $70 - Zenkaku Hankaku (LANG5). Full width / half width toggle.
- $71-$77 - Media keys (play-pause, stop, prev, next, vol up, vol down, mute). These are pretty good to have. The number range and order isn't standard here, but that's OK.
- $78-$7F - Left/right control, shift, alt, win. Standard order but not in the standard range, but these are essential keys and putting them at the very top of the range like this is a great idea. Also think the symmetrical 4+4 arrangement aligned to 4 is good because it facilitates merging them in the NES ROM's software.
So
$00-$65 are standard, almost all of them nearly universally present. Reading the standard it seems that the letter key codes are for the US locale, so e.g. a French keyboard might have a Z where the US W key is, but it will still use the W scancode. Though, maybe in a case like that the USB host device could be tasked with remapping it. At any rate, if your NES software has controls that require a specific physical layout (e.g. WASD) it's probably best to make it user configurable if you can.
$66-$70 feel the weakest, but among the USB HID tables, I would agree that these 11 keys are probably the most important. Everything else is basically uncommon functional extensions, extra F-keys, hexadecimal numpad, stuff like that. A lot of these have uses that probably aren't well suited for the NES anyway (it's never going to be a great word-processing platform), and they tend to have redundant modern IME methods allowing the same functionality through other keys. So... ultimately I think these are fine as-is, but they probably wouldn't get much use anyway.
$71-$77 as media control keys seems good to have. There's probably some relevant purposes for this that would work on the NES. I think the 7 you picked are probably the most commonly seen ones. I often also see a homepage key, or an e-mail key, or sometimes cut/copy/paste keys, but nothing I think is more common or useful here than the media keys chosen.
$78-$7F was a great place for these essential modifier keys.
I think in general this can be considered a "soft" standard, that someone could override if they had a need, but I think this set should cover a pretty wide range of possibilities as-is. If anything is to be overridden for some special purpose software, I would expect the
$66-$70 block to be the first to fall, but it's fine to have them specified here. I don't think we need to have a reserved area or anything like that.
Similarly, the errors at
$01-$03 probably aren't useful. I wouldn't expect NES software to try to do anything with them, but it's not a problem to leave them in.
One thing I noted while implementing this for Mesen is that num-lock can be tricky, because in some interfaces the numpad keys won't be recognized unless num-lock is active. So... if your NES software wants to use num-lock and the num-pad, consider giving a secondary way to toggle the NES software's num-lock state just in case the OS on the user's machine already has num-lock in the opposite state. (If your software doesn't use num-lock for anything, you don't need to worry about it. The user can toggle their own OS num-lock when they want to use the pad.)
---
Edit: Some extra notes on the international keys, after investigating by debugging Mesen, and using Windows 10's On-Screen Keyboard (system32/osk.exe) to use keys I don't have a keyboard for. Also referencing
Wikipedia's Language input keys article.
- Ro (International1) - JP-106/109 works as "|" in Mesen (154 OemBackslash)
- Kana (International2) - JP-106/109 reads as "OemCopy" in Mesen (159 dbehiragana), has down event and repeat, but no keyup!
- Yen (International3) - JP-106/109 works as "\" in Mesen (150 OemPipe)
- Henkan (International4) - JP-106/109 works as "IME convert" in Mesen (14 ImeConvert)
- Muhenkan (International5) - JP-106/109 works as "IME nonconvert" in Mesen (15 ImeNonConvert)
- Kp Jp Comma (International6) - ?
- Han/Yeong (LANG1) - KO-101 works as "Kana Mode" in Mesen (9 HangulMode)
- Hanja (LANG2) - KO-101 works as "Kanji Mode" in Mesen (12 KanjiMode)
- Katakana (LANG3) - ?
- Hiragana (LANG4) - ?
- Zenkaku/Hankaku (LANG5) - JP106/109 reads as "OemEnlw" or "OemAuto" in Mesen (161 DbeDbcsChar, 160 DbeSbcsChar). This is a toggle key, so each press gives a down for one, and up for the other, repeats the same keydown while held.
So, most of these work OK. Three of them are still kinda MIA, probably not found on non-specialized keyboards? Two of them are weird and probably problematic for emulators. The "Kana" key seems to have no keyup on my system, and I suspect it might be possible for this to report 158 DbeKatakana as well, but I couldn't get Mesen to read it as such. The "Han/Zen" key reports as two keys at once with a toggle, so it's not straightforward. However, both of these can simply be remapped to another key if more useful behaviour is needed. Probably nobody needs to use these two in their NES software anyway, but just a note that emulators might have a problem with them.
---
TLDR I think Memblers' spec is perfectly fine. I'd say stick with it.