PDA

View Full Version : Is there a way to convert Qt Key codes to Virtual-key codes (of windows)



rsilva
21st April 2011, 20:29
I'm making a system of hotkeys and need to make a "input" for it, like the win32 HOTKEY_CLASS.

But, the keys returned in Qt, with a example, using F1
return: 16777232
and the virtual key code of VK_F1 is 112

How can I translate Qt key to VK_* or how to make a control that can handle all hotkeys combinations ?

I'm subclassing line edit, and some of keys works, but, if I use Home, End, F* keys, it doesn't work, and return big numbers too like Qt::Key_F1 (not the same number).
And if I use Ctrl + Shift + Alt + L (or S) don't work... (with A, Z, Q, etc works)

I'm using this code to get the keys names (and it works when using VK_*)



QString GetKeyName(unsigned int virtualKey)
{
unsigned int scanCode = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);

// because MapVirtualKey strips the extended bit for some keys
switch (virtualKey)
{
case VK_LEFT: case VK_UP: case VK_RIGHT: case VK_DOWN: // arrow keys
case VK_PRIOR: case VK_NEXT: // page up and page down
case VK_END: case VK_HOME:
case VK_INSERT: case VK_DELETE:
case VK_DIVIDE: // numpad slash
case VK_NUMLOCK:
{
scanCode |= 0x100; // set extended bit
break;
}
}

char* keyName = new char[256];
QString name;

if (GetKeyNameTextA(scanCode << 16, keyName, 256) != 0)
{
name = keyName;
}

delete[] keyName;

return name;
}


can you help me ?

squidge
21st April 2011, 23:47
Trap QKeyEvent and use nativeVirtualKey() ?

See qt\src\gui\kernel\qkeymapper_win.cpp

rsilva
22nd April 2011, 00:39
Thank you, it worked with the nativeVirtualKey... i didn't know about it.
And now my problem trying to use a common control class is solved too, because I can do a similar control with this ^^