24 #include <QtCore/QCOORD>
26 #ifdef Q_WS_MACX // Only compile this module if we're compiling for Mac OS X
31 #include <Carbon/Carbon.h>
33 namespace KKeyServer {
39 static TransKey qtKeyToChar[] = {
40 {Qt::Key_Escape, kEscapeCharCode},
41 {Qt::Key_Tab, kTabCharCode},
42 {Qt::Key_Backtab, kTabCharCode},
43 {Qt::Key_Backspace, kBackspaceCharCode},
44 {Qt::Key_Return, kReturnCharCode},
45 {Qt::Key_Enter, kEnterCharCode},
47 {Qt::Key_Delete, kDeleteCharCode},
49 {Qt::Key_Clear, kClearCharCode},
50 {Qt::Key_Home, kHomeCharCode},
51 {Qt::Key_End, kEndCharCode},
52 {Qt::Key_Left, kLeftArrowCharCode},
53 {Qt::Key_Up, kUpArrowCharCode},
54 {Qt::Key_Right, kRightArrowCharCode},
55 {Qt::Key_Down, kDownArrowCharCode},
56 {Qt::Key_PageUp, kPageUpCharCode},
57 {Qt::Key_PageDown, kPageDownCharCode},
60 {Qt::Key_Help, kHelpCharCode},
62 {Qt::Key_nobreakspace, kNonBreakingSpaceCharCode},
67 static long lastLayoutID = -1;
68 #ifdef QT_MAC_USE_COCOA
69 static TISInputSourceRef lastLayout = 0;
71 static KeyboardLayoutRef lastLayout = NULL;
74 void updateScancodes() {
75 #ifdef QT_MAC_USE_COCOA
76 TISInputSourceRef layout = TISCopyCurrentKeyboardLayoutInputSource();
78 kWarning() <<
"Error retrieving current layout";
81 if (layout == lastLayout) {
86 const void *
name = TISGetInputSourceProperty(layout, kTISPropertyLocalizedName);
87 kDebug() <<
"Layout changed to: " << CFStringGetCStringPtr((CFStringRef)name, 0);
92 CFDataRef data =
static_cast<CFDataRef
>(TISGetInputSourceProperty(layout,
93 kTISPropertyUnicodeKeyLayoutData));
94 const UCKeyboardLayout *ucData = data ?
reinterpret_cast<const UCKeyboardLayout *
>(CFDataGetBytePtr(data)) : 0;
97 kWarning() <<
"Error retrieving current layout character data";
101 for (
int i = 0; i < 128; ++i) {
104 UniCharCount actualLength = 0;
105 OSStatus err = UCKeyTranslate(ucData, i, kUCKeyActionDown, 0, LMGetKbdType(),
106 kUCKeyTranslateNoDeadKeysMask, &tmpState, 4, &actualLength, str);
108 kWarning() <<
"Error translating unicode key" << err;
110 if (str[0] && str[0] != kFunctionKeyCharCode)
111 scancodes.insert(str[0], i);
116 KeyboardLayoutRef layout;
117 if (KLGetCurrentKeyboardLayout(&layout) != noErr) {
118 kWarning() <<
"Error retrieving current layout";
120 if (layout != lastLayout) {
123 KLGetKeyboardLayoutProperty(layout, kKLName, const_cast<const void**>(&name));
124 kDebug() <<
"Layout changed to: " << CFStringGetCStringPtr((CFStringRef) name, 0);
129 if (KLGetKeyboardLayoutProperty(layout, kKLKCHRData, const_cast<const void**>(&kchr)) != noErr) {
130 kWarning() <<
"Couldn't load active keyboard layout";
132 for (
int i = 0; i < 128; i++) {
134 UInt32 chr = KeyTranslate(kchr, i, &tmpState);
135 if (chr && chr != kFunctionKeyCharCode) {
136 scancodes.insert(chr, i);
144 #define SCANCODE(name, value) { Qt::Key_ ## name, value }
145 static TransKey functionKeys[] = {
164 if (keyQt >= 0x20 && keyQt < Qt::Key_A) {
169 if (keyQt >= Qt::Key_A && keyQt <= Qt::Key_Z) {
170 sym = keyQt - Qt::Key_A +
'a';
174 if (keyQt > Qt::Key_Z && keyQt <= 0x60) {
179 if (keyQt >= 0x7B && keyQt < 0x7F) {
184 if (keyQt >= Qt::Key_F1 && keyQt <= Qt::Key_F35) {
185 sym = kFunctionKeyCharCode;
189 for (
int i = 0; qtKeyToChar[i].qt_code; i++) {
190 if (qtKeyToChar[i].qt_code == keyQt) {
191 sym = qtKeyToChar[i].mac_code;
204 keyQt &= ~Qt::KeyboardModifierMask;
208 if (chr == kFunctionKeyCharCode) {
209 for (
int i = 0; functionKeys[i].qt_code; i++) {
210 if (functionKeys[i].qt_code == keyQt) {
211 keyCodes.
append(functionKeys[i].mac_code);
215 keyCodes += scancodes.values(chr);
218 return keyCodes.
count() > 0;
224 if (keyQt & Qt::ShiftModifier) {
227 if (keyQt & Qt::ControlModifier) {
230 if (keyQt & Qt::AltModifier) {
233 if (keyQt & Qt::MetaModifier) {
236 if (keyQt & Qt::KeypadModifier) {
237 mod |= kEventKeyModifierNumLockMask;
240 if ((keyQt & ~Qt::KeyboardModifierMask) == Qt::Key_Backtab) {
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Defines platform-independent classes for keyboard shortcut handling.
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
bool keyQtToCodeMac(int keyQt, QList< uint > &keyCodes)
Extracts all the scancodes from the given Qt key.
int count(const T &value) const
void append(const T &value)
bool keyQtToModMac(int keyQt, uint &mod)
Extracts the modifiers from the given Qt key and converts them in a mask of OSX modifiers.
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
bool keyQtToSymMac(int keyQt, int &sym)
Extracts the symbol from the given Qt key, and converts it to an OSX symbol.