问题
I have a low-level key logger that receives scan codes, without QKeyEvent's since the QApplication doesn't have focus. The scan codes can be converted into key syms using system specific library calls.
Scan codes correspond to Qt's QKeyEvent->nativeScanCode() and key syms correspond to QKeyEvent->nativeVirtualKey(), but Qt's Qt::Key values seems independently mapped. I would like to either take a given Qt::Key and convert to either sym key or scan code, or construct a Qt::Key out of a sym key or scan code, so that I can compare the captured keys with pre-determined Qt::Key's.
I've seen other projects that do this by implementing large, incomplete lookup table ref1 ref2. Surely if Qt is gathering scan codes and constructing QtKey's out of them it must have some internal mapping? I would like some way to avoid duplicating that. Is there any accessible Qt API to construct Qt::Keys from key syms or scan codes, or any non-public API Qt code that could be copied instead of relying on external projects?
回答1:
Unfortunately, there doesn't seem to be easy way to translate between Qt keys and native key codes.
You can use the same mapping Qt uses for X11. I use this table for QxtGlobalShortcut library.
Original table is from qxcbkeyboard.cpp. The source file contains some functions for converting native key codes for use in Qt. The problem is that the QXcb*
classes are not public, and from the looks of the headers, can break between minor Qt releases (so direct use is not advised).
来源:https://stackoverflow.com/questions/48592348/how-to-construct-qtkey-out-of-keysym-or-keycode