2

I got problem when one of my customer complains about they use azerty keyboard and they can't use my product. So I decide to use scan code instead of virtual key. I found the function MapVirtualKey is really useful for me to achieve that. But in some situation, I don't want to use MapVirtualKey function but use the number itself, like {if(isKeyDown[30])return 'A';} but I go around the internet and realize that some source tell that their keyboard scan code is not like mine, like in this image Not like my keyboard I don't understand it, why it's different from my keyboard's scan code, and even different from this MS scan code table

So I really wonder, Is it safe to use

if (isDownNow[48])
    return 'B';

instead of

if (isDownNow[MapVirtualKey('B', MAPVK_VK_TO_VSC)])
    return 'B';

Thank you for reading :)

Edit: I think I have a solution for the problem above, instead of call MapVirtualKey every time, I will store the map in an array. But new problem comes up. I don't have a azerty keyboard so I can't test this, I only have a qwerty keyboard, so I got confuse on this problem: I got this function to store my map.

void MapKeyData()
{
 for (int i = 0; i < KEYS_SIZE; i++)//KEYS_SIZE=255
   mapKey[i] = MapVirtualKey(i, MAPVK_VK_TO_VSC);//mapKey is unsigned char array
}

qwerty vs azerty But I want the Z in normal keyboard (qwerty keyboard) map with the W in azerty keyboard. But as the function MapKeyData above, I think the Z in qwerty keyboard still map with the Z in azerty keyboard which is definitely not my purpose, I want to keep the keyboard layout, not the key itself. But as I said, I don't know if the scan code is the same on every keyboard as the first picture show that the keycode different from my scan code.

Thank for reading :)

123iamking
  • 2,387
  • 4
  • 36
  • 56

2 Answers2

2

Yes, scan code is the same on all keyboard layout. I've tested this fact by changing my keyboard layout to other layout with windows settings.

So to solve the different keyboard layout across PCs, I created an array of unsigned char that stores the scan code of keys: The index of the item is the key on my standard QWERTY keyboard, the value of the item is the scan code. This way, I can easily map the key on my QWERTY to its scan code so that I can work with the scan code.

mapKey['A'] = 30; //Key 'A' on QWERTY keyboard has scan code = 30
mapKey['B'] = 48;
mapKey['C'] = 46;
mapKey['D'] = 32;
mapKey['E'] = 18;
mapKey['F'] = 33;
mapKey['G'] = 34;
mapKey['H'] = 35;
mapKey['I'] = 23;
mapKey['J'] = 36;
mapKey['K'] = 37;
mapKey['L'] = 38;
mapKey['M'] = 50;
mapKey['N'] = 49;
mapKey['O'] = 24;
mapKey['P'] = 25;
mapKey['Q'] = 16;
mapKey['R'] = 19;
mapKey['S'] = 31;
mapKey['T'] = 20;
mapKey['U'] = 22;
mapKey['V'] = 47;
mapKey['W'] = 17;
mapKey['X'] = 45;
mapKey['Y'] = 21;
mapKey['Z'] = 44;
mapKey[VK_LEFT] = 75;
mapKey[VK_RIGHT] = 77;
mapKey[VK_RETURN] = 28;
... more if needed
123iamking
  • 2,387
  • 4
  • 36
  • 56
0

See my answer here -- tl;dr: use WM_CHAR, not MapVirtualKey

Marc Durdin
  • 1,675
  • 2
  • 20
  • 27