What's up with the name of the "Print Screen" key?

by Michael S. Kaplan, published on 2010/08/10 07:01 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2010/08/10/10047945.aspx


So, yesterday Satchmo, in response to my Flirting with a strange keyboard, will you remember its language in the morning? blog, asked:

Hi Michael, So why API GetKeyNameText() returns "Sys Req" instead of "Print Scrn" when using VK_SNAPSHOT? And why does it return "Num +" instead of "+" when using VK_ADD? I was thinking about using this function to display the keys in a settings window, so international users can customize the hotkeys of my program.

Of course the question was off-topic for that blog, but still interesting, so I thought I'd make it a topic for another blog.

This blog, in fact.....

Now to start let's look at the function signature for GetKeyNameText:

int WINAPI GetKeyNameText(
  __in   LONG lParam,
  __out  LPTSTR lpString,
  __in   int cchSize
);

and the meaning of that lParam parameter:

The second parameter of the keyboard message (such as WM_KEYDOWN) to be processed. The function interprets the following bit positions in the lParam.

BitsMeaning
16-23 Scan code.
24 Extended-key flag. Distinguishes some keys on an enhanced keyboard.
25 "Don't care" bit. The application calling this function sets this bit to indicate that the function should not distinguish between left and right CTRL and SHIFT keys, for example.

Now at risk of answering a question with a question, this provides a somewhat direct counter-question to Satchmo:

How are you sending VK_SNAPSHOT to a function that is taking scan codes and not virtual keys? :-)

More seriously, looking at a typical keyboard:

Clearly, one must pick a string for the name.

And the one the keyboard picks may not be so great but it is not "wrong" really.

That it is the keyboard picking here is described in Flirting with a strange keyboard, will you remember its language in the morning?, including the workaround if one wants to have a layout with a different set of names for keys....

Now if the underlying question is Michael, why did they do it this weird way? then the answer is that I have no idea what they were thinking of years ago when they did all of this. :-)

Now as to the larger question, when the Catalan issue I first described in Flirting with a strange keyboard, will you remember its language in the morning? first came up, I suggested that a better and more scalable solution was done, putting all of the key name text returning code into USER32.DLL directly with the resources thereof made localizable (which the current architecture doesn't allow). But it was WON'T FIXED and I do not hold out much hope of improvement....


Satchmo Pops on 11 Aug 2010 12:03 AM:

"How are you sending VK_SNAPSHOT to a function that is taking scan codes and not virtual keys?"

I first obtained a scan code for VK_SNAPSHOT using MapVirtualKey() and then I used GetKeyNameText() to get the key's label. At the beginning I was excited to use this function to get the localized key names of the keyboard. But the feature does not longer appeal to me since one cannot really get all the text/names from the keys (for example for the dual keys Print Scrn / Sys Rq I can only get Sys Req). Even more, I just found out that GetKeyNameText() is just *not accurate* at all.

Below is the output of the function changing to a Swedish input locale (LCID 0x041d) versus the report of a user using a physical Swedish keyboard.

Virtual Keys    English         Swedish (API) labels    Swedish (Real Keyboard) labels
---------------------------------------------------------------------------------
VK_CONTROL      Ctrl            CTRL                    Ctrl
VK_SHIFT        Shift           SKIFT                   <bold Up-arrow>
VK_MENU         Alt             ALT                     Alt
VK_SNAPSHOT     Sys Req         Sys Req                 SysRq
VK_BACK         Backspace       BACKSTEG                <backarrow>
VK_RETURN       Enter           RETUR                   Enter

* All outputs presented verbatim by letter/case except figurative text enclosed by <>.

Satchmo Pops on 11 Aug 2010 1:07 PM:

Michael, have you written or do you know of a good book about making programs for the international community? The subject does not seem to be easy, and there is plenty of very good details spread all over.

Until recently I started to be more concern about internationalization. Just few days ago I did not know about the existence of those API functions (although I know now that I cannot rely on them for stuff like displaying hotkeys or key accelerators). One more fact, I actually used vbKeySnapshot (the VBRUN.KeyCodeConstants from VB6) which is what I mostly code with (VB6 and VBA6). It seems to me that the best way will be to have resource files for every single language, and compile them with the EXE (one for every country).

Michael S. Kaplan on 11 Aug 2010 3:07 PM:

I previously wrote about the book(s) I'm not writing here.... and there is also info there about good (and not good) books....

Steve 'Sly' Williams on 11 Aug 2010 4:30 PM:

SysReq (or SysRq) is the un-shifted function of that key (like 5 is un-shifted and % is shifted on a US keyboard).  Print Screen is the shifted function of that key.  GetKeyNameText() returns the un-shifted name.

We used GetKeyNameText() to display game controls in Microsoft's Game Room.  We had to be careful of some key names that were really quite long compared to the short symbol on the physical key, but overall it fixed all localization issues we had with different keyboard layouts.


Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

go to newer or older post, or back to index or month or day