MagicSmoke  $VERSION$
bcskeyboard.h
Go to the documentation of this file.
1 //
2 // C++ Interface: plugin for USB barcode scanners: Scanner Implementation
3 //
4 // Description:
5 //
6 //
7 // Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2013-2014
8 //
9 // Copyright: See README/COPYING.GPL files that come with this distribution
10 //
11 //
12 
13 #ifndef MAGICSMOKE_BCSUSB_KBD_H
14 #define MAGICSMOKE_BCSUSB_KBD_H
15 
16 #include <QList>
17 #include <QObject>
18 #include <QFlag>
19 #include <QDateTime>
20 #include <QMap>
21 
22 class MKey
23 {
24  public:
25  enum Modifier {
26  None = 0,
27  Ctrl = 1,
28  Shift = 2,
29  Alt = 4,
30  AltGr = 8,
31  GUI = 16,
32  };
33  Q_DECLARE_FLAGS(Modifiers,Modifier);
34 
35  enum KeyType{
37  CharKey = 1,
38  CapsLock = 2,
39  NumLock = 3,
40  Backspace = 4,
41  Return = 5,
42  Tabulator = 6,
43  Space = 7,
45  Escape = 9,
46  };
47 
48  MKey(){}
49  MKey(const MKey&k,Modifiers m);
50  MKey(const MKey&)=default;
51  MKey(MKey&&)=default;
52 
53  MKey(QChar,Modifiers m=None);
54  MKey(KeyType t,Modifiers m=None):mmod(m),mtype(t){}
55 
56  MKey& operator=(const MKey&)=default;
57  MKey& operator=(MKey&&)=default;
58 
59  bool operator==(const MKey&);
60  bool operator!=(const MKey&k){return !operator==(k);}
61 
62  bool isValid()const{return mtype!=AllKeyRelease;}
63 
64  Modifiers modifiers()const{return mmod;}
65  QChar toChar(bool ignoremodifier=false)const;
66  bool isPrintable(bool ignoremodifier=false)const;
67 
69  QString toCharName()const;
71  static MKey fromCharName(QString);
72 
73  MKey toLower()const{MKey r(*this);r.mchar=r.mchar.toLower();return r;}
74  MKey toUpper()const{MKey r(*this);r.mchar=r.mchar.toUpper();return r;}
75  private:
76  Modifiers mmod=None;
77  KeyType mtype=AllKeyRelease;
78  QChar mchar;
79 };
80 
81 Q_DECLARE_OPERATORS_FOR_FLAGS(MKey::Modifiers)
82 
83 class MKeySequence:public QList<MKey>
84 {
85  public:
87  MKeySequence(const QList< MKey >& l):QList< MKey >(l){}
88  MKeySequence(const MKeySequence& other)=default;
89  MKeySequence(MKeySequence&&)=default;
90 
91  MKeySequence& operator=(const QList<MKey>&l){QList<MKey>::operator=(l);return *this;}
92  MKeySequence& operator=(const MKeySequence&)=default;
93  MKeySequence& operator=(MKeySequence&&)=default;
94 
95  QString toString()const;
96 };
97 
99 {
100  inline static MKey::Modifiers modconv(unsigned char m){
101  MKey::Modifiers r=MKey::None;
102  if(m&0x11)r=MKey::Ctrl;
103  if(m&0x22)r|=MKey::Shift;
104  if(m&0x04)r|=MKey::Alt;
105  if(m&0x40)r|=MKey::AltGr;
106  if(m&0x88)r|=MKey::GUI;
107  return r;
108  }
109  public:
110 
112  MHidKeyEvent(unsigned char mod,QByteArray keys):mmod(modconv(mod)),mkeys(keys){reduce();}
113  MHidKeyEvent(const MHidKeyEvent&)=default;
114  MHidKeyEvent(MHidKeyEvent&&)=default;
115 
116  MHidKeyEvent& operator=(const MHidKeyEvent&)=default;
117  MHidKeyEvent& operator=(MHidKeyEvent&&)=default;
118 
119  MKey::Modifiers modifiers()const{return mmod;}
120  QByteArray keycodes()const{return mkeys;}
121  QDateTime timestamp()const{return mtime;}
122  int lastkeycode()const{if(mkeys.size()>0)return mkeys[mkeys.size()-1];else return 0;}
123 
124  private:
125  QDateTime mtime=QDateTime::currentDateTime();
126  MKey::Modifiers mmod=MKey::None;
127  QByteArray mkeys;
128  void reduce();
129 };
130 
131 class MHidKeySequence:public QList<MHidKeyEvent>
132 {
133  public:
135  MHidKeySequence(QList< MHidKeyEvent >& other):QList< MHidKeyEvent >(other){}
136  MHidKeySequence(const MHidKeySequence&)=default;
137  MHidKeySequence(MHidKeySequence&&)=default;
138 
139  MHidKeySequence& operator=(const MHidKeySequence&)=default;
141 
142  qint64 msSinceLastKey()const;
143 };
144 
146 {
147  public:
149  MHidKeyLayout();
151  explicit MHidKeyLayout(QString name);
152  MHidKeyLayout(const MHidKeyLayout&)=default;
153  MHidKeyLayout(MHidKeyLayout&&)=default;
154 
155  MHidKeyLayout& operator=(const MHidKeyLayout&)=default;
157 
158  MKeySequence toKeys(const MHidKeySequence&)const;
159  QString toString(const MHidKeySequence&)const;
160 
165  bool loadLayout(QString filename);
166  bool parseLayout(QString content,QString filename=QString());
167  void resetLayout();
168 
169  QString toLayoutDoc()const;
170 
171  void setKeyMapping(unsigned char keycode,const QList<MKey> &c);
172  void clearKeyMapping();
173 
175  QString layoutName()const{return mlname;}
178  QString layoutCodeName()const;
180  QString layoutFileName()const{return mfilename;}
181 
182  void setLayoutName(QString l){mlname=l;}
183 
185  bool isValid()const{return mkeymap.size()>0;}
186 
187  static QMap<QString,QString> findLayouts();
188  private:
189  QString mfilename,mlname;
190  QMap<unsigned char,QList<MKey>>mkeymap;
191 };
192 
193 #endif
KeyType
Definition: bcskeyboard.h:35
Definition: bcskeyboard.h:39
Definition: bcskeyboard.h:98
MKeySequence toKeys(const MHidKeySequence &) const
Definition: bcskeyboard.cpp:132
static QMap< QString, QString > findLayouts()
Definition: bcskeyboard.cpp:153
MKeySequence(const QList< MKey > &l)
Definition: bcskeyboard.h:87
MHidKeySequence & operator=(const MHidKeySequence &)=default
Modifier
Definition: bcskeyboard.h:25
QString layoutFileName() const
returns the full file name of the layout
Definition: bcskeyboard.h:180
Definition: bcskeyboard.h:37
Modifiers modifiers() const
Definition: bcskeyboard.h:64
void resetLayout()
Definition: bcskeyboard.cpp:39
Definition: bcskeyboard.h:36
void clearKeyMapping()
Definition: bcskeyboard.cpp:34
int lastkeycode() const
Definition: bcskeyboard.h:122
Definition: bcskeyboard.h:131
MHidKeyLayout()
loads the base layout only
Definition: bcskeyboard.cpp:18
MHidKeyEvent & operator=(const MHidKeyEvent &)=default
QString layoutCodeName() const
returns the iso code of the layout (base of the file name) or an empty string if this is a custom lay...
Definition: bcskeyboard.cpp:28
bool operator==(const MKey &)
Definition: bcskeyboard.cpp:198
MKey(KeyType t, Modifiers m=None)
Definition: bcskeyboard.h:54
QChar toChar(bool ignoremodifier=false) const
Definition: bcskeyboard.cpp:278
bool isValid() const
returns true if this is a populated layout
Definition: bcskeyboard.h:185
Definition: bcskeyboard.h:22
MHidKeySequence(QList< MHidKeyEvent > &other)
Definition: bcskeyboard.h:135
MKey toUpper() const
Definition: bcskeyboard.h:74
Definition: bcskeyboard.h:40
MHidKeyLayout & operator=(const MHidKeyLayout &)=default
MHidKeyEvent(unsigned char mod, QByteArray keys)
Definition: bcskeyboard.h:112
Definition: bcskeyboard.h:29
Definition: bcskeyboard.h:28
static MKey fromCharName(QString)
returns a key object matching the encoded character name from a layout file
Definition: bcskeyboard.cpp:231
Definition: bcskeyboard.h:43
Definition: bcskeyboard.h:30
Q_DECLARE_FLAGS(Modifiers, Modifier)
Definition: bcskeyboard.h:83
Q_DECLARE_OPERATORS_FOR_FLAGS(MLocalFormat::MoneyFlags)
QString layoutName() const
returns the human readable name of the layout
Definition: bcskeyboard.h:175
Definition: bcskeyboard.h:44
Definition: bcskeyboard.h:41
bool isPrintable(bool ignoremodifier=false) const
Definition: bcskeyboard.cpp:266
QByteArray keycodes() const
Definition: bcskeyboard.h:120
Definition: bcskeyboard.h:145
QDateTime timestamp() const
Definition: bcskeyboard.h:121
Definition: bcskeyboard.h:38
Definition: bcskeyboard.h:42
Definition: bcskeyboard.h:26
MKeySequence & operator=(const QList< MKey > &l)
Definition: bcskeyboard.h:91
QString toString(const MHidKeySequence &) const
Definition: bcskeyboard.cpp:148
bool parseLayout(QString content, QString filename=QString())
Definition: bcskeyboard.cpp:90
bool loadLayout(QString filename)
attempts to load a layout file, normally the name mentioned should not contain a complete path or the...
Definition: bcskeyboard.cpp:65
MHidKeyEvent()
Definition: bcskeyboard.h:111
Definition: bcskeyboard.h:31
MKey & operator=(const MKey &)=default
bool isValid() const
Definition: bcskeyboard.h:62
MHidKeySequence()
Definition: bcskeyboard.h:134
Definition: bcskeyboard.h:27
QString toLayoutDoc() const
Definition: bcskeyboard.cpp:51
MKeySequence()
Definition: bcskeyboard.h:86
QString toCharName() const
returns the encoded character name as used in layout files
Definition: bcskeyboard.cpp:207
MKey toLower() const
Definition: bcskeyboard.h:73
void setKeyMapping(unsigned char keycode, const QList< MKey > &c)
Definition: bcskeyboard.cpp:45
bool operator!=(const MKey &k)
Definition: bcskeyboard.h:60
Definition: bcskeyboard.h:45
qint64 msSinceLastKey() const
Definition: bcskeyboard.cpp:332
MKey::Modifiers modifiers() const
Definition: bcskeyboard.h:119
void setLayoutName(QString l)
Definition: bcskeyboard.h:182
MKey()
Definition: bcskeyboard.h:48