GetKeyNameText - Is the API or Buggy or is it Me?

This is a discussion on GetKeyNameText - Is the API or Buggy or is it Me? within the Windows Programming forums, part of the Platform Specific Boards category; Hi there, I'm trying to convert a virtual key (vk) code into a simple string, but I'm having problems. Here ...

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    GetKeyNameText - Is the API or Buggy or is it Me?

    Hi there,

    I'm trying to convert a virtual key (vk) code into a simple string, but I'm having problems. Here is my code:

    Code:
      std::string rslt;
      unsigned int sc = MapVirtualKey(vk, 0);
    
      sc <<= 16;
    
      char buf[256];
      if (GetKeyNameTextA(sc, buf, 256) > 0)
        rslt = buf;
    It works for some key code values, but there are dozens where it inexplicably returns errorneours values. For example, PAGE_UP returns the string "NUM 9", PAGE_DOWN returns give "NUM 3", DELETE gives "NUM DECIMAL".

    I have tried setting bits 24 & 25 of the scan code, but that appears to make things worse.

    I read somewhere that GetKeyNameText has a bug where it returns a wrong value for the numeric keypad divide, however in my case, it is not just one character which is wrong.

    Am I doing something wrong? Can anyone help?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    here's some tests i did.

    I ran a win32 program and put that code in my WM_KEYDOWN handler, and it seemed to work fine (I hadn't seen this function before, definitely remembering it though, thanks)

    but anyways, here's some testing I did, I displayed the string value and the hex value of lParam (which I passed to GetKeyNameText (not really sure why you're using GetKeyNameTextA...most compilers put stuff in to decide which function you should be using)) and here's what I got:
    ...wow, I just nested my parenthesis....

    Code:
      key:	  binary value of lParam
      F:		10 0001 0000 0000 0000 0001
      Page Up: 1 0100 1001 0000 0000 0000 0001
      Num 9:	  100 1001 0000 0000 0000 0001
      F1:	   11 1011 0000 0000 0000 0001
    -edit- I give up trying to line this up, *shakes fist* -edit-

    so it seems shifting the first 16 bits is right, but you need to set the first bit or something.....possibly that has to do with it somehow? I'm noticing your shifting the bits to the left 16 bits, but not setting the first bit....

    -hope that helps
    Last edited by jverkoey; 08-26-2004 at 02:52 AM.

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks for the reply. Your output values gave me some ideas. By purely empirical means, the following code seems to be working:

    Code:
      unsigned int sc = MapVirtualKey(vk, 0);
    
      // Is vk ASCII?
      char buf[256];
      memset(buf, 0, 256);
      unsigned short int temp;
      bool asc = (vk <= 32);
      if (!asc && vk != VK_DIVIDE)
       asc = ToAscii(vk, sc, buf, &temp, 1);
    
      // Set bits
      sc <<= 16;
      sc |= 0x1 << 25;  // <- don't care
      if (!asc) sc |= 0x1 << 24; // <- extended bit
    
      // Convert to ANSI string
      if (GetKeyNameTextA(sc, buf, 256) > 0)
        rslt = buf;
    Although I haven't fully tested it with every possible key value. Also I can't say I fully understand it. It seems I should only set bit 24 if the VK is not ASCII. So if there is a better way to do this, please let me know anyone.

    Thanks again jverkoey.
    Last edited by Davros; 08-26-2004 at 04:09 AM.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Want to learn Windows API for Game Programming
    By George M. in forum Windows Programming
    Replies: 15
    Last Post: 09-28-2008, 11:26 AM
  2. platform specific API or C standard API
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 01:32 AM
  3. OpenSSL and Win32 SSL API :: SSL/TLS
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-10-2004, 07:46 PM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 07:51 PM
  5. Is it foolish to program with Win32 API ?
    By Kelvin in forum Windows Programming
    Replies: 2
    Last Post: 07-09-2002, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21