Qt 4.8
Public Functions | Private Functions | Properties | List of all members
QXlibKeyboard Class Reference

#include <qxlibkeyboard.h>

Public Functions

void changeLayout ()
 
void handleKeyEvent (QWidget *widget, QEvent::Type type, XKeyEvent *ev)
 
 QXlibKeyboard (QXlibScreen *screen)
 
Qt::KeyboardModifiers translateModifiers (int s)
 

Private Functions

void setMask (KeySym sym, uint mask)
 
int translateKeySym (uint key) const
 
QString translateKeySym (KeySym keysym, uint xmodifiers, int &code, Qt::KeyboardModifiers &modifiers, QByteArray &chars, int &count)
 

Properties

uint m_alt_mask
 
uint m_hyper_mask
 
uint m_meta_mask
 
uint m_mode_switch_mask
 
uint m_num_lock_mask
 
QXlibScreenm_screen
 
uint m_super_mask
 

Detailed Description

Definition at line 47 of file qxlibkeyboard.h.

Constructors and Destructors

◆ QXlibKeyboard()

QXlibKeyboard::QXlibKeyboard ( QXlibScreen screen)

Definition at line 943 of file qxlibkeyboard.cpp.

944  : m_screen(screen)
945  , m_alt_mask(0)
946  , m_super_mask(0)
947  , m_hyper_mask(0)
948  , m_meta_mask(0)
949 {
950  changeLayout();
951 }
QXlibScreen * m_screen
Definition: qxlibkeyboard.h:66

Functions

◆ changeLayout()

void QXlibKeyboard::changeLayout ( )

Definition at line 953 of file qxlibkeyboard.cpp.

Referenced by QXlibKeyboard().

954 {
955  XkbDescPtr xkbDesc = XkbGetMap(m_screen->display()->nativeDisplay(), XkbAllClientInfoMask, XkbUseCoreKbd);
956  for (int i = xkbDesc->min_key_code; i < xkbDesc->max_key_code; ++i) {
957  const uint mask = xkbDesc->map->modmap ? xkbDesc->map->modmap[i] : 0;
958  if (mask == 0) {
959  // key is not bound to a modifier
960  continue;
961  }
962 
963  for (int j = 0; j < XkbKeyGroupsWidth(xkbDesc, i); ++j) {
964  KeySym keySym = XkbKeySym(xkbDesc, i, j);
965  if (keySym == NoSymbol)
966  continue;
967  setMask(keySym, mask);
968  }
969  }
970  XkbFreeKeyboard(xkbDesc, XkbAllComponentsMask, true);
971 
972 }
Display * nativeDisplay() const
void setMask(KeySym sym, uint mask)
unsigned int uint
Definition: qglobal.h:996
QXlibScreen * m_screen
Definition: qxlibkeyboard.h:66
QXlibDisplay * display() const

◆ handleKeyEvent()

void QXlibKeyboard::handleKeyEvent ( QWidget widget,
QEvent::Type  type,
XKeyEvent *  ev 
)

Definition at line 990 of file qxlibkeyboard.cpp.

Referenced by QXlibScreen::handleEvent().

991 {
992  int qtcode = 0;
993  Qt::KeyboardModifiers modifiers = translateModifiers(ev->state);
994  QByteArray chars;
995  chars.resize(513);
996  int count = 0;
997  KeySym keySym;
998  count = XLookupString(ev,chars.data(),chars.size(),&keySym,0);
999  QString text = translateKeySym(keySym,ev->state,qtcode,modifiers,chars,count);
1000  QWindowSystemInterface::handleExtendedKeyEvent(widget,ev->time,type,qtcode,modifiers,ev->keycode,0,ev->state,text.left(count));
1001 }
int type
Definition: qmetatype.cpp:239
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
Qt::KeyboardModifiers translateModifiers(int s)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
static void handleExtendedKeyEvent(QWidget *w, QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, quint32 nativeScanCode, quint32 nativeVirtualKey, quint32 nativeModifiers, const QString &text=QString(), bool autorep=false, ushort count=1)
void resize(int size)
Sets the size of the byte array to size bytes.
int translateKeySym(uint key) const
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
#define text
Definition: qobjectdefs.h:80

◆ setMask()

void QXlibKeyboard::setMask ( KeySym  sym,
uint  mask 
)
private

Definition at line 760 of file qxlibkeyboard.cpp.

Referenced by changeLayout().

761 {
762  if (m_alt_mask == 0
763  && m_meta_mask != mask
764  && m_super_mask != mask
765  && m_hyper_mask != mask
766  && (sym == XK_Alt_L || sym == XK_Alt_R)) {
767  m_alt_mask = mask;
768  }
769  if (m_meta_mask == 0
770  && m_alt_mask != mask
771  && m_super_mask != mask
772  && m_hyper_mask != mask
773  && (sym == XK_Meta_L || sym == XK_Meta_R)) {
774  m_meta_mask = mask;
775  }
776  if (m_super_mask == 0
777  && m_alt_mask != mask
778  && m_meta_mask != mask
779  && m_hyper_mask != mask
780  && (sym == XK_Super_L || sym == XK_Super_R)) {
781  m_super_mask = mask;
782  }
783  if (m_hyper_mask == 0
784  && m_alt_mask != mask
785  && m_meta_mask != mask
786  && m_super_mask != mask
787  && (sym == XK_Hyper_L || sym == XK_Hyper_R)) {
788  m_hyper_mask = mask;
789  }
790  if (m_mode_switch_mask == 0
791  && m_alt_mask != mask
792  && m_meta_mask != mask
793  && m_super_mask != mask
794  && m_hyper_mask != mask
795  && sym == XK_Mode_switch) {
796  m_mode_switch_mask = mask;
797  }
798  if (m_num_lock_mask == 0
799  && sym == XK_Num_Lock) {
800  m_num_lock_mask = mask;
801  }
802 }
uint m_num_lock_mask
Definition: qxlibkeyboard.h:73
uint m_mode_switch_mask
Definition: qxlibkeyboard.h:72

◆ translateKeySym() [1/2]

int QXlibKeyboard::translateKeySym ( uint  key) const
private

Definition at line 804 of file qxlibkeyboard.cpp.

Referenced by handleKeyEvent(), and translateKeySym().

805 {
806  int code = -1;
807  int i = 0; // any other keys
808  while (KeyTbl[i]) {
809  if (key == KeyTbl[i]) {
810  code = (int)KeyTbl[i+1];
811  break;
812  }
813  i += 2;
814  }
815  if (m_meta_mask) {
816  // translate Super/Hyper keys to Meta if we're using them as the MetaModifier
817  if (m_meta_mask == m_super_mask && (code == Qt::Key_Super_L || code == Qt::Key_Super_R)) {
818  code = Qt::Key_Meta;
819  } else if (m_meta_mask == m_hyper_mask && (code == Qt::Key_Hyper_L || code == Qt::Key_Hyper_R)) {
820  code = Qt::Key_Meta;
821  }
822  }
823  return code;
824 }
static const unsigned int KeyTbl[]
int key

◆ translateKeySym() [2/2]

QString QXlibKeyboard::translateKeySym ( KeySym  keysym,
uint  xmodifiers,
int &  code,
Qt::KeyboardModifiers &  modifiers,
QByteArray chars,
int &  count 
)
private

Definition at line 826 of file qxlibkeyboard.cpp.

829 {
830  // all keysyms smaller than 0xff00 are actally keys that can be mapped to unicode chars
831 
833  QChar converted;
834 
835  if (/*count == 0 &&*/ keysym < 0xff00) {
836  unsigned char byte3 = (unsigned char)(keysym >> 8);
837  int mib = -1;
838  switch(byte3) {
839  case 0: // Latin 1
840  case 1: // Latin 2
841  case 2: //latin 3
842  case 3: // latin4
843  mib = byte3 + 4; break;
844  case 5: // arabic
845  mib = 82; break;
846  case 12: // Hebrew
847  mib = 85; break;
848  case 13: // Thai
849  mib = 2259; break;
850  case 4: // kana
851  case 6: // cyrillic
852  case 7: // greek
853  case 8: // technical, no mapping here at the moment
854  case 9: // Special
855  case 10: // Publishing
856  case 11: // APL
857  case 14: // Korean, no mapping
858  mib = -1; // manual conversion
859  mapper= 0;
860 #if !defined(QT_NO_XIM)
861  converted = keysymToUnicode(byte3, keysym & 0xff);
862 #endif
863  case 0x20:
864  // currency symbols
865  if (keysym >= 0x20a0 && keysym <= 0x20ac) {
866  mib = -1; // manual conversion
867  mapper = 0;
868  converted = (uint)keysym;
869  }
870  break;
871  default:
872  break;
873  }
874  if (mib != -1) {
875  mapper = QTextCodec::codecForMib(mib);
876  if (chars.isEmpty())
877  chars.resize(1);
878  chars[0] = (unsigned char) (keysym & 0xff); // get only the fourth bit for conversion later
879  count = 1;
880  }
881  } else if (keysym >= 0x1000000 && keysym <= 0x100ffff) {
882  converted = (ushort) (keysym - 0x1000000);
883  mapper = 0;
884  }
885  if (count < (int)chars.size()-1)
886  chars[count] = '\0';
887 
888  QString text;
889  if (!mapper && converted.unicode() != 0x0) {
890  text = converted;
891  } else if (!chars.isEmpty()) {
892  // convert chars (8bit) to text (unicode).
893  if (mapper)
894  text = mapper->toUnicode(chars.data(), count, 0);
895  if (text.isEmpty()) {
896  // no mapper, or codec couldn't convert to unicode (this
897  // can happen when running in the C locale or with no LANG
898  // set). try converting from latin-1
899  text = QString::fromLatin1(chars);
900  }
901  }
902 
903  modifiers = translateModifiers(xmodifiers);
904 
905  // Commentary in X11/keysymdef says that X codes match ASCII, so it
906  // is safe to use the locale functions to process X codes in ISO8859-1.
907  //
908  // This is mainly for compatibility - applications should not use the
909  // Qt keycodes between 128 and 255, but should rather use the
910  // QKeyEvent::text().
911  //
912  if (keysym < 128 || (keysym < 256 && (!mapper || mapper->mibEnum()==4))) {
913  // upper-case key, if known
914  code = isprint((int)keysym) ? toupper((int)keysym) : 0;
915  } else if (keysym >= XK_F1 && keysym <= XK_F35) {
916  // function keys
917  code = Qt::Key_F1 + ((int)keysym - XK_F1);
918  } else if (keysym >= XK_KP_Space && keysym <= XK_KP_9) {
919  if (keysym >= XK_KP_0) {
920  // numeric keypad keys
921  code = Qt::Key_0 + ((int)keysym - XK_KP_0);
922  } else {
923  code = translateKeySym(keysym);
924  }
925  modifiers |= Qt::KeypadModifier;
926  } else if (text.length() == 1 && text.unicode()->unicode() > 0x1f && text.unicode()->unicode() != 0x7f && !(keysym >= XK_dead_grave && keysym <= XK_dead_horn)) {
927  code = text.unicode()->toUpper().unicode();
928  } else {
929  // any other keys
930  code = translateKeySym(keysym);
931 
932  if (code == Qt::Key_Tab && (modifiers & Qt::ShiftModifier)) {
933  // map shift+tab to shift+backtab, QShortcutMap knows about it
934  // and will handle it.
935  code = Qt::Key_Backtab;
936  text = QString();
937  }
938  }
939 
940  return text;
941 }
static QTextCodec * codecForLocale()
Returns a pointer to the codec most suitable for this locale.
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
Qt::KeyboardModifiers translateModifiers(int s)
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
int keysym
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
virtual int mibEnum() const =0
Subclasses of QTextCodec must reimplement this function.
unsigned int uint
Definition: qglobal.h:996
int mib
QChar toUpper() const
Returns the uppercase equivalent if the character is lowercase or titlecase; otherwise returns the ch...
Definition: qchar.cpp:1287
static QTextCodec * codecForMib(int mib)
Returns the QTextCodec which matches the MIBenum mib.
#define XK_dead_horn
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
unsigned short ushort
Definition: qglobal.h:995
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
void resize(int size)
Sets the size of the byte array to size bytes.
int translateKeySym(uint key) const
static QChar keysymToUnicode(unsigned char byte3, unsigned char byte4)
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
#define text
Definition: qobjectdefs.h:80

◆ translateModifiers()

Qt::KeyboardModifiers QXlibKeyboard::translateModifiers ( int  s)

Definition at line 744 of file qxlibkeyboard.cpp.

Referenced by handleKeyEvent(), QXlibWindow::handleMouseEvent(), and translateKeySym().

745 {
746  Qt::KeyboardModifiers ret = 0;
747  if (s & ShiftMask)
748  ret |= Qt::ShiftModifier;
749  if (s & ControlMask)
750  ret |= Qt::ControlModifier;
751  if (s & m_alt_mask)
752  ret |= Qt::AltModifier;
753  if (s & m_meta_mask)
754  ret |= Qt::MetaModifier;
755 // if (s & m_mode_switch_mask) //doesn't seem to work correctly
756 // ret |= Qt::GroupSwitchModifier;
757  return ret;
758 }

Properties

◆ m_alt_mask

uint QXlibKeyboard::m_alt_mask
private

Definition at line 68 of file qxlibkeyboard.h.

Referenced by setMask(), and translateModifiers().

◆ m_hyper_mask

uint QXlibKeyboard::m_hyper_mask
private

Definition at line 70 of file qxlibkeyboard.h.

Referenced by setMask(), and translateKeySym().

◆ m_meta_mask

uint QXlibKeyboard::m_meta_mask
private

Definition at line 71 of file qxlibkeyboard.h.

Referenced by setMask(), translateKeySym(), and translateModifiers().

◆ m_mode_switch_mask

uint QXlibKeyboard::m_mode_switch_mask
private

Definition at line 72 of file qxlibkeyboard.h.

Referenced by setMask().

◆ m_num_lock_mask

uint QXlibKeyboard::m_num_lock_mask
private

Definition at line 73 of file qxlibkeyboard.h.

Referenced by setMask().

◆ m_screen

QXlibScreen* QXlibKeyboard::m_screen
private

Definition at line 66 of file qxlibkeyboard.h.

Referenced by changeLayout().

◆ m_super_mask

uint QXlibKeyboard::m_super_mask
private

Definition at line 69 of file qxlibkeyboard.h.

Referenced by setMask(), and translateKeySym().


The documentation for this class was generated from the following files: