File indexing completed on 2023-12-03 04:12:00

0001 /*
0002     SPDX-FileCopyrightText: 2008 Carlo Segato <brandon.ml@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "kkeyserver_win.h"
0008 
0009 #include <windows.h>
0010 
0011 #include <QDebug>
0012 
0013 namespace KKeyServer
0014 {
0015 struct TransKey {
0016     uint keySymQt;
0017     uint keySymWin;
0018 };
0019 
0020 static const TransKey KeyTbl[] = {
0021     {Qt::Key_Cancel, VK_CANCEL},
0022     {Qt::Key_Backspace, VK_BACK},
0023     {Qt::Key_Tab, VK_TAB},
0024     {Qt::Key_Clear, VK_CLEAR},
0025     {Qt::Key_Return, VK_RETURN},
0026     {Qt::Key_Shift, VK_SHIFT},
0027     {Qt::Key_Control, VK_CONTROL},
0028     {Qt::Key_Alt, VK_MENU},
0029     {Qt::Key_Pause, VK_PAUSE},
0030     {Qt::Key_CapsLock, VK_CAPITAL},
0031     {Qt::Key_Escape, VK_ESCAPE},
0032 // FIXME: under wince VK_MODECHANGE is not defined
0033 #ifndef _WIN32_WCE
0034     {Qt::Key_Mode_switch, VK_MODECHANGE},
0035 #endif
0036     {Qt::Key_Space, VK_SPACE},
0037     {Qt::Key_PageUp, VK_PRIOR},
0038     {Qt::Key_PageDown, VK_NEXT},
0039     {Qt::Key_End, VK_END},
0040     {Qt::Key_Home, VK_HOME},
0041     {Qt::Key_Left, VK_LEFT},
0042     {Qt::Key_Up, VK_UP},
0043     {Qt::Key_Right, VK_RIGHT},
0044     {Qt::Key_Down, VK_DOWN},
0045     {Qt::Key_Select, VK_SELECT},
0046     {Qt::Key_Printer, VK_PRINT},
0047     {Qt::Key_Execute, VK_EXECUTE},
0048     {Qt::Key_Print, VK_SNAPSHOT},
0049     {Qt::Key_Insert, VK_INSERT},
0050     {Qt::Key_Delete, VK_DELETE},
0051     {Qt::Key_Help, VK_HELP},
0052     /*        { 0,            VK_0 },
0053             { 0,            VK_1 },
0054             { 0,            VK_2 },
0055             { 0,            VK_3 },
0056             { 0,            VK_4 },
0057             { 0,            VK_5 },
0058             { 0,            VK_6 },
0059             { 0,            VK_7 },
0060             { 0,            VK_8 },
0061             { 0,            VK_9 },
0062             { 0,            VK_A },
0063             { 0,            VK_B },
0064             { 0,            VK_C },
0065             { 0,            VK_D },
0066             { 0,            VK_E },
0067             { 0,            VK_F },
0068             { 0,            VK_G },
0069             { 0,            VK_H },
0070             { 0,            VK_I },
0071             { 0,            VK_J },
0072             { 0,            VK_K },
0073             { 0,            VK_L },
0074             { 0,            VK_M },
0075             { 0,            VK_N },
0076             { 0,            VK_O },
0077             { 0,            VK_P },
0078             { 0,            VK_Q },
0079             { 0,            VK_R },
0080             { 0,            VK_S },
0081             { 0,            VK_T },
0082             { 0,            VK_U },
0083             { 0,            VK_V },
0084             { 0,            VK_W },
0085             { 0,            VK_X },
0086             { 0,            VK_Y },
0087             { 0,            VK_Z },*/
0088     {Qt::Key_Meta, VK_LWIN},
0089     {Qt::Key_Meta, VK_RWIN},
0090     {Qt::Key_Menu, VK_APPS},
0091     {Qt::Key_Sleep, VK_SLEEP},
0092     {Qt::Key_0, VK_NUMPAD0},
0093     {Qt::Key_1, VK_NUMPAD1},
0094     {Qt::Key_2, VK_NUMPAD2},
0095     {Qt::Key_3, VK_NUMPAD3},
0096     {Qt::Key_4, VK_NUMPAD4},
0097     {Qt::Key_5, VK_NUMPAD5},
0098     {Qt::Key_6, VK_NUMPAD6},
0099     {Qt::Key_7, VK_NUMPAD7},
0100     {Qt::Key_8, VK_NUMPAD8},
0101     {Qt::Key_9, VK_NUMPAD9},
0102     {Qt::Key_Asterisk, VK_MULTIPLY},
0103     {Qt::Key_Plus, VK_ADD},
0104     {Qt::Key_Comma, VK_SEPARATOR},
0105     {Qt::Key_Minus, VK_SUBTRACT},
0106     {Qt::Key_Period, VK_DECIMAL},
0107     {Qt::Key_Slash, VK_DIVIDE},
0108     {Qt::Key_F1, VK_F1},
0109     {Qt::Key_F2, VK_F2},
0110     {Qt::Key_F3, VK_F3},
0111     {Qt::Key_F4, VK_F4},
0112     {Qt::Key_F5, VK_F5},
0113     {Qt::Key_F6, VK_F6},
0114     {Qt::Key_F7, VK_F7},
0115     {Qt::Key_F8, VK_F8},
0116     {Qt::Key_F9, VK_F9},
0117     {Qt::Key_F10, VK_F10},
0118     {Qt::Key_F11, VK_F11},
0119     {Qt::Key_F12, VK_F12},
0120     {Qt::Key_F13, VK_F13},
0121     {Qt::Key_F14, VK_F14},
0122     {Qt::Key_F15, VK_F15},
0123     {Qt::Key_F16, VK_F16},
0124     {Qt::Key_F17, VK_F17},
0125     {Qt::Key_F18, VK_F18},
0126     {Qt::Key_F19, VK_F19},
0127     {Qt::Key_F20, VK_F20},
0128     {Qt::Key_F21, VK_F21},
0129     {Qt::Key_F22, VK_F22},
0130     {Qt::Key_F23, VK_F23},
0131     {Qt::Key_F24, VK_F24},
0132     {Qt::Key_NumLock, VK_NUMLOCK},
0133     {Qt::Key_ScrollLock, VK_SCROLL},
0134     //         { 0,            VK_OEM_FJ_JISHO },
0135     //         { Qt::Key_Massyo,           VK_OEM_FJ_MASSHOU },
0136     //         { Qt::Key_Touroku,          VK_OEM_FJ_TOUROKU },
0137     //         { 0,            VK_OEM_FJ_LOYA },
0138     //         { 0,            VK_OEM_FJ_ROYA },
0139     {Qt::Key_Shift, VK_LSHIFT},
0140     {Qt::Key_Shift, VK_RSHIFT},
0141     {Qt::Key_Control, VK_LCONTROL},
0142     {Qt::Key_Control, VK_RCONTROL},
0143     {Qt::Key_Alt, VK_LMENU},
0144     {Qt::Key_Alt, VK_RMENU},
0145 
0146 //  winuser.h from psdk w2kserver2003R2 defines the following constants only for _WIN32_WINNT >= 0x0500
0147 // and we should also do so - otherwise the constants may not be available
0148 #if (_WIN32_WINNT >= 0x0500)
0149     {Qt::Key_Back, VK_BROWSER_BACK},
0150     {Qt::Key_Forward, VK_BROWSER_FORWARD},
0151     {Qt::Key_Refresh, VK_BROWSER_REFRESH},
0152     {Qt::Key_Stop, VK_BROWSER_STOP},
0153     {Qt::Key_Search, VK_BROWSER_SEARCH},
0154     {Qt::Key_Favorites, VK_BROWSER_FAVORITES},
0155     {Qt::Key_HomePage, VK_BROWSER_HOME},
0156     {Qt::Key_VolumeMute, VK_VOLUME_MUTE},
0157     {Qt::Key_VolumeDown, VK_VOLUME_DOWN},
0158     {Qt::Key_VolumeUp, VK_VOLUME_UP},
0159     {Qt::Key_MediaNext, VK_MEDIA_NEXT_TRACK},
0160     {Qt::Key_MediaPrevious, VK_MEDIA_PREV_TRACK},
0161     {Qt::Key_MediaStop, VK_MEDIA_STOP},
0162     {Qt::Key_MediaPlay, VK_MEDIA_PLAY_PAUSE},
0163     {Qt::Key_LaunchMail, VK_LAUNCH_MAIL},
0164     {Qt::Key_LaunchMedia, VK_LAUNCH_MEDIA_SELECT},
0165     {Qt::Key_Launch0, VK_LAUNCH_APP1},
0166     {Qt::Key_Launch1, VK_LAUNCH_APP2},
0167 #endif
0168     /*        { 0,            VK_OEM_1 },
0169             { 0,            VK_OEM_PLUS },
0170             { 0,            VK_OEM_COMMA },
0171             { 0,            VK_OEM_MINUS },
0172             { 0,            VK_OEM_PERIOD },
0173             { 0,            VK_OEM_2 },
0174             { 0,            VK_OEM_3 },
0175             { 0,            VK_OEM_4 },
0176             { 0,            VK_OEM_5 },
0177             { 0,            VK_OEM_6 },
0178             { 0,            VK_OEM_7 },
0179             { 0,            VK_OEM_8 },*/
0180     {Qt::Key_Play, VK_PLAY},
0181     {Qt::Key_Zoom, VK_ZOOM},
0182     {Qt::Key_Clear, VK_OEM_CLEAR},
0183     //         { 0
0184 };
0185 
0186 bool keyQtToModWin(int keyQt, uint *mod)
0187 {
0188     *mod = 0;
0189     if (keyQt & Qt::ShiftModifier) {
0190         *mod |= MOD_SHIFT;
0191     }
0192     if (keyQt & Qt::ControlModifier) {
0193         *mod |= MOD_CONTROL;
0194     }
0195     if (keyQt & Qt::AltModifier) {
0196         *mod |= MOD_ALT;
0197     }
0198     if (keyQt & Qt::MetaModifier) {
0199         *mod |= MOD_WIN;
0200     }
0201 
0202     return true;
0203 }
0204 
0205 bool modWinToKeyQt(uint mod, int *keyQt)
0206 {
0207     *keyQt = 0;
0208     if (mod & MOD_SHIFT) {
0209         *keyQt |= Qt::ShiftModifier;
0210     }
0211     if (mod & MOD_CONTROL) {
0212         *keyQt |= Qt::ControlModifier;
0213     }
0214     if (mod & MOD_ALT) {
0215         *keyQt |= Qt::AltModifier;
0216     }
0217     if (mod & MOD_WIN) {
0218         *keyQt |= Qt::MetaModifier;
0219     }
0220 
0221     return true;
0222 }
0223 
0224 bool keyQtToCodeWin(int keyQt, uint *sym)
0225 {
0226     int symQt = keyQt & ~Qt::KeyboardModifierMask;
0227 
0228     if (symQt >= Qt::Key_A && symQt <= Qt::Key_Z) {
0229         *sym = symQt;
0230         return true;
0231     }
0232 
0233     for (uint i = 0; i < sizeof(KeyTbl); i++) {
0234         if (KeyTbl[i].keySymQt == symQt) {
0235             *sym = KeyTbl[i].keySymWin;
0236             return true;
0237         }
0238     }
0239 
0240     *sym = 0;
0241     if (symQt != Qt::Key_Shift && symQt != Qt::Key_Control && symQt != Qt::Key_Alt && symQt != Qt::Key_Meta && symQt != Qt::Key_Direction_L
0242         && symQt != Qt::Key_Direction_R) {
0243         // qDebug() << "Sym::initQt( " << QString::number(keyQt,16) << " ): failed to convert key.";
0244     }
0245     return false;
0246 }
0247 
0248 bool codeWinToKeyQt(uint sym, int *keyQt)
0249 {
0250     if (sym >= Qt::Key_A && sym <= Qt::Key_Z) {
0251         *keyQt = sym;
0252         return true;
0253     }
0254 
0255     for (uint i = 0; i < sizeof(KeyTbl); i++) {
0256         if (KeyTbl[i].keySymWin == sym) {
0257             *keyQt = KeyTbl[i].keySymQt;
0258             return true;
0259         }
0260     }
0261 
0262     *keyQt = 0;
0263     return false;
0264 }
0265 
0266 }