Russian translation. Gui fix.
[qstardict] / qstardict / keyboard.cpp
1 /*****************************************************************************
2  * keyboard.cpp - QStarDict, a StarDict clone written with using Qt          *
3  * Copyright (C) 2007 Alexander Rodin                                        *
4  *                                                                           *
5  * This program is free software; you can redistribute it and/or modify      *
6  * it under the terms of the GNU General Public License as published by      *
7  * the Free Software Foundation; either version 2 of the License, or         *
8  * (at your option) any later version.                                       *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU General Public License for more details.                              *
14  *                                                                           *
15  * You should have received a copy of the GNU General Public License along   *
16  * with this program; if not, write to the Free Software Foundation, Inc.,   *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
18  *****************************************************************************/
19
20 #include "keyboard.h"
21
22 #ifdef Q_WS_X11
23
24 #include <QX11Info>
25 #include <X11/XKBlib.h>
26 #include <stdio.h>
27
28 namespace
29 {
30 const unsigned mAlt     = 0010;
31 const unsigned mCtrl    = 0004;
32 const unsigned mShift   = 0001;
33 const unsigned mWin     = 0100;
34 }
35
36 namespace QStarDict 
37 {
38
39 Qt::KeyboardModifiers Keyboard::activeModifiers()
40 {
41     XkbStateRec state;
42     Qt::KeyboardModifiers result;
43
44     XkbGetState(QX11Info::display(), XkbUseCoreKbd, &state);
45     if (state.base_mods & mAlt)
46         result |= Qt::AltModifier;
47     if (state.base_mods & mCtrl)
48         result |= Qt::ControlModifier;
49     if (state.base_mods & mShift)
50         result |= Qt::ShiftModifier;
51     if (state.base_mods & mWin)
52         result |= Qt::MetaModifier;
53
54     return result;
55 }
56
57 } // namespace
58
59 #elif defined(Q_WS_WIN) // Q_WS_X11
60
61 #include <windows.h>
62 #include <winuser.h>
63
64 namespace QStarDict
65 {
66
67 Qt::KeyboardModifiers Keyboard::activeModifiers()
68 {
69     Qt::KeyboardModifiers result;
70
71     if (GetAsyncKeyState(VK_MENU) & 0x8000)
72         result |= Qt::AltModifier;
73     if (GetAsyncKeyState(VK_CONTROL) & 0x8000)
74         result |= Qt::ControlModifier;
75     if (GetAsyncKeyState(VK_SHIFT) & 0x8000)
76         result |= Qt::ShiftModifier;
77     if ((GetAsyncKeyState(VK_LWIN) & 0x8000) || (GetAsyncKeyState(VK_RWIN) & 0x8000))
78         result |= Qt::MetaModifier;
79
80     return result;
81 }
82
83 } // namespace
84
85 #elif defined(Q_WS_MAC) // Q_WS_WIN
86 #include <QApplication>
87
88 namespace QStarDict
89 {
90
91 Qt::KeyboardModifiers Keyboard::activeModifiers()
92 {
93     return QApplication::keyboardModifiers();
94 }
95
96 } // namespace
97 #endif // Q_WS_MAC
98
99 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
100