Get friendly name from VK_ keycode?
-
wrote on 8 Mar 2022, 01:47 last edited by
I'm porting an app from MFC to Qt. Right now we're using a hybrid approach and the main window is still MFC. We currently have a system where the user can define custom keyboard shortcuts which are then stored in the registry as VK_XXX keycodes. I need to leave the way the keys are stored as-is because I'm not ready to port the main window yet, but I'm working on porting the Options dialog over to Qt. So somehow I need to convert the VK_XXX code to a friendly name using Qt so I can display it in the QTreeWidget I use for displaying them in the options dialog. In the MFC code I use this...
UINT scanCode = MapVirtualKey(keyCode, MAPVK_VK_TO_VSC); CString sKeyName; int result = 0; switch (keyCode) { case VK_LEFT: case VK_UP: case VK_RIGHT: case VK_DOWN: case VK_PRIOR: case VK_NEXT: case VK_END: case VK_HOME: case VK_INSERT: case VK_DELETE: case VK_DIVIDE: case VK_NUMLOCK: scanCode |= KF_EXTENDED; default: result = GetKeyNameText(scanCode << 16, sKeyName.GetBuffer(128), 128); sKeyName.ReleaseBuffer(); } return sKeyName;
Is there anyway to do the same thing in Qt? I just need some way to convert the VK_XXX code to a friendly name as a QString. I can't use that existing code because MapVirtualKey and GetKeyNameText both require winuser.h which causes all sorts of compile errors in my Qt dialog class.
-
I'm porting an app from MFC to Qt. Right now we're using a hybrid approach and the main window is still MFC. We currently have a system where the user can define custom keyboard shortcuts which are then stored in the registry as VK_XXX keycodes. I need to leave the way the keys are stored as-is because I'm not ready to port the main window yet, but I'm working on porting the Options dialog over to Qt. So somehow I need to convert the VK_XXX code to a friendly name using Qt so I can display it in the QTreeWidget I use for displaying them in the options dialog. In the MFC code I use this...
UINT scanCode = MapVirtualKey(keyCode, MAPVK_VK_TO_VSC); CString sKeyName; int result = 0; switch (keyCode) { case VK_LEFT: case VK_UP: case VK_RIGHT: case VK_DOWN: case VK_PRIOR: case VK_NEXT: case VK_END: case VK_HOME: case VK_INSERT: case VK_DELETE: case VK_DIVIDE: case VK_NUMLOCK: scanCode |= KF_EXTENDED; default: result = GetKeyNameText(scanCode << 16, sKeyName.GetBuffer(128), 128); sKeyName.ReleaseBuffer(); } return sKeyName;
Is there anyway to do the same thing in Qt? I just need some way to convert the VK_XXX code to a friendly name as a QString. I can't use that existing code because MapVirtualKey and GetKeyNameText both require winuser.h which causes all sorts of compile errors in my Qt dialog class.
wrote on 8 Mar 2022, 02:23 last edited by Pl45m4 3 Aug 2022, 02:49@Dan203 said in Get friendly name from VK_ keycode?:
Is there anyway to do the same thing in Qt? I just need some way to convert the VK_XXX code to a friendly name as a QString.
Hi, what about a simple
QMap<QString, VK_Key>
whereVK_Key
could be an enum with your settings?Edit:
You could also combine the map with a
Q_ENUM
declared enum... Then you can easily translate your enum "key" to a real string (orQString
) without dealing with the default enum ints.
(Maybe you dont even need theQMap
then... just the enum and do the translation afterwards)enum VK_Key { VK_LEFT, VK_UP, VK_RIGHT }; Q_ENUM(VK_Key)
QMetaEnum vkEnum = QMetaEnum::fromType<YourClass::VK_Key>(); // With this metaEnum you can translate keys to "their" strings QString string_left = vkEnum.valueToKey(YourClass::VK_LEFT); // this could be mapped (if you need to) using a QMap to, "Left arrow key" or whatever description.
-
@Dan203 said in Get friendly name from VK_ keycode?:
Is there anyway to do the same thing in Qt? I just need some way to convert the VK_XXX code to a friendly name as a QString.
Hi, what about a simple
QMap<QString, VK_Key>
whereVK_Key
could be an enum with your settings?Edit:
You could also combine the map with a
Q_ENUM
declared enum... Then you can easily translate your enum "key" to a real string (orQString
) without dealing with the default enum ints.
(Maybe you dont even need theQMap
then... just the enum and do the translation afterwards)enum VK_Key { VK_LEFT, VK_UP, VK_RIGHT }; Q_ENUM(VK_Key)
QMetaEnum vkEnum = QMetaEnum::fromType<YourClass::VK_Key>(); // With this metaEnum you can translate keys to "their" strings QString string_left = vkEnum.valueToKey(YourClass::VK_LEFT); // this could be mapped (if you need to) using a QMap to, "Left arrow key" or whatever description.
wrote on 8 Mar 2022, 03:37 last edited by@Pl45m4 said in Get friendly name from VK_ keycode?:
@Dan203 said in Get friendly name from VK_ keycode?:
Is there anyway to do the same thing in Qt? I just need some way to convert the VK_XXX code to a friendly name as a QString.
Hi, what about a simple
QMap<QString, VK_Key>
whereVK_Key
could be an enum with your settings?Edit:
You could also combine the map with a
Q_ENUM
declared enum... Then you can easily translate your enum "key" to a real string (orQString
) without dealing with the default enum ints.
(Maybe you dont even need theQMap
then... just the enum and do the translation afterwards)enum VK_Key { VK_LEFT, VK_UP, VK_RIGHT }; Q_ENUM(VK_Key)
QMetaEnum vkEnum = QMetaEnum::fromType<YourClass::VK_Key>(); // With this metaEnum you can translate keys to "their" strings QString string_left = vkEnum.valueToKey(YourClass::VK_LEFT); // this could be mapped (if you need to) using a QMap to, "Left arrow key" or whatever description.
I found a map similar to this in the Qt source. Or more specifically one that converts from VK to a QKey value. I just don’t see anyway to access it from the main API. I'll just copy it if I need to, but I was hoping there was some official way to do this that I was just missing.
-
@Pl45m4 said in Get friendly name from VK_ keycode?:
@Dan203 said in Get friendly name from VK_ keycode?:
Is there anyway to do the same thing in Qt? I just need some way to convert the VK_XXX code to a friendly name as a QString.
Hi, what about a simple
QMap<QString, VK_Key>
whereVK_Key
could be an enum with your settings?Edit:
You could also combine the map with a
Q_ENUM
declared enum... Then you can easily translate your enum "key" to a real string (orQString
) without dealing with the default enum ints.
(Maybe you dont even need theQMap
then... just the enum and do the translation afterwards)enum VK_Key { VK_LEFT, VK_UP, VK_RIGHT }; Q_ENUM(VK_Key)
QMetaEnum vkEnum = QMetaEnum::fromType<YourClass::VK_Key>(); // With this metaEnum you can translate keys to "their" strings QString string_left = vkEnum.valueToKey(YourClass::VK_LEFT); // this could be mapped (if you need to) using a QMap to, "Left arrow key" or whatever description.
I found a map similar to this in the Qt source. Or more specifically one that converts from VK to a QKey value. I just don’t see anyway to access it from the main API. I'll just copy it if I need to, but I was hoping there was some official way to do this that I was just missing.
-
wrote on 8 Mar 2022, 16:08 last edited by
@Pl45m4 said in Get friendly name from VK_ keycode?:
Have you seen this?
Sounds similar, but nativeVirtualKey is used to convert from a QKey to a VK_ code, so it's the opposite of what I need. I need to convert the VK_ code to a QKey so I can use the QKeySequence::toString function to get the name of the key.
Once I convert the main window this wont matter, but that’s a big project and wont happen until the next major release. Right now I'm just trying to do as many of the secondary dialogs as I can.
-
wrote on 9 Mar 2022, 16:38 last edited by
I ended up pulling the array from the windows keymapper cpp and filling in some of the gaps manually. I also had to subclass the QKeySequenceEdit control so that it worked properly with shifted characters. But now it's all working and I can move to the next dialog
1/6